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

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