partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

FilterService.php (2105B)


      1 <?php
      2 
      3 namespace PartKeepr\DoctrineReflectionBundle\Services;
      4 
      5 use Doctrine\Bundle\DoctrineBundle\Registry;
      6 use Doctrine\ORM\EntityManager;
      7 use PartKeepr\DoctrineReflectionBundle\Filter\Filter;
      8 
      9 class FilterService
     10 {
     11     /**
     12      * @var EntityManager
     13      */
     14     private $em;
     15 
     16     public function __construct(Registry $registry)
     17     {
     18         $this->em = $registry->getManager();
     19     }
     20 
     21     /**
     22      * Returns a DQL expression for the given filter and alias.
     23      *
     24      * @param Filter $filter    The filter to build the expression for
     25      * @param string $alias     The field alias to search in
     26      * @param string $paramName The parameter name you use to bind the value to
     27      *
     28      * @throws \Exception
     29      *
     30      * @return \Doctrine\ORM\Query\Expr\Comparison
     31      */
     32     public function getExpressionForFilter(Filter $filter, $alias, $paramName)
     33     {
     34         switch (strtolower($filter->getOperator())) {
     35             case Filter::OPERATOR_EQUALS:
     36                 return $this->em->getExpressionBuilder()->eq($alias, $paramName);
     37                 break;
     38             case Filter::OPERATOR_GREATER_THAN:
     39                 return $this->em->getExpressionBuilder()->gt($alias, $paramName);
     40                 break;
     41             case Filter::OPERATOR_GREATER_THAN_EQUALS:
     42                 return $this->em->getExpressionBuilder()->gte($alias, $paramName);
     43                 break;
     44             case Filter::OPERATOR_LESS_THAN:
     45                 return $this->em->getExpressionBuilder()->lt($alias, $paramName);
     46                 break;
     47             case Filter::OPERATOR_LESS_THAN_EQUALS:
     48                 return $this->em->getExpressionBuilder()->lte($alias, $paramName);
     49                 break;
     50             case Filter::OPERATOR_NOT_EQUALS:
     51                 return $this->em->getExpressionBuilder()->neq($alias, $paramName);
     52                 break;
     53             case Filter::OPERATOR_LIKE:
     54                 return $this->em->getExpressionBuilder()->like($alias, $paramName);
     55                 break;
     56             default:
     57                 throw new \Exception('Unknown operator '.$filter->getOperator());
     58         }
     59     }
     60 }