Пример #1
0
 /**
  * @param ResourceInterface $resource
  * @param QueryBuilder      $queryBuilder
  */
 public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
 {
     $request = $this->requestStack->getCurrentRequest();
     if (null === $request) {
         return;
     }
     $properties = $this->queryExtractor->extractProperties($request);
     $fieldNames = array_flip($this->getClassMetadata($resource)->getFieldNames());
     foreach ($properties as $property => $order) {
         if (!$this->isPropertyEnabled($property) || !isset($fieldNames[$property])) {
             continue;
         } elseif ('' === $order && isset($this->properties[$property])) {
             $order = $this->properties[$property];
         }
         $order = strtoupper($order);
         if ('ASC' === $order || 'DESC' === $order) {
             $queryBuilder->addOrderBy(sprintf('o.%s', $property), $order);
         }
     }
     //        \Symfony\Component\VarDumper\VarDumper::dump($queryBuilder->getDQL());
     //        \Symfony\Component\VarDumper\VarDumper::dump($request->query->all());
     //        \Symfony\Component\VarDumper\VarDumper::dump($properties);die();
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function apply(QueryBuilder $queryBuilder, string $resourceClass, string $operationName = null)
 {
     if (null === ($request = $this->requestStack->getCurrentRequest())) {
         return null;
     }
     $queryValues = $this->queryExtractor->extractProperties($request);
     $metadata = $this->getClassMetadata($resourceClass);
     $queryExpr = [];
     $aliases = [];
     $associationsMetadata = [];
     // Retrieve all doctrine query expressions
     foreach ($queryValues as $key => $value) {
         if (self::PARAMETER_OPERATOR_OR === $key && is_array($value)) {
             /*
              * OR operator case
              *
              * At this point $dataSet is expected to equal to something like this:
              *
              * $value = [
              *    0 => [
              *       0 => [
              *          'property' => [
              *             'operator' => 'operand'
              *          ]
              *       ],
              *       1 => [
              *          'property' => value
              *       ]
              *    ],
              *    1 => [...],
              *    ...
              * ]
              */
             foreach ($value as $index => $dataSet) {
                 // Expect $dataSet to be an array containing 2 parameters
                 if (is_array($dataSet) && 2 === count($dataSet)) {
                     $queries = [];
                     // Handle each "query" of $dataSet
                     $count = 0;
                     foreach ($dataSet as $dataSetElem) {
                         if (false === is_array($dataSetElem)) {
                             continue;
                         }
                         $property = key($dataSetElem);
                         // At this point the value may be either a value or an array (for operators)
                         $expr = $this->handleFilter($queryBuilder, $metadata, $aliases, $associationsMetadata, $property, $dataSetElem[$property], sprintf('or_%s%d%d', $property, $index, $count));
                         $queries = array_merge($queries, $expr);
                         ++$count;
                     }
                     if (2 === count($queries)) {
                         $queryExpr[] = $queryBuilder->expr()->orX($queries[0], $queries[1]);
                     }
                 }
             }
         } else {
             $queryExpr = array_merge($queryExpr, $this->handleFilter($queryBuilder, $metadata, $aliases, $associationsMetadata, $key, $value));
         }
     }
     foreach ($queryExpr as $expr) {
         $queryBuilder->andWhere($expr);
     }
 }