public function testHowWellItWorksWithMixedFilters() { $input = array(array('property' => 'orderTotal', 'value' => array('eq:10', 'eq:20')), array(array('property' => 'user.firstname', 'value' => 'like:Se%'), array('property' => 'user.lastname', 'value' => 'like:Li%'))); $filters = new Filters($input); $this->assertEquals(2, count($filters)); $this->assertInstanceOf(Filter::clazz(), $filters[0]); $this->assertInstanceOf(OrFilter::clazz(), $filters[1]); $this->assertEquals('orderTotal', $filters[0]->getProperty()); $this->assertNull($filters[0]->getComparator()); $this->assertSame(array(array('comparator' => 'eq', 'value' => '10'), array('comparator' => 'eq', 'value' => '20')), $filters[0]->getValue()); /* @var Filter[] $subFilters */ $subFilters = $filters[1]->getFilters(); $this->assertTrue(is_array($subFilters)); $this->assertEquals(2, count($subFilters)); }
public function testHowWellItWorksWithValidInput() { $f = new OrFilter(array(array('property' => 'user.firstname', 'value' => 'like:Se%'), array('property' => 'user.lastname', 'value' => 'like:Li%'))); $this->assertTrue($f->isValid()); $filters = $f->getFilters(); $this->assertEquals(2, count($filters)); $this->assertInstanceOf(Filter::clazz(), $filters[0]); $this->assertInstanceOf(Filter::clazz(), $filters[1]); $this->assertEquals('user.firstname', $filters[0]->getProperty()); $this->assertEquals('like', $filters[0]->getComparator()); $this->assertEquals('Se%', $filters[0]->getValue()); $this->assertEquals('user.lastname', $filters[1]->getProperty()); $this->assertEquals('like', $filters[1]->getComparator()); $this->assertEquals('Li%', $filters[1]->getValue()); $compiled = $f->compile(); $this->assertTrue(is_array($compiled)); }
public function testCreate() { $f = Filter::create('price', Filter::COMPARATOR_GREATER_THAN, 500); $this->assertInstanceOf(Filter::clazz(), $f); $this->assertEquals('price', $f->getProperty()); $this->assertEquals(Filter::COMPARATOR_GREATER_THAN, $f->getComparator()); $this->assertEquals(500, $f->getValue()); // --- $f = Filter::create('shippedAt', Filter::COMPARATOR_IS_NULL); $this->assertInstanceOf(Filter::clazz(), $f); $this->assertEquals('shippedAt', $f->getProperty()); $this->assertEquals(Filter::COMPARATOR_IS_NULL, $f->getComparator()); $this->assertNull($f->getValue()); }
private function processFilter(ExpressionManager $expressionManager, Expr\Composite $compositeExpr, QueryBuilder $qb, DoctrineQueryBuilderParametersBinder $binder, Filter $filter) { $name = $filter->getProperty(); $fieldName = $expressionManager->getDqlPropertyName($name); if (in_array($filter->getComparator(), array(Filter::COMPARATOR_IS_NULL, Filter::COMPARATOR_IS_NOT_NULL))) { // these are sort of 'special case' $compositeExpr->add($qb->expr()->{$filter->getComparator()}($fieldName)); } else { $value = $filter->getValue(); $comparatorName = $filter->getComparator(); if (!$this->isUsefulInFilter($filter->getComparator(), $filter->getValue()) || !$this->isUsefulFilter($expressionManager, $name, $value)) { return; } // when "IN" is used in conjunction with TO_MANY type of relation, // then we will treat it in a special way and generate "MEMBER OF" queries // instead $isAdded = false; if ($expressionManager->isAssociation($name)) { $mapping = $expressionManager->getMapping($name); if (in_array($comparatorName, array(Filter::COMPARATOR_IN, Filter::COMPARATOR_NOT_IN)) && in_array($mapping['type'], array(CMI::ONE_TO_MANY, CMI::MANY_TO_MANY))) { $statements = array(); foreach ($value as $id) { $statements[] = sprintf((Filter::COMPARATOR_NOT_IN == $comparatorName ? 'NOT ' : '') . '?%d MEMBER OF %s', $binder->getNextIndex(), $expressionManager->getDqlPropertyName($name)); $binder->bind($this->convertValue($expressionManager, $name, $id)); } if (Filter::COMPARATOR_IN == $comparatorName) { $compositeExpr->add(call_user_func_array(array($qb->expr(), 'orX'), $statements)); } else { $compositeExpr->addMultiple($statements); } $isAdded = true; } } if (!$isAdded) { if (is_array($value) && count($value) != count($value, \COUNT_RECURSIVE)) { // must be "OR-ed" ( multi-dimensional array ) $orStatements = array(); foreach ($value as $orFilter) { if (!$this->isUsefulInFilter($orFilter['comparator'], $orFilter['value']) || !$this->isUsefulFilter($expressionManager, $name, $orFilter['value'])) { continue; } if (in_array($orFilter['comparator'], array(Filter::COMPARATOR_IN, Filter::COMPARATOR_NOT_IN))) { $orStatements[] = $qb->expr()->{$orFilter['comparator']}($fieldName); } else { $orStatements[] = $qb->expr()->{$orFilter['comparator']}($fieldName, '?' . $binder->getNextIndex()); } $binder->bind($orFilter['value']); } $compositeExpr->add(call_user_func_array(array($qb->expr(), 'orX'), $orStatements)); } else { $compositeExpr->add($qb->expr()->{$comparatorName}($fieldName, '?' . $binder->getNextIndex())); $binder->bind($this->convertValue($expressionManager, $name, $value)); } } } }