Reference.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Collaboration\Reference;
  8. /**
  9. * @since 25.0.0
  10. * @psalm-type OpenGraphObject = array{id: string, name: string, description: ?string, thumb: ?string, link: string}
  11. */
  12. class Reference implements IReference {
  13. protected string $reference;
  14. protected bool $accessible = true;
  15. protected ?string $title = null;
  16. protected ?string $description = null;
  17. protected ?string $imageUrl = null;
  18. protected ?string $contentType = null;
  19. protected ?string $url = null;
  20. protected ?string $richObjectType = null;
  21. protected ?array $richObject = null;
  22. /**
  23. * @since 25.0.0
  24. */
  25. public function __construct(string $reference) {
  26. $this->reference = $reference;
  27. }
  28. /**
  29. * @inheritdoc
  30. * @since 25.0.0
  31. */
  32. public function getId(): string {
  33. return $this->reference;
  34. }
  35. /**
  36. * @inheritdoc
  37. * @since 25.0.0
  38. */
  39. public function setAccessible(bool $accessible): void {
  40. $this->accessible = $accessible;
  41. }
  42. /**
  43. * @inheritdoc
  44. * @since 25.0.0
  45. */
  46. public function getAccessible(): bool {
  47. return $this->accessible;
  48. }
  49. /**
  50. * @inheritdoc
  51. * @since 25.0.0
  52. */
  53. public function setTitle(string $title): void {
  54. $this->title = $title;
  55. }
  56. /**
  57. * @inheritdoc
  58. * @since 25.0.0
  59. */
  60. public function getTitle(): string {
  61. return $this->title ?? $this->reference;
  62. }
  63. /**
  64. * @inheritdoc
  65. * @since 25.0.0
  66. */
  67. public function setDescription(?string $description): void {
  68. $this->description = $description;
  69. }
  70. /**
  71. * @inheritdoc
  72. * @since 25.0.0
  73. */
  74. public function getDescription(): ?string {
  75. return $this->description;
  76. }
  77. /**
  78. * @inheritdoc
  79. * @since 25.0.0
  80. */
  81. public function setImageUrl(?string $imageUrl): void {
  82. $this->imageUrl = $imageUrl;
  83. }
  84. /**
  85. * @inheritdoc
  86. * @since 25.0.0
  87. */
  88. public function getImageUrl(): ?string {
  89. return $this->imageUrl;
  90. }
  91. /**
  92. * @inheritdoc
  93. * @since 25.0.0
  94. */
  95. public function setImageContentType(?string $contentType): void {
  96. $this->contentType = $contentType;
  97. }
  98. /**
  99. * @inheritdoc
  100. * @since 25.0.0
  101. */
  102. public function getImageContentType(): ?string {
  103. return $this->contentType;
  104. }
  105. /**
  106. * @inheritdoc
  107. * @since 25.0.0
  108. */
  109. public function setUrl(?string $url): void {
  110. $this->url = $url;
  111. }
  112. /**
  113. * @inheritdoc
  114. * @since 25.0.0
  115. */
  116. public function getUrl(): string {
  117. return $this->url ?? $this->reference;
  118. }
  119. /**
  120. * @inheritdoc
  121. * @since 25.0.0
  122. */
  123. public function setRichObject(string $type, ?array $richObject): void {
  124. $this->richObjectType = $type;
  125. $this->richObject = $richObject;
  126. }
  127. /**
  128. * @inheritdoc
  129. * @since 25.0.0
  130. */
  131. public function getRichObjectType(): string {
  132. if ($this->richObjectType === null) {
  133. return 'open-graph';
  134. }
  135. return $this->richObjectType;
  136. }
  137. /**
  138. * @inheritdoc
  139. * @since 25.0.0
  140. * @return array<string, mixed>
  141. */
  142. public function getRichObject(): array {
  143. if ($this->richObject === null) {
  144. return $this->getOpenGraphObject();
  145. }
  146. return $this->richObject;
  147. }
  148. /**
  149. * @inheritdoc
  150. * @since 25.0.0
  151. * @return OpenGraphObject
  152. */
  153. public function getOpenGraphObject(): array {
  154. return [
  155. 'id' => $this->getId(),
  156. 'name' => $this->getTitle(),
  157. 'description' => $this->getDescription(),
  158. 'thumb' => $this->getImageUrl(),
  159. 'link' => $this->getUrl()
  160. ];
  161. }
  162. /**
  163. * @param IReference $reference
  164. * @return array
  165. * @since 25.0.0
  166. */
  167. public static function toCache(IReference $reference): array {
  168. return [
  169. 'id' => $reference->getId(),
  170. 'title' => $reference->getTitle(),
  171. 'imageUrl' => $reference->getImageUrl(),
  172. 'imageContentType' => $reference->getImageContentType(),
  173. 'description' => $reference->getDescription(),
  174. 'link' => $reference->getUrl(),
  175. 'accessible' => $reference->getAccessible(),
  176. 'richObjectType' => $reference->getRichObjectType(),
  177. 'richObject' => $reference->getRichObject(),
  178. ];
  179. }
  180. /**
  181. * @param array $cache
  182. * @return IReference
  183. * @since 25.0.0
  184. */
  185. public static function fromCache(array $cache): IReference {
  186. $reference = new Reference($cache['id']);
  187. $reference->setTitle($cache['title']);
  188. $reference->setDescription($cache['description']);
  189. $reference->setImageUrl($cache['imageUrl']);
  190. $reference->setImageContentType($cache['imageContentType']);
  191. $reference->setUrl($cache['link']);
  192. $reference->setRichObject($cache['richObjectType'], $cache['richObject']);
  193. $reference->setAccessible($cache['accessible']);
  194. return $reference;
  195. }
  196. /**
  197. * @inheritdoc
  198. * @since 25.0.0
  199. * @return array{richObjectType: string, richObject: array<string, mixed>, openGraphObject: OpenGraphObject, accessible: bool}
  200. */
  201. public function jsonSerialize(): array {
  202. return [
  203. 'richObjectType' => $this->getRichObjectType(),
  204. 'richObject' => $this->getRichObject(),
  205. 'openGraphObject' => $this->getOpenGraphObject(),
  206. 'accessible' => $this->accessible
  207. ];
  208. }
  209. }