src/App/Model/Content/Media/MediaCollection.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Model\Content\Media;
  3. /**
  4.  *
  5.  */
  6. final class MediaCollection
  7.     implements
  8.         \ArrayAccess,
  9.         \Countable,
  10.         \Iterator,
  11.         \JsonSerializable
  12. {
  13.     /**
  14.      * @var array|AbstractMedia[]
  15.      */
  16.     protected array $elements;
  17.     /**
  18.      * @param array $elements
  19.      */
  20.     public function __construct(array $elements = [])
  21.     {
  22.         $this->elements array_map(
  23.             function ($element) {
  24.                 return AbstractMedia::build($element);
  25.             },
  26.             $elements
  27.         );
  28.     }
  29.     /**
  30.      * @return AbstractMedia|null
  31.      */
  32.     public function getFeature(): ?AbstractMedia
  33.     {
  34.         if ( ! $this->elements) {
  35.             return null;
  36.         }
  37.         foreach ($this->elements as $element) {
  38.             if ($element->isFeatured()) {
  39.                 return $element;
  40.             }
  41.         }
  42.         return $this->elements[0];
  43.     }
  44.     /**
  45.      * @param int $max
  46.      * @return array|AbstractMedia[]
  47.      */
  48.     public function getFeatures(int $max PHP_INT_MAX): array
  49.     {
  50.         $max max(1$max);
  51.         if ( ! $this->elements) {
  52.             return [];
  53.         }
  54.         if (($first $this->getFeature()) === $this->elements[0]) {
  55.             return array_slice($this->elements0$max);
  56.         }
  57.         $features = [
  58.             $first,
  59.         ];
  60.         foreach ($this->elements as $element) {
  61.             if (count($features) >= $max) {
  62.                 break;
  63.             }
  64.             if ($element !== $first) {
  65.                 $features[] = $element;
  66.             }
  67.         }
  68.         return $features;
  69.     }
  70.     /**
  71.      * @return array|AbstractMedia[]
  72.      */
  73.     public function toArray(): array
  74.     {
  75.         return $this->elements;
  76.     }
  77.     /**
  78.      * @return array
  79.      */
  80.     public function jsonSerialize(): array
  81.     {
  82.         return array_map(
  83.             function (AbstractMedia $element) {
  84.                 return $element->jsonSerialize();
  85.             },
  86.             array_values($this->elements)
  87.         );
  88.     }
  89.     /**
  90.      * {@inheritDoc}
  91.      */
  92.     public function offsetExists($offset): bool
  93.     {
  94.         return array_key_exists($offset$this->elements);
  95.     }
  96.     /**
  97.      * {@inheritDoc}
  98.      */
  99.     public function offsetGet($offset)
  100.     {
  101.         return $this->elements[$offset];
  102.     }
  103.     /**
  104.      * {@inheritDoc}
  105.      */
  106.     public function offsetSet($offset$value)
  107.     {
  108.         $this->elements[$offset] = ( ! $value instanceof AbstractMedia)
  109.             ? AbstractMedia::build($value)
  110.             : $value;
  111.     }
  112.     /**
  113.      * {@inheritDoc}
  114.      */
  115.     public function offsetUnset($offset)
  116.     {
  117.         unset($this->elements[$offset]);
  118.     }
  119.     /**
  120.      * {@inheritDoc}
  121.      */
  122.     public function count(): int
  123.     {
  124.         return count($this->elements);
  125.     }
  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     public function current(): AbstractMedia
  130.     {
  131.         return current($this->elements);
  132.     }
  133.     /**
  134.      * {@inheritDoc}
  135.      */
  136.     public function next(): void
  137.     {
  138.         next($this->elements);
  139.     }
  140.     /**
  141.      * {@inheritDoc}
  142.      */
  143.     public function key(): int
  144.     {
  145.         return key($this->elements);
  146.     }
  147.     /**
  148.      * {@inheritDoc}
  149.      */
  150.     public function valid(): bool
  151.     {
  152.         return isset($this->elements[key($this->elements)]);
  153.     }
  154.     /**
  155.      * {@inheritDoc}
  156.      */
  157.     public function rewind(): void
  158.     {
  159.         reset($this->elements);
  160.     }
  161.     /**
  162.      * @return AbstractMedia|null
  163.      */
  164.     public function first(): ?AbstractMedia
  165.     {
  166.         if ($this->elements) {
  167.             return $this->elements[0];
  168.         }
  169.         return null;
  170.     }
  171.     /**
  172.      * @param AbstractMedia $media
  173.      * @return $this
  174.      */
  175.     public function append(AbstractMedia $media): self
  176.     {
  177.         $this->elements[] = $media;
  178.         return $this;
  179.     }
  180. }