vendor/shopware/core/Framework/DataAbstractionLayer/Search/Criteria.php line 127

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Search;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  4. use Shopware\Core\Framework\DataAbstractionLayer\FieldSerializer\JsonFieldSerializer;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Aggregation;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Grouping\FieldGrouping;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Parser\AggregationParser;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Query\ScoreQuery;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\Framework\Feature;
  13. use Shopware\Core\Framework\Struct\StateAwareTrait;
  14. use Shopware\Core\Framework\Struct\Struct;
  15. /**
  16.  * @final tag:v6.5.0
  17.  */
  18. class Criteria extends Struct implements \Stringable
  19. {
  20.     use StateAwareTrait;
  21.     /**
  22.      * no total count will be selected. Should be used if no pagination required (fastest)
  23.      */
  24.     public const TOTAL_COUNT_MODE_NONE 0;
  25.     /**
  26.      * exact total count will be selected. Should be used if an exact pagination is required (slow)
  27.      */
  28.     public const TOTAL_COUNT_MODE_EXACT 1;
  29.     /**
  30.      * fetches limit * 5 + 1. Should be used if pagination can work with "next page exists" (fast)
  31.      */
  32.     public const TOTAL_COUNT_MODE_NEXT_PAGES 2;
  33.     /**
  34.      * @var FieldSorting[]
  35.      */
  36.     protected $sorting = [];
  37.     /**
  38.      * @var Filter[]
  39.      */
  40.     protected $filters = [];
  41.     /**
  42.      * @var Filter[]
  43.      */
  44.     protected $postFilters = [];
  45.     /**
  46.      * @var Aggregation[]
  47.      */
  48.     protected $aggregations = [];
  49.     /**
  50.      * @var ScoreQuery[]
  51.      */
  52.     protected $queries = [];
  53.     /**
  54.      * @var FieldGrouping[]
  55.      */
  56.     protected $groupFields = [];
  57.     /**
  58.      * @var int|null
  59.      */
  60.     protected $offset;
  61.     /**
  62.      * @var int|null
  63.      */
  64.     protected $limit;
  65.     /**
  66.      * @var int
  67.      */
  68.     protected $totalCountMode self::TOTAL_COUNT_MODE_NONE;
  69.     /**
  70.      * @var Criteria[]
  71.      */
  72.     protected $associations = [];
  73.     /**
  74.      * @var string[]|array<int, string[]>
  75.      */
  76.     protected $ids = [];
  77.     /**
  78.      * @var bool
  79.      */
  80.     protected $inherited false;
  81.     /**
  82.      * @var string|null
  83.      */
  84.     protected $term;
  85.     /**
  86.      * @var array|null
  87.      */
  88.     protected $includes;
  89.     /**
  90.      * @var string|null
  91.      */
  92.     protected $title;
  93.     protected array $fields = [];
  94.     public function __construct(?array $ids null)
  95.     {
  96.         if ($ids === null) {
  97.             return;
  98.         }
  99.         $ids array_filter($ids);
  100.         if (empty($ids)) {
  101.             Feature::triggerDeprecationOrThrow(
  102.                 'v6.5.0.0',
  103.                 'The `Criteria()` constructor does not support passing an empty array of ids from v6.5.0.0 onwards'
  104.             );
  105.             if (Feature::isActive('FEATURE_NEXT_16710')) {
  106.                 throw new \RuntimeException('Empty ids provided in criteria');
  107.             }
  108.         }
  109.         $this->ids $ids;
  110.     }
  111.     public function __toString(): string
  112.     {
  113.         $parsed = (new CriteriaArrayConverter(new AggregationParser()))->convert($this);
  114.         return JsonFieldSerializer::encodeJson($parsed);
  115.     }
  116.     /**
  117.      * @return string[]|array<int, string[]>
  118.      */
  119.     public function getIds(): array
  120.     {
  121.         return $this->ids;
  122.     }
  123.     public function getOffset(): ?int
  124.     {
  125.         return $this->offset;
  126.     }
  127.     public function getLimit(): ?int
  128.     {
  129.         return $this->limit;
  130.     }
  131.     public function getTotalCountMode(): int
  132.     {
  133.         return $this->totalCountMode;
  134.     }
  135.     /**
  136.      * @return FieldSorting[]
  137.      */
  138.     public function getSorting(): array
  139.     {
  140.         return $this->sorting;
  141.     }
  142.     /**
  143.      * @return Aggregation[]
  144.      */
  145.     public function getAggregations(): array
  146.     {
  147.         return $this->aggregations;
  148.     }
  149.     public function getAggregation(string $name): ?Aggregation
  150.     {
  151.         return $this->aggregations[$name] ?? null;
  152.     }
  153.     /**
  154.      * @return Filter[]
  155.      */
  156.     public function getFilters(): array
  157.     {
  158.         return $this->filters;
  159.     }
  160.     public function hasEqualsFilter($field): bool
  161.     {
  162.         return \count(array_filter($this->filters, static function (Filter $filter) use ($field) {
  163.             /* EqualsFilter $filter */
  164.             return $filter instanceof EqualsFilter && $filter->getField() === $field;
  165.         })) > 0;
  166.     }
  167.     /**
  168.      * @return Filter[]
  169.      */
  170.     public function getPostFilters(): array
  171.     {
  172.         return $this->postFilters;
  173.     }
  174.     /**
  175.      * @return ScoreQuery[]
  176.      */
  177.     public function getQueries(): array
  178.     {
  179.         return $this->queries;
  180.     }
  181.     /**
  182.      * @return Criteria[]
  183.      */
  184.     public function getAssociations(): array
  185.     {
  186.         return $this->associations;
  187.     }
  188.     /**
  189.      * Returns the criteria for the provided association path. Also supports nested paths
  190.      *
  191.      * e.g `$criteria->getAssociation('categories.media.thumbnails')`
  192.      *
  193.      * @throws InconsistentCriteriaIdsException
  194.      */
  195.     public function getAssociation(string $path): Criteria
  196.     {
  197.         $parts explode('.'$path);
  198.         $criteria $this;
  199.         foreach ($parts as $part) {
  200.             if ($part === 'extensions') {
  201.                 continue;
  202.             }
  203.             if (!$criteria->hasAssociation($part)) {
  204.                 $criteria->associations[$part] = new Criteria();
  205.             }
  206.             $criteria $criteria->associations[$part];
  207.         }
  208.         return $criteria;
  209.     }
  210.     public function addFilter(Filter ...$queries): self
  211.     {
  212.         foreach ($queries as $query) {
  213.             $this->filters[] = $query;
  214.         }
  215.         return $this;
  216.     }
  217.     public function setFilter(string $keyFilter $filter): self
  218.     {
  219.         $this->filters[$key] = $filter;
  220.         return $this;
  221.     }
  222.     public function addSorting(FieldSorting ...$sorting): self
  223.     {
  224.         foreach ($sorting as $sort) {
  225.             $this->sorting[] = $sort;
  226.         }
  227.         return $this;
  228.     }
  229.     public function addAggregation(Aggregation ...$aggregations): self
  230.     {
  231.         foreach ($aggregations as $aggregation) {
  232.             $this->aggregations[$aggregation->getName()] = $aggregation;
  233.         }
  234.         return $this;
  235.     }
  236.     public function addPostFilter(Filter ...$queries): self
  237.     {
  238.         foreach ($queries as $query) {
  239.             $this->postFilters[] = $query;
  240.         }
  241.         return $this;
  242.     }
  243.     public function addQuery(ScoreQuery ...$queries): self
  244.     {
  245.         foreach ($queries as $query) {
  246.             $this->queries[] = $query;
  247.         }
  248.         return $this;
  249.     }
  250.     /**
  251.      * Add for each part of the provided path an association
  252.      *
  253.      * e.g
  254.      *
  255.      * $criteria->addAssociation('categories.media.thumbnails')
  256.      *
  257.      * @throws InconsistentCriteriaIdsException
  258.      */
  259.     public function addAssociation(string $path): self
  260.     {
  261.         $parts explode('.'$path);
  262.         $criteria $this;
  263.         foreach ($parts as $part) {
  264.             if (mb_strtolower($part) === 'extensions') {
  265.                 continue;
  266.             }
  267.             $criteria $criteria->getAssociation($part);
  268.         }
  269.         return $this;
  270.     }
  271.     /**
  272.      * Allows to add multiple associations paths
  273.      *
  274.      * e.g.:
  275.      *
  276.      * $criteria->addAssociations([
  277.      *      'prices',
  278.      *      'cover.media',
  279.      *      'categories.cover.media'
  280.      * ]);
  281.      *
  282.      * @throws InconsistentCriteriaIdsException
  283.      */
  284.     public function addAssociations(array $paths): self
  285.     {
  286.         foreach ($paths as $path) {
  287.             $this->addAssociation($path);
  288.         }
  289.         return $this;
  290.     }
  291.     public function hasAssociation(string $field): bool
  292.     {
  293.         return isset($this->associations[$field]);
  294.     }
  295.     public function resetSorting(): self
  296.     {
  297.         $this->sorting = [];
  298.         return $this;
  299.     }
  300.     public function resetAssociations(): self
  301.     {
  302.         $this->associations = [];
  303.         return $this;
  304.     }
  305.     public function resetQueries(): self
  306.     {
  307.         $this->queries = [];
  308.         return $this;
  309.     }
  310.     public function resetFilters(): self
  311.     {
  312.         $this->filters = [];
  313.         return $this;
  314.     }
  315.     public function resetPostFilters(): self
  316.     {
  317.         $this->postFilters = [];
  318.         return $this;
  319.     }
  320.     public function resetAggregations(): self
  321.     {
  322.         $this->aggregations = [];
  323.         return $this;
  324.     }
  325.     public function setTotalCountMode(int $totalCountMode): self
  326.     {
  327.         $this->totalCountMode $totalCountMode;
  328.         return $this;
  329.     }
  330.     public function setLimit(?int $limit): self
  331.     {
  332.         $this->limit $limit;
  333.         return $this;
  334.     }
  335.     public function setOffset(?int $offset): self
  336.     {
  337.         $this->offset $offset;
  338.         return $this;
  339.     }
  340.     public function getAggregationQueryFields(): array
  341.     {
  342.         return $this->collectFields([
  343.             $this->filters,
  344.             $this->queries,
  345.         ]);
  346.     }
  347.     public function getSearchQueryFields(): array
  348.     {
  349.         return $this->collectFields([
  350.             $this->filters,
  351.             $this->postFilters,
  352.             $this->sorting,
  353.             $this->queries,
  354.             $this->groupFields,
  355.         ]);
  356.     }
  357.     public function getFilterFields(): array
  358.     {
  359.         return $this->collectFields([
  360.             $this->filters,
  361.             $this->postFilters,
  362.         ]);
  363.     }
  364.     public function getAllFields(): array
  365.     {
  366.         return $this->collectFields([
  367.             $this->filters,
  368.             $this->postFilters,
  369.             $this->sorting,
  370.             $this->queries,
  371.             $this->groupFields,
  372.             $this->aggregations,
  373.         ]);
  374.     }
  375.     /**
  376.      * @param string[]|array<int, string[]> $ids
  377.      */
  378.     public function setIds(array $ids): self
  379.     {
  380.         $this->ids $ids;
  381.         return $this;
  382.     }
  383.     public function getTerm(): ?string
  384.     {
  385.         return $this->term;
  386.     }
  387.     public function setTerm(?string $term): self
  388.     {
  389.         $this->term $term;
  390.         return $this;
  391.     }
  392.     /**
  393.      * @param string[]|array<int, string[]> $ids
  394.      */
  395.     public function cloneForRead(array $ids = []): Criteria
  396.     {
  397.         $self = new self($ids);
  398.         $self->setTitle($this->getTitle());
  399.         $associations = [];
  400.         foreach ($this->associations as $name => $association) {
  401.             $associations[$name] = clone $association;
  402.         }
  403.         $self->associations $associations;
  404.         $self->fields $this->fields;
  405.         return $self;
  406.     }
  407.     public function addGroupField(FieldGrouping $grouping): self
  408.     {
  409.         $this->groupFields[] = $grouping;
  410.         return $this;
  411.     }
  412.     /**
  413.      * @return FieldGrouping[]
  414.      */
  415.     public function getGroupFields(): array
  416.     {
  417.         return $this->groupFields;
  418.     }
  419.     public function resetGroupFields(): self
  420.     {
  421.         $this->groupFields = [];
  422.         return $this;
  423.     }
  424.     public function setIncludes(?array $includes): void
  425.     {
  426.         $this->includes $includes;
  427.     }
  428.     public function getIncludes()
  429.     {
  430.         return $this->includes;
  431.     }
  432.     public function getApiAlias(): string
  433.     {
  434.         return 'dal_criteria';
  435.     }
  436.     public function useIdSorting(): bool
  437.     {
  438.         if (empty($this->getIds())) {
  439.             return false;
  440.         }
  441.         // manual sorting provided
  442.         if (!empty($this->getSorting())) {
  443.             return false;
  444.         }
  445.         // result will be sorted by interpreted search term and the calculated ranking
  446.         if (!empty($this->getTerm())) {
  447.             return false;
  448.         }
  449.         // result will be sorted by calculated ranking
  450.         if (!empty($this->getQueries())) {
  451.             return false;
  452.         }
  453.         return true;
  454.     }
  455.     public function removeAssociation(string $association): void
  456.     {
  457.         unset($this->associations[$association]);
  458.     }
  459.     public function getTitle(): ?string
  460.     {
  461.         return $this->title;
  462.     }
  463.     public function setTitle(?string $title): void
  464.     {
  465.         $this->title $title;
  466.     }
  467.     /**
  468.      * @internal
  469.      */
  470.     public function addFields(array $fields): self
  471.     {
  472.         Feature::throwException('v6.5.0.0''Partial data loading is not active'false);
  473.         $this->fields array_merge($this->fields$fields);
  474.         return $this;
  475.     }
  476.     /**
  477.      * @internal
  478.      */
  479.     public function getFields(): array
  480.     {
  481.         return $this->fields;
  482.     }
  483.     private function collectFields(array $parts): array
  484.     {
  485.         $fields = [];
  486.         foreach ($parts as $part) {
  487.             /** @var CriteriaPartInterface $item */
  488.             foreach ($part as $item) {
  489.                 foreach ($item->getFields() as $field) {
  490.                     $fields[] = $field;
  491.                 }
  492.             }
  493.         }
  494.         return $fields;
  495.     }
  496. }