Example #1
0
 public function testSwitchParameters()
 {
     $input = array('where' => array('foo' => 'bar'));
     $eventDispatcher = new EventDispatcher();
     $eventDispatcher->addListener(OperatorEvent::EVENT_NAME, function (OperatorEvent $event) {
         if ($event->getField() === 'foo') {
             $event->setField('pew-pew');
             if ($event->getValue() === 'bar') {
                 $value = [1, 2, 3, 4];
             } else {
                 $value = [5, 6, 7, 8];
             }
             $event->setValue($value);
         }
     });
     $builder = new Builder($eventDispatcher);
     $builder->build($input);
     $this->assertCount(1, $builder->getFilters());
     $filters = $builder->getFilters();
     $this->assertEquals('and', $filters[0]->getGroup());
     /* @var $expected ConditionInterface[] */
     $expected = array(FilterConditionBuilder::create(FilterCondition::CONDITION_IN, 'pew-pew', [1, 2, 3, 4], $eventDispatcher));
     foreach ($filters[0]->getConditions() as $key => $condition) {
         $this->assertEquals($expected[$key]->getValue(), $condition->getValue());
         $this->assertEquals($expected[$key]->getField(), $condition->getField());
         $this->assertEquals($expected[$key]->getOperator(), $condition->getOperator());
     }
 }
 /**
  * @inheritdoc
  * @return $this
  */
 public function build($conditions = null)
 {
     if (!in_array($this->group, $this->acceptedGroups)) {
         throw new InvalidConditionException('invalid group');
     }
     if ($this->dispatcher) {
         $this->dispatcher->dispatch(GroupEvent::EVENT_NAME, new GroupEvent($this->group));
     }
     $results = array();
     foreach ($this->parameters as $field => $values) {
         $operator = null;
         if (is_scalar($values) || ArrayUtils::isList($values)) {
             $values = [FilterCondition::CONDITION_EQ => $values];
         }
         //            if (is_array($values) && !ArrayUtils::isList($values)) {
         //                $operator = $condition;
         //            } else {
         //                $fieldValues = array($condition => $fieldValues);
         //            }
         foreach ($values as $operator => $value) {
             $builder = new FilterConditionBuilder();
             if ($operator !== null) {
                 $builder->setCompareOperator($operator);
             }
             $builder->setEventDispatcher($this->dispatcher);
             $builder->setConditions($field, $value);
             try {
                 $results[] = $builder->build();
             } catch (InvalidConditionException $e) {
                 continue;
             }
         }
     }
     $this->checkCondition($results);
     $this->conditions = $results;
     return $this;
 }