Exemple #1
0
 /**
  * @param $item
  * @return bool
  * @throws \Braincrafted\ArrayQuery\Exception\UnkownOperatorException
  */
 protected function evaluate(array $item)
 {
     foreach ($this->where as $clause) {
         if (false === $this->whereEvaluation->evaluate($item, $clause)) {
             return false;
         }
     }
     return true;
 }
 /**
  * @param WhereEvaluation $whereEvaluation
  *
  * @return WhereEvaluation
  */
 protected function getWhereEvaluation($whereEvaluation = null)
 {
     if (null !== $whereEvaluation && false === $whereEvaluation instanceof WhereEvaluation) {
         throw new \InvalidArgumentException('Argument "whereEvaluation" must be an instance of WhereEvaluation.');
     }
     if (null === $whereEvaluation) {
         $whereEvaluation = new WhereEvaluation();
     }
     foreach (OperatorFactory::getOperators() as $operator) {
         $whereEvaluation->addOperator($operator);
     }
     foreach (FilterFactory::getFilters() as $filter) {
         $whereEvaluation->addFilter($filter);
     }
     return $whereEvaluation;
 }
 /**
  * @covers Braincrafted\ArrayQuery\WhereEvaluation::addFilter()
  * @covers Braincrafted\ArrayQuery\WhereEvaluation::evaluate()
  * @covers Braincrafted\ArrayQuery\WhereEvaluation::evaluateFilter()
  * @covers Braincrafted\ArrayQuery\Evaluation::evaluateFilter()
  */
 public function testEvaluateFilterWithArgs()
 {
     $operator = m::mock('Braincrafted\\ArrayQuery\\Operator\\OperatorInterface');
     $operator->shouldReceive('getName')->andReturn('.');
     $operator->shouldReceive('evaluate')->with('y', 'x')->andReturn(true);
     $this->where->addOperator($operator);
     $filter = m::mock('Braincrafted\\ArrayQuery\\Filter\\FilterInterface');
     $filter->shouldReceive('getName')->andReturn('test');
     $filter->shouldReceive('evaluate')->with('x', ['a'])->andReturn('y');
     $this->where->addFilter($filter);
     $this->assertTrue($this->where->evaluate(['a' => 'x'], ['key' => 'a', 'value' => 'x', 'operator' => '.', 'filters' => 'test a']));
 }