src/App/Model/Content/Media/AbstractMedia.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Model\Content\Media;
  3. use App\Model\Content\Media;
  4. use ArrayAccess;
  5. use JsonSerializable;
  6. use Symfony\Component\HttpFoundation\ParameterBag;
  7. abstract class AbstractMedia implements JsonSerializableArrayAccess
  8. {
  9.     public const TYPE null;
  10.     protected const TYPES = [
  11.         Media\ImageLooseMedia::TYPE => Media\ImageLooseMedia::class,
  12.         Media\YoutubeVideoMedia::TYPE => Media\YoutubeVideoMedia::class,
  13.         Media\VimeoVideoMedia::TYPE => Media\VimeoVideoMedia::class,
  14.         Media\DocumentLooseMedia::TYPE => Media\DocumentLooseMedia::class,
  15.         Media\GoogleDocumentMedia::TYPE => Media\GoogleDocumentMedia::class,
  16.     ];
  17.     protected const IMAGE_TYPES = [
  18.         Media\ImageLooseMedia::TYPE,
  19.     ];
  20.     protected const VIDEO_TYPES = [
  21.         Media\YoutubeVideoMedia::TYPE,
  22.         Media\VimeoVideoMedia::TYPE,
  23.     ];
  24.     protected const AUDIO_TYPES = [];
  25.     protected const DOCUMENT_TYPES = [
  26.         Media\DocumentLooseMedia::TYPE,
  27.         Media\GoogleDocumentMedia::TYPE,
  28.     ];
  29.     public const DECORS__META 'meta';
  30.     public const DECORS__URLS 'urls';
  31.     public const DECORS__UPLOADS 'uploads';
  32.     protected const PARAMS__FEATURED 'featured';
  33.     protected const PARAMS__NOTE 'note';
  34.     protected const PARAMS__ALT 'alt';
  35.     // NOTE: these are carried over from old gallery module, currently unused...
  36.     protected const PARAMS__TITLE 'title';
  37.     protected const PARAMS__HIDDEN 'hidden';
  38.     /**
  39.      * @var string|null
  40.      */
  41.     protected ?string $id null;
  42.     /**
  43.      * @var ParameterBag
  44.      */
  45.     protected ParameterBag $params;
  46.     /**
  47.      * @var ParameterBag
  48.      */
  49.     protected ParameterBag $decors;
  50.     /**
  51.      * @param AbstractMedia|array $data
  52.      * @return self
  53.      */
  54.     public static function build($data): self
  55.     {
  56.         // if we already have an abstract media, return it; just a helper to make other logic less verbose
  57.         if ($data instanceof self) {
  58.             return $data;
  59.         }
  60.         // determine the type
  61.         $type $data['type'];
  62.         // determine the class and ensure it is legit
  63.         $class self::TYPES[$type];
  64.         if ( ! is_subclass_of($classself::class)) {
  65.             throw new \RuntimeException();
  66.         }
  67.         // build an object of the proper type
  68.         return new $class($data);
  69.     }
  70.     /**
  71.      * @param array|null $data
  72.      */
  73.     public function __construct(?array $data null)
  74.     {
  75.         // set up the basic params
  76.         $this->params = new ParameterBag([
  77.             self::PARAMS__FEATURED => false,
  78.             self::PARAMS__NOTE => null,
  79.             self::PARAMS__ALT => null,
  80.         ]);
  81.         // init the decorations
  82.         $this->decors = new ParameterBag();
  83.         // if there is data, handle it
  84.         if ($data) {
  85.             $this->parseArray($data);
  86.         }
  87.     }
  88.     /**
  89.      * @return array
  90.      * @throws \Exception
  91.      */
  92.     public function toArray(): array
  93.     {
  94.         $data = [
  95.             'type' => $this->getType(),
  96.             'id' => $this->getId(),
  97.             'params' => $this->getParams()->all(),
  98.         ];
  99.         foreach ($this->getDecorations()->keys() as $key) {
  100.             $data['_'.$key] = $this->getDecorations()->get($key);
  101.         }
  102.         return $data;
  103.     }
  104.     /**
  105.      * {@inheritDoc}
  106.      */
  107.     public function jsonSerialize(): array
  108.     {
  109.         return $this->toArray();
  110.     }
  111.     /**
  112.      * @return string
  113.      */
  114.     public function getType(): string
  115.     {
  116.         if ( ! static::TYPE) {
  117.             throw new \LogicException();
  118.         }
  119.         return static::TYPE;
  120.     }
  121.     /**
  122.      * @return bool
  123.      */
  124.     public function isImage(): bool
  125.     {
  126.         return in_array(
  127.             $this->getType(),
  128.             self::IMAGE_TYPES,
  129.             true,
  130.         );
  131.     }
  132.     /**
  133.      * @return bool
  134.      */
  135.     public function isVideo(): bool
  136.     {
  137.         return in_array(
  138.             $this->getType(),
  139.             self::VIDEO_TYPES,
  140.             true,
  141.         );
  142.     }
  143.     /**
  144.      * @return bool
  145.      */
  146.     public function isAudio(): bool
  147.     {
  148.         return in_array(
  149.             $this->getType(),
  150.             self::AUDIO_TYPES,
  151.             true,
  152.         );
  153.     }
  154.     /**
  155.      * @return bool
  156.      */
  157.     public function isDocument(): bool
  158.     {
  159.         return in_array(
  160.             $this->getType(),
  161.             self::DOCUMENT_TYPES,
  162.             true,
  163.         );
  164.     }
  165.     /**
  166.      * @return string|null
  167.      */
  168.     public function getId(): ?string
  169.     {
  170.         return $this->id;
  171.     }
  172.     /**
  173.      * @param string $id
  174.      * @return $this
  175.      */
  176.     public function setId(string $id): self
  177.     {
  178.         $this->id $id;
  179.         return $this;
  180.     }
  181.     /**
  182.      * @return ParameterBag
  183.      */
  184.     public function getParams(): ParameterBag
  185.     {
  186.         return $this->params;
  187.     }
  188.     /**
  189.      * @return ParameterBag
  190.      */
  191.     public function getDecorations(): ParameterBag
  192.     {
  193.         return $this->decors;
  194.     }
  195.     /**
  196.      * @param array $data
  197.      * @return void
  198.      */
  199.     protected function parseArray(array $data): void
  200.     {
  201.         // set the id value
  202.         $this->setId($data['id']);
  203.         // replace the params
  204.         $this->params->replace($data['params'] ?: []);
  205.         // need to clear the decorations
  206.         $this->decors->replace([]);
  207.     }
  208.     /**
  209.      * @return bool
  210.      */
  211.     public function isFeatured(): bool
  212.     {
  213.         return ($this->getParams()->getBoolean(self::PARAMS__FEATURED) === true);
  214.     }
  215.     /**
  216.      * @param bool $featured
  217.      * @return $this
  218.      */
  219.     public function setFeatured(bool $featured): self
  220.     {
  221.         $this->getParams()->set(self::PARAMS__FEATURED$featured);
  222.         return $this;
  223.     }
  224.     /**
  225.      * @return string|null
  226.      */
  227.     public function getNote(): ?string
  228.     {
  229.         return $this->getParams()->get(self::PARAMS__NOTE) ?: null;
  230.     }
  231.     /**
  232.      * @param string|null $note
  233.      * @return $this
  234.      */
  235.     public function setNote(?string $note): self
  236.     {
  237.         $this->getParams()->set(self::PARAMS__NOTEtrim($note) ?: null);
  238.         return $this;
  239.     }
  240.     /**
  241.      * @return string|null
  242.      */
  243.     public function getAlt(): ?string
  244.     {
  245.         return $this->getParams()->get(self::PARAMS__ALT) ?: null;
  246.     }
  247.     /**
  248.      * @param string|null $alt
  249.      * @return $this
  250.      */
  251.     public function setAlt(?string $alt): self
  252.     {
  253.         $this->getParams()->set(self::PARAMS__ALTtrim($alt) ?: null);
  254.         return $this;
  255.     }
  256.     /**
  257.      * @return string|null
  258.      */
  259.     public function getTitle(): ?string
  260.     {
  261.         return $this->getParams()->get(self::PARAMS__TITLE) ?: null;
  262.     }
  263.     /**
  264.      * @param string|null $title
  265.      * @return $this
  266.      */
  267.     public function setTitle(?string $title): self
  268.     {
  269.         $this->getParams()->set(self::PARAMS__TITLEtrim($title) ?: null);
  270.         return $this;
  271.     }
  272.     /**
  273.      * @return bool
  274.      */
  275.     public function isHidden(): bool
  276.     {
  277.         return $this->getParams()->get(self::PARAMS__HIDDEN) ?: false;
  278.     }
  279.     /**
  280.      * @param bool|null $hidden
  281.      * @return $this
  282.      */
  283.     public function setHidden(?bool $hidden): self
  284.     {
  285.         $this->getParams()->set(self::PARAMS__HIDDEN$hidden ?: false);
  286.         return $this;
  287.     }
  288.     /**
  289.      * @param string $name
  290.      * @return mixed
  291.      */
  292.     public function __get(string $name)
  293.     {
  294.         if (str_starts_with($name'_')) {
  295.             $name ltrim($name'_');
  296.             if ( ! $this->getDecorations()->has($name)) {
  297.                 throw new \RuntimeException();
  298.             }
  299.             return $this->getDecorations()->get($name);
  300.         }
  301.         throw new \RuntimeException();
  302.     }
  303.     /**
  304.      * @param string $name
  305.      * @return bool
  306.      */
  307.     public function __isset(string $name): bool
  308.     {
  309.         if (str_starts_with($name'_')) {
  310.             $name ltrim($name'_');
  311.             return $this->getDecorations()->has($name);
  312.         }
  313.         return false;
  314.     }
  315.     /**
  316.      * {@inheritDoc}
  317.      */
  318.     public function offsetExists($offset): bool
  319.     {
  320.         if (str_starts_with($offset'_')) {
  321.             return $this->decors->has($offset);
  322.         }
  323.         return $this->params->has($offset);
  324.     }
  325.     /**
  326.      * {@inheritDoc}
  327.      */
  328.     public function offsetGet($offset)
  329.     {
  330.         if (str_starts_with($offset'_')) {
  331.             return $this->decors->get($offset);
  332.         }
  333.         return $this->params->get($offset);
  334.     }
  335.     /**
  336.      * {@inheritDoc}
  337.      */
  338.     public function offsetSet($offset$value): void
  339.     {
  340.         if (str_starts_with($offset'_')) {
  341.             $this->decors->set($offset$value);
  342.         } else {
  343.             $this->params->set($offset$value);
  344.         }
  345.     }
  346.     /**
  347.      * {@inheritDoc}
  348.      */
  349.     public function offsetUnset($offset): void
  350.     {
  351.         if (str_starts_with($offset'_')) {
  352.             $this->decors->remove($offset);
  353.         } else {
  354.             $this->params->remove($offset);
  355.         }
  356.     }
  357. }