vendor/sulu/sulu/src/Sulu/Bundle/TrashBundle/Domain/Model/TrashItem.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Sulu.
  5.  *
  6.  * (c) Sulu GmbH
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace Sulu\Bundle\TrashBundle\Domain\Model;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use JMS\Serializer\Annotation\ExclusionPolicy;
  15. use JMS\Serializer\Annotation\Expose;
  16. use JMS\Serializer\Annotation\Groups;
  17. use JMS\Serializer\Annotation\SerializedName;
  18. use JMS\Serializer\Annotation\VirtualProperty;
  19. use Sulu\Bundle\TrashBundle\Domain\Exception\TrashItemTranslationNotFoundException;
  20. use Sulu\Component\Security\Authentication\UserInterface;
  21. /**
  22.  * @ExclusionPolicy("all")
  23.  */
  24. class TrashItem implements TrashItemInterface
  25. {
  26.     /**
  27.      * @Expose
  28.      * @Groups({"trash_item_admin_api"})
  29.      *
  30.      * @var int
  31.      */
  32.     private $id;
  33.     /**
  34.      * @Expose
  35.      * @Groups({"trash_item_admin_api"})
  36.      *
  37.      * @var string
  38.      */
  39.     private $resourceKey;
  40.     /**
  41.      * @Expose
  42.      * @Groups({"trash_item_admin_api"})
  43.      *
  44.      * @var string
  45.      */
  46.     private $resourceId;
  47.     /**
  48.      * @Expose
  49.      * @Groups({"trash_item_admin_api"})
  50.      *
  51.      * @var mixed[]
  52.      */
  53.     private $restoreData = [];
  54.     /**
  55.      * The restoreType can be used to indicate a sub entity.
  56.      *     e.g.: Store and Restore a single translation of a page.
  57.      *          -> "translation".
  58.      *
  59.      * @Expose
  60.      * @Groups({"trash_item_admin_api"})
  61.      *
  62.      * @var string|null
  63.      */
  64.     private $restoreType;
  65.     /**
  66.      * The restoreOptions are used to change behaviour of store and restore handler.
  67.      *     e.g.: Store and Restore a single translation of a page.
  68.      *          -> ["locale" => "en"].
  69.      *
  70.      * @Expose
  71.      * @Groups({"trash_item_admin_api"})
  72.      *
  73.      * @var mixed[]
  74.      */
  75.     private $restoreOptions = [];
  76.     /**
  77.      * @Expose
  78.      * @Groups({"trash_item_admin_api"})
  79.      *
  80.      * @var string|null
  81.      */
  82.     private $resourceSecurityContext;
  83.     /**
  84.      * @Expose
  85.      *
  86.      * @var string|null
  87.      */
  88.     private $resourceSecurityObjectType;
  89.     /**
  90.      * @Expose
  91.      * @Groups({"trash_item_admin_api"})
  92.      *
  93.      * @var string|null
  94.      */
  95.     private $resourceSecurityObjectId;
  96.     /**
  97.      * @Expose
  98.      * @Groups({"trash_item_admin_api"})
  99.      *
  100.      * @var \DateTimeImmutable
  101.      */
  102.     private $storeTimestamp;
  103.     /**
  104.      * @var UserInterface|null
  105.      */
  106.     private $user;
  107.     /**
  108.      * @var Collection<int, TrashItemTranslation>
  109.      */
  110.     private $translations;
  111.     /**
  112.      * @var string|null
  113.      */
  114.     private $defaultLocale;
  115.     public function __construct()
  116.     {
  117.         $this->translations = new ArrayCollection();
  118.         $this->storeTimestamp = new \DateTimeImmutable();
  119.     }
  120.     public function getId(): ?int
  121.     {
  122.         return $this->id;
  123.     }
  124.     public function getResourceKey(): string
  125.     {
  126.         return $this->resourceKey;
  127.     }
  128.     public function setResourceKey(string $resourceKey): TrashItemInterface
  129.     {
  130.         $this->resourceKey $resourceKey;
  131.         return $this;
  132.     }
  133.     public function getResourceId(): string
  134.     {
  135.         return $this->resourceId;
  136.     }
  137.     public function setResourceId(string $resourceId): TrashItemInterface
  138.     {
  139.         $this->resourceId $resourceId;
  140.         return $this;
  141.     }
  142.     public function getRestoreData(): array
  143.     {
  144.         return $this->restoreData;
  145.     }
  146.     public function setRestoreData(array $restoreData): TrashItemInterface
  147.     {
  148.         $this->restoreData $restoreData;
  149.         return $this;
  150.     }
  151.     public function getRestoreType(): ?string
  152.     {
  153.         return $this->restoreType;
  154.     }
  155.     public function setRestoreType(?string $restoreType): TrashItemInterface
  156.     {
  157.         $this->restoreType $restoreType;
  158.         return $this;
  159.     }
  160.     public function getRestoreOptions(): array
  161.     {
  162.         return $this->restoreOptions;
  163.     }
  164.     public function setRestoreOptions(array $restoreOptions): TrashItemInterface
  165.     {
  166.         $this->restoreOptions $restoreOptions;
  167.         return $this;
  168.     }
  169.     public function getResourceTitle(?string $locale null): string
  170.     {
  171.         return $this->getTranslation($localetrue)->getTitle();
  172.     }
  173.     public function setResourceTitle(string $resourceTitle, ?string $locale null): TrashItemInterface
  174.     {
  175.         if (!$this->hasTranslation($locale)) {
  176.             $translation = new TrashItemTranslation($this$locale$resourceTitle);
  177.             $this->addTranslation($translation);
  178.             return $this;
  179.         }
  180.         $translation $this->getTranslation($localefalse);
  181.         $translation->setTitle($resourceTitle);
  182.         return $this;
  183.     }
  184.     public function getResourceSecurityContext(): ?string
  185.     {
  186.         return $this->resourceSecurityContext;
  187.     }
  188.     public function setResourceSecurityContext(?string $resourceSecurityContext): TrashItemInterface
  189.     {
  190.         $this->resourceSecurityContext $resourceSecurityContext;
  191.         return $this;
  192.     }
  193.     public function getResourceSecurityObjectType(): ?string
  194.     {
  195.         return $this->resourceSecurityObjectType;
  196.     }
  197.     public function setResourceSecurityObjectType(?string $resourceSecurityObjectType): TrashItemInterface
  198.     {
  199.         $this->resourceSecurityObjectType $resourceSecurityObjectType;
  200.         return $this;
  201.     }
  202.     public function getResourceSecurityObjectId(): ?string
  203.     {
  204.         return $this->resourceSecurityObjectId;
  205.     }
  206.     public function setResourceSecurityObjectId(?string $resourceSecurityObjectId): TrashItemInterface
  207.     {
  208.         $this->resourceSecurityObjectId $resourceSecurityObjectId;
  209.         return $this;
  210.     }
  211.     public function getStoreTimestamp(): \DateTimeImmutable
  212.     {
  213.         return $this->storeTimestamp;
  214.     }
  215.     public function setStoreTimestamp(\DateTimeImmutable $storeTimestamp): TrashItemInterface
  216.     {
  217.         $this->storeTimestamp $storeTimestamp;
  218.         return $this;
  219.     }
  220.     public function getUser(): ?UserInterface
  221.     {
  222.         return $this->user;
  223.     }
  224.     /**
  225.      * @VirtualProperty
  226.      * @SerializedName("userId")
  227.      * @Groups({"trash_item_api"})
  228.      */
  229.     public function getUserId(): ?int
  230.     {
  231.         return $this->user $this->user->getId() : null;
  232.     }
  233.     public function setUser(?UserInterface $user): TrashItemInterface
  234.     {
  235.         $this->user $user;
  236.         return $this;
  237.     }
  238.     public function getTranslation(?string $locale nullbool $fallback false): TrashItemTranslation
  239.     {
  240.         /** @var TrashItemTranslation|false $translation */
  241.         $translation $this->translations->filter(
  242.             function(TrashItemTranslation $translation) use ($locale) {
  243.                 return $translation->getLocale() === $locale;
  244.             }
  245.         )->first();
  246.         if (!$translation && $fallback) {
  247.             $translation $this->translations->filter(
  248.                 function(TrashItemTranslation $translation) {
  249.                     return $translation->getLocale() === $this->defaultLocale;
  250.                 }
  251.             )->first();
  252.         }
  253.         if (!$translation) {
  254.             throw new TrashItemTranslationNotFoundException($locale);
  255.         }
  256.         return $translation;
  257.     }
  258.     private function hasTranslation(?string $locale): bool
  259.     {
  260.         return !$this->translations->filter(
  261.             function(TrashItemTranslation $translation) use ($locale) {
  262.                 return $translation->getLocale() === $locale;
  263.             }
  264.         )->isEmpty();
  265.     }
  266.     private function addTranslation(TrashItemTranslation $translation): void
  267.     {
  268.         if (=== $this->translations->count()) {
  269.             $this->defaultLocale $translation->getLocale();
  270.         }
  271.         $this->translations->add($translation);
  272.     }
  273. }