vendor/elements/demi-bundle/src/Elements/Demi/AdditionalService/Search/Listing.php line 113

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Demi\AdditionalService\Search;
  11. use Elements\Demi\AdditionalService\Search\Adapter\Local;
  12. use Elements\Demi\Deskline\AdditionalService\Search\Adapter;
  13. class Listing implements \Countable\Iterator
  14. {
  15.     protected ?ResultSet $resultSet null;
  16.     protected ?AbstractAdapter $adapter null;
  17.     protected ?Parameter $parameter null;
  18.     protected int $count 0;
  19.     protected ?array $objects null;
  20.     public function __construct(AbstractAdapter $adapter)
  21.     {
  22.         $this->adapter $adapter;
  23.     }
  24.     public function setAdapter(Local|Adapter|null $adapter): void
  25.     {
  26.         $this->adapter $adapter;
  27.     }
  28.     public function getAdapter(): Local|Adapter|null
  29.     {
  30.         return $this->adapter;
  31.     }
  32.     public function getParameter(): Parameter
  33.     {
  34.         if (!$this->parameter) {
  35.             throw new \Exception('Parameter not set.');
  36.         }
  37.         return $this->parameter;
  38.     }
  39.     public function setParameter(Parameter $parameter): void
  40.     {
  41.         $this->parameter $parameter;
  42.         $this->adapter->setParams($parameter);
  43.     }
  44.     public function load(): void
  45.     {
  46.         $result $this->adapter->search();
  47.         $this->count $result->count();
  48.         $this->resultSet $result;
  49.         $this->objects $result->getProducts();
  50.     }
  51.     public function count(): int
  52.     {
  53.         $this->checkLoaded();
  54.         return $this->count;
  55.     }
  56.     public function checkLoaded(): void
  57.     {
  58.         $this->adapter->setParams($this->getParameter());
  59.         if ($this->objects === null) {
  60.             $this->load();
  61.         }
  62.     }
  63.     public function getItems(?int $page, ?int $itemCountPerPage): ?array
  64.     {
  65.         $this->checkLoaded();
  66.         return $this->objects;
  67.     }
  68.     public function getPaginatorAdapter(): self
  69.     {
  70.         return $this;
  71.     }
  72.     public function rewind(): void
  73.     {
  74.         $this->checkLoaded();
  75.         reset($this->objects);
  76.     }
  77.     public function current(): mixed
  78.     {
  79.         $this->checkLoaded();
  80.         return current($this->objects);
  81.     }
  82.     public function key(): string|int|null
  83.     {
  84.         $this->checkLoaded();
  85.         return key($this->objects);
  86.     }
  87.     public function next(): mixed
  88.     {
  89.         $this->checkLoaded();
  90.         return next($this->objects);
  91.     }
  92.     public function valid(): bool
  93.     {
  94.         return $this->current() !== null;
  95.     }
  96.     public function flushObjects(): void
  97.     {
  98.         $this->objects null;
  99.     }
  100. }