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

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