vendor/shopware/core/Framework/DataAbstractionLayer/Search/EntitySearchResult.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Search;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\AggregationResultCollection;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Struct\StateAwareTrait;
  9. /**
  10.  * @final
  11.  *
  12.  * @extends EntityCollection<Entity>
  13.  */
  14. #[Package('core')]
  15. class EntitySearchResult extends EntityCollection
  16. {
  17.     use StateAwareTrait;
  18.     /**
  19.      * @var string
  20.      */
  21.     protected $entity;
  22.     /**
  23.      * @var int
  24.      */
  25.     protected $total;
  26.     /**
  27.      * @var EntityCollection<Entity>
  28.      */
  29.     protected $entities;
  30.     /**
  31.      * @var AggregationResultCollection
  32.      */
  33.     protected $aggregations;
  34.     /**
  35.      * @var Criteria
  36.      */
  37.     protected $criteria;
  38.     /**
  39.      * @var Context
  40.      */
  41.     protected $context;
  42.     /**
  43.      * @var int
  44.      */
  45.     protected $page;
  46.     /**
  47.      * @var int|null
  48.      */
  49.     protected $limit;
  50.     /**
  51.      * @phpstan-ignore-next-line -> we can't generalize the type of EntityCollection here
  52.      */
  53.     final public function __construct(
  54.         string $entity,
  55.         int $total,
  56.         EntityCollection $entities,
  57.         ?AggregationResultCollection $aggregations,
  58.         Criteria $criteria,
  59.         Context $context
  60.     ) {
  61.         $this->entities $entities;
  62.         $this->total $total;
  63.         $this->aggregations $aggregations ?? new AggregationResultCollection();
  64.         $this->criteria $criteria;
  65.         $this->context $context;
  66.         $this->limit $criteria->getLimit();
  67.         $this->page = !$criteria->getLimit() ? : (int) ceil((($criteria->getOffset() ?? 0) + 1) / $criteria->getLimit());
  68.         parent::__construct($entities);
  69.         $this->entity $entity;
  70.     }
  71.     public function filter(\Closure $closure)
  72.     {
  73.         return $this->createNew($this->entities->filter($closure));
  74.     }
  75.     public function slice(int $offset, ?int $length null)
  76.     {
  77.         return $this->createNew($this->entities->slice($offset$length));
  78.     }
  79.     public function getTotal(): int
  80.     {
  81.         return $this->total;
  82.     }
  83.     /**
  84.      * @return EntityCollection<Entity>
  85.      */
  86.     public function getEntities(): EntityCollection
  87.     {
  88.         return $this->entities;
  89.     }
  90.     public function getAggregations(): AggregationResultCollection
  91.     {
  92.         return $this->aggregations;
  93.     }
  94.     public function getCriteria(): Criteria
  95.     {
  96.         return $this->criteria;
  97.     }
  98.     public function getContext(): Context
  99.     {
  100.         return $this->context;
  101.     }
  102.     public function clear(): void
  103.     {
  104.         parent::clear();
  105.         $this->entities->clear();
  106.     }
  107.     public function add($entity): void
  108.     {
  109.         parent::add($entity);
  110.         $this->entities->add($entity);
  111.     }
  112.     public function jsonSerialize(): array
  113.     {
  114.         $vars get_object_vars($this);
  115.         unset($vars['criteria']);
  116.         unset($vars['context']);
  117.         unset($vars['entities']);
  118.         $this->convertDateTimePropertiesToJsonStringRepresentation($vars);
  119.         return $vars;
  120.     }
  121.     public function getApiAlias(): string
  122.     {
  123.         return 'dal_entity_search_result';
  124.     }
  125.     public function getPage(): ?int
  126.     {
  127.         return $this->page;
  128.     }
  129.     public function setPage(int $page): void
  130.     {
  131.         $this->page $page;
  132.     }
  133.     public function getLimit(): ?int
  134.     {
  135.         return $this->limit;
  136.     }
  137.     public function setLimit(int $limit): void
  138.     {
  139.         $this->limit $limit;
  140.     }
  141.     public function getEntity(): string
  142.     {
  143.         return $this->entity;
  144.     }
  145.     public function setEntity(string $entity): void
  146.     {
  147.         $this->entity $entity;
  148.     }
  149.     /**
  150.      * @return Entity|null
  151.      */
  152.     public function getAt(int $position)
  153.     {
  154.         return $this->entities->getAt($position);
  155.     }
  156.     /**
  157.      * @return static
  158.      *
  159.      * @deprecated tag:v6.6.0  - reason:return-type-change - Return type will be changed to `static`
  160.      */
  161.     protected function createNew(iterable $elements = [])
  162.     {
  163.         if (!($elements instanceof EntityCollection)) {
  164.             $elements = new EntityCollection($elements);
  165.         }
  166.         return new static(
  167.             $this->entity,
  168.             $elements->count(),
  169.             $elements,
  170.             $this->aggregations,
  171.             $this->criteria,
  172.             $this->context
  173.         );
  174.     }
  175. }