public function expand(FilterBuilder $filterBuilder, $value, $table, $field, $where)
 {
     $qb = $filterBuilder->getQueryBuilder();
     $getValue = function () use($filterBuilder, $value) {
         return $filterBuilder->placeValue($value);
     };
     call_user_func($this->closure, $qb, $table, $field, $getValue);
 }
 public function expand(FilterBuilder $filterBuilder, $value, $table, $field, $where)
 {
     $qb = $filterBuilder->getQueryBuilder();
     // We assume that the discriminator map name was given and no the FQCN
     if (!class_exists($value)) {
         $value = $filterBuilder->placeValue($value);
     }
     return $this->add($qb, $where, new Comparison($table, 'INSTANCE OF', $value));
 }
 public function expand(FilterBuilder $filterBuilder, $value, $table, $field, $where)
 {
     $qb = $filterBuilder->getQueryBuilder();
     $tableField = $table . '.' . $field;
     if (!$this->options['case_sensitive']) {
         $tableField = $qb->expr()->lower($tableField);
         $value = strtolower($value);
     }
     // Doctrine 2.3 can not handle boolean values
     if (is_bool($value)) {
         $value = $value ? 1 : 0;
     }
     return $this->add($qb, $where, $qb->expr()->eq($tableField, $filterBuilder->placeValue($value)));
 }
 /** @test */
 public function it_adds_filters_to_query_on_the_parent_resources()
 {
     $qb = $this->em->createQueryBuilder()->select('x')->from(Post::class, 'x');
     $manager = $this->getMockBuilder(ApiManager::class)->disableOriginalConstructor()->setMethods(['getBundlePrefix'])->getMock();
     $manager->method('getBundlePrefix')->willReturn('some.prefix');
     $categoryResource = new ApiResource('categories', ['entity' => Category::class, 'filter' => null, 'pagination' => ['enabled' => true, 'limit' => 10], 'form_type' => DynamicFormType::class, 'identifier' => 'id']);
     $categoryResource->setManager($manager);
     $postResource = new ApiResource('posts', ['entity' => Post::class, 'filter' => null, 'pagination' => ['enabled' => true, 'limit' => 10], 'form_type' => DynamicFormType::class, 'identifier' => 'id']);
     $postResource->setParentResource($categoryResource);
     $filter = new FilterDecorator($postResource);
     $builder = new FilterBuilder();
     $builder->setQueryBuilder($qb);
     $filter->buildFilter($builder);
     $filters = $builder->getFilters();
     $this->assertCount(1, $filters);
     $this->assertTrue(isset($filters[FilterDecorator::getFilterName($categoryResource)]));
     $filter = $filters[FilterDecorator::getFilterName($categoryResource)];
     $this->assertEquals(['category.id'], $filter->getFields());
 }
 /**
  * @param FilterInterface $filter
  * @param $searchParams
  * @param $page
  * @param $maxPerPage
  * @param Pagerfanta $pagerfanta
  * @return array
  */
 public function paginate($filter, $searchParams, $page, $maxPerPage, &$pagerfanta = null)
 {
     /** @var QueryBuilder $qb */
     $qb = $this->createQueryBuilder('x');
     $query = FilterBuilder::create()->setQueryBuilder($qb)->setFilter($filter)->buildQuery($searchParams)->getQuery();
     $adapter = new DoctrineORMAdapter($query, true, false);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setAllowOutOfRangePages(true)->setMaxPerPage($maxPerPage)->setCurrentPage($page);
     return iterator_to_array($pagerfanta->getCurrentPageResults());
 }
 public function expand(FilterBuilder $filterBuilder, $value, $table, $field, $where)
 {
     $qb = $filterBuilder->getQueryBuilder();
     $likeString = $this->getLikeString($value);
     return $this->add($qb, $where, $qb->expr()->like($table . '.' . $field, $filterBuilder->placeValue($likeString)));
 }
 /**
  * @param FilterBuilder $builder
  * @param $entity
  * @param EntityManager $em
  * @param ApiResource $parentResource
  * @param string $prefix
  */
 protected function addFilterForParent(FilterBuilder $builder, $entity, EntityManager $em, ApiResource $parentResource, $prefix = '')
 {
     $meta = $em->getClassMetadata($entity);
     $mappings = $this->getAssociationMappings($meta, $parentResource->getEntityClass());
     $builder->add(self::getFilterName($parentResource), EqualFilterType::class, ['fields' => $this->getFields($prefix, $mappings)]);
     if ($parentsParent = $parentResource->getParentResource()) {
         $this->addFilterForParent($builder, $parentResource->getEntityClass(), $em, $parentsParent, $parentResource->getAssocSubResource() . '.');
     }
 }
 /** @test */
 public function the_filter_can_be_set_again()
 {
     $qb = $this->em->getRepository(Post::class)->createQueryBuilder('x');
     $builder = new FilterBuilder();
     $posts = $builder->setQueryBuilder($qb)->setFilter($this->filter)->getResult(['title' => 'Post title']);
     $this->assertCount(1, $posts);
     // Change filter
     $this->filter->defineFilter(function (FilterBuilder $builder) {
         $builder->add('content', LikeFilterType::class);
     });
     // Query again with new filter
     $posts = $builder->setFilter($this->filter)->getResult(['title' => 'Should be ignored', 'content' => 'post content']);
     $this->assertCount(1, $posts);
 }
 public function buildFilter(FilterBuilder $builder)
 {
     $builder->add('title', LikeFilterType::class)->add('content', LikeFilterType::class)->add('createdAt', BetweenFilterType::class);
 }
 /**
  * Where in filter where all values have to match
  *
  * @param FilterBuilder $filterBuilder
  * @param $value
  * @param $table
  * @param $field
  * @param $where
  * @return \Doctrine\ORM\QueryBuilder
  */
 private function buildQueryToMatchAll(FilterBuilder $filterBuilder, $value, $table, $field, $where)
 {
     $qb = $filterBuilder->getQueryBuilder();
     $count = count(array_unique($value));
     return $this->add($qb, $where, $qb->expr()->in($table . '.' . $field, $filterBuilder->placeValue($value)))->groupBy($qb->getRootAliases()[0])->andHaving($qb->expr()->eq($qb->expr()->countDistinct($table), $count));
 }