vendor/sulu/sulu/src/Sulu/Bundle/MediaBundle/Api/Media.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\MediaBundle\Api;
  11. use JMS\Serializer\Annotation\ExclusionPolicy;
  12. use JMS\Serializer\Annotation\Groups;
  13. use JMS\Serializer\Annotation\SerializedName;
  14. use JMS\Serializer\Annotation\VirtualProperty;
  15. use Sulu\Bundle\AudienceTargetingBundle\Entity\TargetGroupInterface;
  16. use Sulu\Bundle\CategoryBundle\Api\Category;
  17. use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface as CategoryEntity;
  18. use Sulu\Bundle\MediaBundle\Entity\File;
  19. use Sulu\Bundle\MediaBundle\Entity\FileVersion;
  20. use Sulu\Bundle\MediaBundle\Entity\FileVersionContentLanguage;
  21. use Sulu\Bundle\MediaBundle\Entity\FileVersionMeta;
  22. use Sulu\Bundle\MediaBundle\Entity\FileVersionPublishLanguage;
  23. use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
  24. use Sulu\Bundle\MediaBundle\Entity\MediaType;
  25. use Sulu\Bundle\MediaBundle\Media\Exception\FileNotFoundException;
  26. use Sulu\Bundle\MediaBundle\Media\Exception\FileVersionNotFoundException;
  27. use Sulu\Bundle\TagBundle\Tag\TagInterface;
  28. use Sulu\Component\Rest\ApiWrapper;
  29. use Sulu\Component\Security\Authentication\UserInterface;
  30. /**
  31.  * Class Media
  32.  * The Media RestObject is the api entity for the MediaController.
  33.  *
  34.  * @ExclusionPolicy("all")
  35.  */
  36. class Media extends ApiWrapper
  37. {
  38.     /**
  39.      * @var string
  40.      */
  41.     const MEDIA_TYPE_IMAGE 'image';
  42.     /**
  43.      * @var string
  44.      */
  45.     const MEDIA_TYPE_VIDEO 'video';
  46.     /**
  47.      * @var string
  48.      */
  49.     const MEDIA_TYPE_AUDIO 'audio';
  50.     /**
  51.      * @var string
  52.      */
  53.     const MEDIA_TYPE_DOCUMENT 'document';
  54.     /**
  55.      * @var string
  56.      */
  57.     protected $url;
  58.     /**
  59.      * @var string|null
  60.      */
  61.     protected $adminUrl;
  62.     /**
  63.      * @var array
  64.      */
  65.     protected $formats = [];
  66.     /**
  67.      * @var string
  68.      */
  69.     protected $locale;
  70.     /**
  71.      * @var int
  72.      */
  73.     protected $version;
  74.     /**
  75.      * @var array
  76.      */
  77.     protected $additionalVersionData = [];
  78.     /**
  79.      * @var FileVersion
  80.      */
  81.     protected $fileVersion null;
  82.     /**
  83.      * @var FileVersionMeta
  84.      */
  85.     protected $localizedMeta null;
  86.     /**
  87.      * @var File
  88.      */
  89.     protected $file null;
  90.     public function __construct(MediaInterface $media$locale$version null)
  91.     {
  92.         $this->entity $media;
  93.         $this->locale $locale;
  94.         $this->version $version;
  95.     }
  96.     /**
  97.      * @VirtualProperty
  98.      * @SerializedName("id")
  99.      * @Groups({"partialMedia", "Default"})
  100.      *
  101.      * @return int
  102.      */
  103.     public function getId()
  104.     {
  105.         return $this->entity->getId();
  106.     }
  107.     /**
  108.      * @VirtualProperty
  109.      * @SerializedName("locale")
  110.      *
  111.      * @return string
  112.      */
  113.     public function getLocale()
  114.     {
  115.         return $this->locale;
  116.     }
  117.     /**
  118.      * @VirtualProperty
  119.      *
  120.      * @return string
  121.      */
  122.     public function getFallbackLocale()
  123.     {
  124.         if (!$this->getLocalizedMeta()) {
  125.             return;
  126.         }
  127.         $fallbackLocale $this->getLocalizedMeta()->getLocale();
  128.         return $fallbackLocale !== $this->locale $fallbackLocale null;
  129.     }
  130.     /**
  131.      * @param Collection $collection
  132.      *
  133.      * @return $this
  134.      */
  135.     public function setCollection($collection)
  136.     {
  137.         $this->entity->setCollection($collection);
  138.         return $this;
  139.     }
  140.     /**
  141.      * @VirtualProperty
  142.      * @SerializedName("collection")
  143.      *
  144.      * @return int
  145.      */
  146.     public function getCollection()
  147.     {
  148.         $collection $this->entity->getCollection();
  149.         if ($collection) {
  150.             return $collection->getId();
  151.         }
  152.         return;
  153.     }
  154.     /**
  155.      * @param int $size
  156.      *
  157.      * @return $this
  158.      */
  159.     public function setSize($size)
  160.     {
  161.         $this->getFileVersion()->setSize($size);
  162.         return $this;
  163.     }
  164.     /**
  165.      * @VirtualProperty
  166.      * @SerializedName("size")
  167.      *
  168.      * @return int
  169.      */
  170.     public function getSize()
  171.     {
  172.         return $this->getFileVersion()->getSize();
  173.     }
  174.     /**
  175.      * @param string $mimeType
  176.      *
  177.      * @return $this
  178.      */
  179.     public function setMimeType($mimeType)
  180.     {
  181.         $this->getFileVersion()->setMimeType($mimeType);
  182.         return $this;
  183.     }
  184.     /**
  185.      * @VirtualProperty
  186.      * @SerializedName("mimeType")
  187.      *
  188.      * @return string
  189.      */
  190.     public function getMimeType()
  191.     {
  192.         return $this->getFileVersion()->getMimeType();
  193.     }
  194.     /**
  195.      * @return string|null
  196.      */
  197.     public function getExtension()
  198.     {
  199.         return $this->getFileVersion()->getExtension();
  200.     }
  201.     /**
  202.      * @param string $title
  203.      *
  204.      * @return $this
  205.      */
  206.     public function setTitle($title)
  207.     {
  208.         $this->getMeta(true)->setTitle($title);
  209.         return $this;
  210.     }
  211.     /**
  212.      * @VirtualProperty
  213.      * @SerializedName("title")
  214.      *
  215.      * @return string|null
  216.      */
  217.     public function getTitle()
  218.     {
  219.         if (!$this->getLocalizedMeta()) {
  220.             return null;
  221.         }
  222.         return $this->getLocalizedMeta()->getTitle();
  223.     }
  224.     /**
  225.      * @param string|null $description
  226.      *
  227.      * @return $this
  228.      */
  229.     public function setDescription($description)
  230.     {
  231.         $this->getMeta(true)->setDescription($description);
  232.         return $this;
  233.     }
  234.     /**
  235.      * @VirtualProperty
  236.      * @SerializedName("description")
  237.      *
  238.      * @return string|null
  239.      */
  240.     public function getDescription()
  241.     {
  242.         if (!$this->getLocalizedMeta()) {
  243.             return null;
  244.         }
  245.         return $this->getLocalizedMeta()->getDescription();
  246.     }
  247.     /**
  248.      * @param string|null $copyright
  249.      *
  250.      * @return $this
  251.      */
  252.     public function setCopyright($copyright)
  253.     {
  254.         $this->getMeta(true)->setCopyright($copyright);
  255.         return $this;
  256.     }
  257.     /**
  258.      * Returns copyright for media.
  259.      *
  260.      * @VirtualProperty
  261.      * @SerializedName("copyright")
  262.      *
  263.      * @return string|null
  264.      *
  265.      * @throws FileVersionNotFoundException
  266.      */
  267.     public function getCopyright()
  268.     {
  269.         if (!$this->getLocalizedMeta()) {
  270.             return null;
  271.         }
  272.         return $this->getLocalizedMeta()->getCopyright();
  273.     }
  274.     /**
  275.      * @param string|null $credits
  276.      *
  277.      * @return $this
  278.      */
  279.     public function setCredits($credits)
  280.     {
  281.         $this->getMeta(true)->setCredits($credits);
  282.         return $this;
  283.     }
  284.     /**
  285.      * Returns copyright for media.
  286.      *
  287.      * @VirtualProperty
  288.      * @SerializedName("credits")
  289.      *
  290.      * @return string|null
  291.      *
  292.      * @throws FileVersionNotFoundException
  293.      */
  294.     public function getCredits()
  295.     {
  296.         if (!$this->getLocalizedMeta()) {
  297.             return null;
  298.         }
  299.         return $this->getLocalizedMeta()->getCredits();
  300.     }
  301.     /**
  302.      * @param int $version
  303.      *
  304.      * @return $this
  305.      */
  306.     public function setVersion($version)
  307.     {
  308.         $this->version $version;
  309.         return $this;
  310.     }
  311.     /**
  312.      * @VirtualProperty
  313.      * @SerializedName("version")
  314.      *
  315.      * @return int
  316.      */
  317.     public function getVersion()
  318.     {
  319.         return $this->getFileVersion()->getVersion();
  320.     }
  321.     /**
  322.      * @VirtualProperty
  323.      * @SerializedName("subVersion")
  324.      *
  325.      * @return int
  326.      */
  327.     public function getSubVersion()
  328.     {
  329.         return $this->getFileVersion()->getSubVersion();
  330.     }
  331.     /**
  332.      * @return array
  333.      */
  334.     public function getAdditionalVersionData()
  335.     {
  336.         return $this->additionalVersionData;
  337.     }
  338.     /**
  339.      * @param array $additionalVersionData
  340.      *
  341.      * @return $this
  342.      */
  343.     public function setAdditionalVersionData($additionalVersionData)
  344.     {
  345.         $this->additionalVersionData $additionalVersionData;
  346.         return $this;
  347.     }
  348.     /**
  349.      * @VirtualProperty
  350.      * @SerializedName("versions")
  351.      *
  352.      * @return array
  353.      */
  354.     public function getVersions()
  355.     {
  356.         $versions = [];
  357.         $file $this->getFile();
  358.         /** @var FileVersion $fileVersion */
  359.         foreach ($file->getFileVersions() as $fileVersion) {
  360.             $versionData = [];
  361.             if (isset($this->additionalVersionData[$fileVersion->getVersion()])) {
  362.                 $versionData $this->additionalVersionData[$fileVersion->getVersion()];
  363.             }
  364.             $versionData['version'] = $fileVersion->getVersion();
  365.             $versionData['name'] = $fileVersion->getName();
  366.             $versionData['created'] = $fileVersion->getCreated();
  367.             $versionData['changed'] = $fileVersion->getChanged();
  368.             $versionData['active'] = $fileVersion->isActive();
  369.             $versions[$fileVersion->getVersion()] = $versionData;
  370.         }
  371.         return $versions;
  372.     }
  373.     /**
  374.      * @param int $name
  375.      *
  376.      * @return $this
  377.      */
  378.     public function setName($name)
  379.     {
  380.         $this->getFileVersion()->setName($name);
  381.         return $this;
  382.     }
  383.     /**
  384.      * @VirtualProperty
  385.      * @SerializedName("name")
  386.      *
  387.      * @return int
  388.      */
  389.     public function getName()
  390.     {
  391.         return $this->getFileVersion()->getName();
  392.     }
  393.     /**
  394.      * @VirtualProperty
  395.      * @SerializedName("type")
  396.      *
  397.      * @return MediaType
  398.      */
  399.     public function getType()
  400.     {
  401.         return $this->entity->getType();
  402.     }
  403.     /**
  404.      * @param MediaType $type
  405.      *
  406.      * @return $this
  407.      */
  408.     public function setType($type)
  409.     {
  410.         $this->entity->setType($type);
  411.         return $this;
  412.     }
  413.     /**
  414.      * @param string $type
  415.      *
  416.      * @return bool
  417.      */
  418.     public function isTypeOf($type)
  419.     {
  420.         return $this->getType()->getName() == $type;
  421.     }
  422.     /**
  423.      * @VirtualProperty
  424.      * @SerializedName("isImage")
  425.      *
  426.      * @return bool
  427.      */
  428.     public function isImage()
  429.     {
  430.         return $this->isTypeOf(self::MEDIA_TYPE_IMAGE);
  431.     }
  432.     /**
  433.      * @VirtualProperty
  434.      * @SerializedName("isVideo")
  435.      *
  436.      * @return bool
  437.      */
  438.     public function isVideo()
  439.     {
  440.         return $this->isTypeOf(self::MEDIA_TYPE_VIDEO);
  441.     }
  442.     /**
  443.      * @VirtualProperty
  444.      * @SerializedName("isAudio")
  445.      *
  446.      * @return bool
  447.      */
  448.     public function isAudio()
  449.     {
  450.         return $this->isTypeOf(self::MEDIA_TYPE_AUDIO);
  451.     }
  452.     /**
  453.      * @VirtualProperty
  454.      * @SerializedName("isDocument")
  455.      *
  456.      * @return bool
  457.      */
  458.     public function isDocument()
  459.     {
  460.         return $this->isTypeOf(self::MEDIA_TYPE_DOCUMENT);
  461.     }
  462.     /**
  463.      * @VirtualProperty
  464.      * @SerializedName("storageOptions")
  465.      *
  466.      * @return array
  467.      */
  468.     public function getStorageOptions()
  469.     {
  470.         return $this->getFileVersion()->getStorageOptions();
  471.     }
  472.     /**
  473.      * @param array $storageOptions
  474.      *
  475.      * @return $this
  476.      */
  477.     public function setStorageOptions($storageOptions)
  478.     {
  479.         $this->getFileVersion()->setStorageOptions($storageOptions);
  480.         return $this;
  481.     }
  482.     /**
  483.      * @param array $publishLanguages
  484.      *
  485.      * @return $this
  486.      */
  487.     public function setPublishLanguages($publishLanguages)
  488.     {
  489.         $fileVersion $this->getFileVersion();
  490.         foreach ($publishLanguages as $locale) {
  491.             $publishLanguage = new FileVersionPublishLanguage();
  492.             $publishLanguage->setFileVersion($fileVersion);
  493.             $publishLanguage->setLocale($locale);
  494.             if (!$fileVersion->getPublishLanguages()->contains($publishLanguage)) {
  495.                 $fileVersion->addPublishLanguage($publishLanguage);
  496.             }
  497.         }
  498.         return $this;
  499.     }
  500.     /**
  501.      * @VirtualProperty
  502.      * @SerializedName("publishLanguages")
  503.      *
  504.      * @return array
  505.      */
  506.     public function getPublishLanguages()
  507.     {
  508.         $publishLanguages = [];
  509.         /** @var FileVersionPublishLanguage $publishLanguage */
  510.         foreach ($this->getFileVersion()->getPublishLanguages() as $publishLanguage) {
  511.             $publishLanguages[] = $publishLanguage->getLocale();
  512.         }
  513.         return $publishLanguages;
  514.     }
  515.     /**
  516.      * @param array $contentLanguages
  517.      *
  518.      * @return $this
  519.      */
  520.     public function setContentLanguages($contentLanguages)
  521.     {
  522.         $fileVersion $this->getFileVersion();
  523.         foreach ($contentLanguages as $locale) {
  524.             $contentLanguage = new FileVersionContentLanguage();
  525.             $contentLanguage->setFileVersion($fileVersion);
  526.             $contentLanguage->setLocale($locale);
  527.             if (!$fileVersion->getContentLanguages()->contains($contentLanguage)) {
  528.                 $fileVersion->addContentLanguage($contentLanguage);
  529.             }
  530.         }
  531.         return $this;
  532.     }
  533.     /**
  534.      * @VirtualProperty
  535.      * @SerializedName("contentLanguages")
  536.      *
  537.      * @return array
  538.      */
  539.     public function getContentLanguages()
  540.     {
  541.         $contentLanguages = [];
  542.         /** @var FileVersionContentLanguage $contentLanguage */
  543.         foreach ($this->getFileVersion()->getContentLanguages() as $contentLanguage) {
  544.             $contentLanguages[] = $contentLanguage->getLocale();
  545.         }
  546.         return $contentLanguages;
  547.     }
  548.     /**
  549.      * @return $this
  550.      */
  551.     public function removeTags()
  552.     {
  553.         $fileVersion $this->getFileVersion();
  554.         $fileVersion->removeTags();
  555.         return $this;
  556.     }
  557.     /**
  558.      * @return $this
  559.      */
  560.     public function addTag(TagInterface $tagEntity)
  561.     {
  562.         $fileVersion $this->getFileVersion();
  563.         if (!$fileVersion->getTags()->contains($tagEntity)) {
  564.             $fileVersion->addTag($tagEntity);
  565.         }
  566.         return $this;
  567.     }
  568.     /**
  569.      * @VirtualProperty
  570.      * @SerializedName("tags")
  571.      *
  572.      * @return string[]
  573.      */
  574.     public function getTags()
  575.     {
  576.         $tags = [];
  577.         foreach ($this->getFileVersion()->getTags() as $tag) {
  578.             /* @var TagInterface $tag */
  579.             \array_push($tags$tag->getName());
  580.         }
  581.         return $tags;
  582.     }
  583.     /**
  584.      * @SerializedName("thumbnails")
  585.      *
  586.      * @return array
  587.      */
  588.     public function getFormats()
  589.     {
  590.         return $this->formats;
  591.     }
  592.     /**
  593.      * @VirtualProperty
  594.      * @SerializedName("thumbnails")
  595.      *
  596.      * @return array
  597.      */
  598.     public function getThumbnails() // FIXME change to getPreviews when SerializedName working
  599.     {
  600.         return $this->formats;
  601.     }
  602.     /**
  603.      * @param array $formats
  604.      */
  605.     public function setFormats($formats)
  606.     {
  607.         $this->formats $formats;
  608.     }
  609.     /**
  610.      * @VirtualProperty
  611.      * @SerializedName("url")
  612.      *
  613.      * @return string
  614.      */
  615.     public function getUrl()
  616.     {
  617.         return $this->url;
  618.     }
  619.     /**
  620.      * @param string $url
  621.      */
  622.     public function setUrl($url)
  623.     {
  624.         $this->url $url;
  625.     }
  626.     /**
  627.      * @VirtualProperty
  628.      * @SerializedName("adminUrl")
  629.      *
  630.      * @return string
  631.      */
  632.     public function getAdminUrl()
  633.     {
  634.         // if the admin url is not set fallback to the website url for backward compatibility
  635.         if (!$this->adminUrl) {
  636.             return $this->url;
  637.         }
  638.         return $this->adminUrl;
  639.     }
  640.     /**
  641.      * @param string $adminUrl
  642.      */
  643.     public function setAdminUrl($adminUrl)
  644.     {
  645.         $this->adminUrl $adminUrl;
  646.     }
  647.     /**
  648.      * @param \DateTime|string $changed
  649.      *
  650.      * @return $this
  651.      */
  652.     public function setChanged($changed)
  653.     {
  654.         if (\is_string($changed)) {
  655.             $changed = new \DateTime($changed);
  656.         }
  657.         $this->getFileVersion()->setChanged($changed);
  658.         return $this;
  659.     }
  660.     /**
  661.      * @VirtualProperty
  662.      * @SerializedName("changed")
  663.      *
  664.      * @return \DateTime
  665.      */
  666.     public function getChanged()
  667.     {
  668.         return $this->getFileVersion()->getChanged();
  669.     }
  670.     /**
  671.      * @param UserInterface|null $changer
  672.      *
  673.      * @return $this
  674.      */
  675.     public function setChanger($changer)
  676.     {
  677.         $this->entity->setChanger($changer);
  678.         return $this;
  679.     }
  680.     /**
  681.      * @VirtualProperty
  682.      * @SerializedName("changer")
  683.      *
  684.      * @return string|null
  685.      */
  686.     public function getChanger()
  687.     {
  688.         $user $this->getFileVersion()->getChanger();
  689.         if ($user) {
  690.             return $user->getFullName();
  691.         }
  692.         return null;
  693.     }
  694.     /**
  695.      * @VirtualProperty
  696.      * @SerializedName("created")
  697.      *
  698.      * @return \DateTime
  699.      */
  700.     public function getCreated()
  701.     {
  702.         return $this->entity->getCreated();
  703.     }
  704.     /**
  705.      * @param UserInterface|null $creator
  706.      *
  707.      * @return $this
  708.      */
  709.     public function setCreator($creator)
  710.     {
  711.         $this->entity->setCreator($creator);
  712.         return $this;
  713.     }
  714.     /**
  715.      * @VirtualProperty
  716.      * @SerializedName("creator")
  717.      *
  718.      * @return string|null
  719.      */
  720.     public function getCreator()
  721.     {
  722.         $user $this->getFileVersion()->getCreator();
  723.         if ($user) {
  724.             return $user->getFullName();
  725.         }
  726.         return null;
  727.     }
  728.     /**
  729.      * @param array $properties
  730.      *
  731.      * @return $this
  732.      */
  733.     public function setProperties($properties)
  734.     {
  735.         $this->getFileVersion()->setProperties($properties);
  736.         return $this;
  737.     }
  738.     /**
  739.      * @VirtualProperty
  740.      * @SerializedName("properties")
  741.      *
  742.      * @return array
  743.      */
  744.     public function getProperties()
  745.     {
  746.         return $this->getFileVersion()->getProperties();
  747.     }
  748.     /**
  749.      * @VirtualProperty
  750.      * @SerializedName("downloadCounter")
  751.      *
  752.      * @return int
  753.      */
  754.     public function getDownloadCounter()
  755.     {
  756.         $downloadCounter 0;
  757.         foreach ($this->getEntity()->getFiles() as $file) {
  758.             /** @var FileVersion $fileVersion */
  759.             foreach ($file->getFileVersions() as $fileVersion) {
  760.                 $downloadCounter += \intval($fileVersion->getDownloadCounter());
  761.             }
  762.         }
  763.         return $downloadCounter;
  764.     }
  765.     /**
  766.      * @return FileVersion
  767.      *
  768.      * @throws \Sulu\Bundle\MediaBundle\Media\Exception\FileVersionNotFoundException
  769.      */
  770.     public function getFileVersion()
  771.     {
  772.         if (null !== $this->fileVersion) {
  773.             return $this->fileVersion;
  774.         }
  775.         /** @var File $file */
  776.         foreach ($this->entity->getFiles() as $file) {
  777.             if (null !== $this->version) {
  778.                 $version $this->version;
  779.             } else {
  780.                 $version $file->getVersion();
  781.             }
  782.             if ($fileVersion $file->getFileVersion($version)) {
  783.                 $this->fileVersion $fileVersion;
  784.                 return $fileVersion;
  785.             }
  786.             break; // currently only one file per media exists
  787.         }
  788.         throw new FileVersionNotFoundException($this->entity->getId(), $this->version);
  789.     }
  790.     /**
  791.      * @return File
  792.      *
  793.      * @throws \Sulu\Bundle\MediaBundle\Media\Exception\FileNotFoundException
  794.      */
  795.     public function getFile()
  796.     {
  797.         if (null !== $this->file) {
  798.             return $this->file;
  799.         }
  800.         /** @var File $file */
  801.         foreach ($this->entity->getFiles() as $file) {
  802.             // currently only one file per media exists
  803.             $this->file $file;
  804.             return $this->file;
  805.         }
  806.         throw new FileNotFoundException($this->entity->getId());
  807.     }
  808.     /**
  809.      * @param bool $create
  810.      *
  811.      * @return FileVersionMeta
  812.      */
  813.     private function getMeta($create false)
  814.     {
  815.         $locale $this->locale;
  816.         $metaCollection $this->getFileVersion()->getMeta();
  817.         // get meta only with this locale
  818.         $metaCollectionFiltered $metaCollection->filter(function($meta) use ($locale) {
  819.             /** @var FileVersionMeta $meta */
  820.             if ($meta->getLocale() == $locale) {
  821.                 return true;
  822.             }
  823.             return false;
  824.         });
  825.         // check if meta was found
  826.         if ($metaCollectionFiltered->isEmpty()) {
  827.             if ($create) {
  828.                 // create when not found
  829.                 $meta = new FileVersionMeta();
  830.                 $meta->setLocale($this->locale);
  831.                 $meta->setFileVersion($this->getFileVersion());
  832.                 $this->getFileVersion()->addMeta($meta);
  833.                 return $meta;
  834.             }
  835.             // return first when create false
  836.             return $this->getFileVersion()->getDefaultMeta();
  837.         }
  838.         // return exists
  839.         return $metaCollectionFiltered->first();
  840.     }
  841.     /**
  842.      * Searches the meta for the file version in the media locale. Might also return a fallback.
  843.      *
  844.      * @return FileVersionMeta
  845.      *
  846.      * @throws FileVersionNotFoundException
  847.      */
  848.     private function getLocalizedMeta()
  849.     {
  850.         if ($this->localizedMeta) {
  851.             return $this->localizedMeta;
  852.         }
  853.         $metas $this->getFileVersion()->getMeta();
  854.         $this->localizedMeta $metas[0];
  855.         foreach ($metas as $key => $meta) {
  856.             if ($meta->getLocale() == $this->locale) {
  857.                 $this->localizedMeta $meta;
  858.                 break;
  859.             }
  860.         }
  861.         return $this->localizedMeta;
  862.     }
  863.     /**
  864.      * Adds a category to the entity.
  865.      */
  866.     public function addCategory(CategoryEntity $category)
  867.     {
  868.         $fileVersion $this->getFileVersion();
  869.         $fileVersion->addCategory($category);
  870.         return $this;
  871.     }
  872.     /**
  873.      * Removes all category from the entity.
  874.      *
  875.      * @return self
  876.      */
  877.     public function removeCategories()
  878.     {
  879.         $fileVersion $this->getFileVersion();
  880.         $fileVersion->removeCategories();
  881.         return $this;
  882.     }
  883.     /**
  884.      * Returns the categories of the media.
  885.      *
  886.      * @VirtualProperty
  887.      * @SerializedName("categories")
  888.      *
  889.      * @return int[]
  890.      */
  891.     public function getCategories()
  892.     {
  893.         $apiCategories = [];
  894.         $fileVersion $this->getFileVersion();
  895.         $categories $fileVersion->getCategories();
  896.         return \array_map(function(CategoryEntity $category) {
  897.             return $category->getId();
  898.         }, $categories->toArray());
  899.     }
  900.     /**
  901.      * Adds a target group to the entity.
  902.      *
  903.      * @return self
  904.      */
  905.     public function addTargetGroup(TargetGroupInterface $targetGroup)
  906.     {
  907.         $fileVersion $this->getFileVersion();
  908.         $fileVersion->addTargetGroup($targetGroup);
  909.         return $this;
  910.     }
  911.     /**
  912.      * Removes all target groups from the entities.
  913.      *
  914.      * @return self
  915.      */
  916.     public function removeTargetGroups()
  917.     {
  918.         $fileVersion $this->getFileVersion();
  919.         $fileVersion->removeTargetGroups();
  920.         return $this;
  921.     }
  922.     /**
  923.      * Returns the target groups of the media.
  924.      *
  925.      * @VirtualProperty
  926.      * @SerializedName("targetGroups")
  927.      * @Groups({"fullMediaAudienceTargeting"})
  928.      *
  929.      * @return int[]
  930.      */
  931.     public function getTargetGroups()
  932.     {
  933.         if (!$this->getFileVersion()->getTargetGroups()) {
  934.             return [];
  935.         }
  936.         return \array_map(function(TargetGroupInterface $targetGroup) {
  937.             return $targetGroup->getId();
  938.         }, $this->getFileVersion()->getTargetGroups()->toArray());
  939.     }
  940.     /**
  941.      * Returns the x coordinate of the focus point.
  942.      *
  943.      * @VirtualProperty
  944.      * @SerializedName("focusPointX")
  945.      *
  946.      * @return int
  947.      */
  948.     public function getFocusPointX()
  949.     {
  950.         return $this->getFileVersion()->getFocusPointX();
  951.     }
  952.     /**
  953.      * Sets the x coordinate of the focus point.
  954.      *
  955.      * @param int $focusPointX
  956.      */
  957.     public function setFocusPointX($focusPointX)
  958.     {
  959.         $this->getFileVersion()->setFocusPointX($focusPointX);
  960.     }
  961.     /**
  962.      * Returns the y coordinate of the focus point.
  963.      *
  964.      * @VirtualProperty
  965.      * @SerializedName("focusPointY")
  966.      *
  967.      * @return int
  968.      */
  969.     public function getFocusPointY()
  970.     {
  971.         return $this->getFileVersion()->getFocusPointY();
  972.     }
  973.     /**
  974.      * Sets the y coordinate of the focus point.
  975.      *
  976.      * @param int $focusPointY
  977.      */
  978.     public function setFocusPointY($focusPointY)
  979.     {
  980.         $this->getFileVersion()->setFocusPointY($focusPointY);
  981.     }
  982.     /**
  983.      * @VirtualProperty
  984.      * @SerializedName("previewImageId")
  985.      *
  986.      * @return ?int
  987.      */
  988.     public function getPreviewImageId()
  989.     {
  990.         $previewImage $this->entity->getPreviewImage();
  991.         if (!$previewImage) {
  992.             return null;
  993.         }
  994.         return $previewImage->getId();
  995.     }
  996. }