示例#1
0
 /**
  * @param CriteriaInterface $criteria
  * @param array $filter
  * @return CriteriaInterface
  */
 private function applyFilter(CriteriaInterface $criteria, array $filter)
 {
     foreach ($filter as $expressionString => $value) {
         $criteriaMap = $this->config->getCriteriaMap($criteria->getEntityName());
         if (isset($criteriaMap[$expressionString])) {
             $expressionString = $criteriaMap[$expressionString];
         }
         if (in_array($expressionString, ['limit', 'offset'])) {
             $value = (int) $value;
             if ($value < 0) {
                 throw new RuntimeException(sprintf('Predicate %s must unsigned int, %s given', $expressionString, $value));
             }
             $criteria->{$expressionString}($value);
             continue;
         }
         if ($expressionString == 'page') {
             if (!isset($filter['limit'])) {
                 throw new RuntimeException(sprintf('Predicate %s require limit', $expressionString));
             }
             $criteria->offset($filter['limit'] * ($value - 1));
             continue;
         }
         if ($expressionString == 'order') {
             $criteria->order($value);
             continue;
         }
         if (strpos($expressionString, '.') !== false) {
             $expressionArray = explode('.', $expressionString);
         } else {
             $expressionArray = explode('_', $expressionString);
         }
         if (count($expressionArray) > 2) {
             continue;
         }
         if (count($expressionArray) == 2) {
             list($attribute, $method) = $expressionArray;
             if (in_array($method, ['isNull', 'isNotNull']) && $value) {
                 $criteria->{$method}($attribute);
                 continue;
             }
             if ($method == 'between') {
                 if (!is_array($value) || !isset($value[0]) || !isset($value[1])) {
                     throw new RuntimeException(sprintf('Predicate %s must contain array [MIN_VALUE, MAX_VALUE], ', $method));
                 }
                 $criteria->between($attribute, $value[0], $value[1]);
                 continue;
             }
             if (!method_exists($criteria, $method)) {
                 throw new RuntimeException(sprintf('Predicate %s does not exists', $method));
             }
             call_user_func([$criteria, $method], $attribute, $value);
         }
         if (count($expressionArray) == 1) {
             $customCriteriaInstance = $this->buildCustomCriteria($expressionArray[0], $criteria);
             $customCriteriaInstance($criteria, $value);
         }
     }
     return $criteria;
 }
示例#2
0
 public function testGetCriteriaMap()
 {
     $criteriaMap = $this->config->getCriteriaMap('Task');
     $this->assertEquals($this->entityMap['Task']['criteriaMap'], $criteriaMap);
 }