Reference.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OCP\Collaboration\Reference;
  24. /**
  25. * @since 25.0.0
  26. * @psalm-type OpenGraphObject = array{id: string, name: string, description: ?string, thumb: ?string, link: string}
  27. */
  28. class Reference implements IReference {
  29. protected string $reference;
  30. protected bool $accessible = true;
  31. protected ?string $title = null;
  32. protected ?string $description = null;
  33. protected ?string $imageUrl = null;
  34. protected ?string $contentType = null;
  35. protected ?string $url = null;
  36. protected ?string $richObjectType = null;
  37. protected ?array $richObject = null;
  38. /**
  39. * @since 25.0.0
  40. */
  41. public function __construct(string $reference) {
  42. $this->reference = $reference;
  43. }
  44. /**
  45. * @inheritdoc
  46. * @since 25.0.0
  47. */
  48. public function getId(): string {
  49. return $this->reference;
  50. }
  51. /**
  52. * @inheritdoc
  53. * @since 25.0.0
  54. */
  55. public function setAccessible(bool $accessible): void {
  56. $this->accessible = $accessible;
  57. }
  58. /**
  59. * @inheritdoc
  60. * @since 25.0.0
  61. */
  62. public function getAccessible(): bool {
  63. return $this->accessible;
  64. }
  65. /**
  66. * @inheritdoc
  67. * @since 25.0.0
  68. */
  69. public function setTitle(string $title): void {
  70. $this->title = $title;
  71. }
  72. /**
  73. * @inheritdoc
  74. * @since 25.0.0
  75. */
  76. public function getTitle(): string {
  77. return $this->title ?? $this->reference;
  78. }
  79. /**
  80. * @inheritdoc
  81. * @since 25.0.0
  82. */
  83. public function setDescription(?string $description): void {
  84. $this->description = $description;
  85. }
  86. /**
  87. * @inheritdoc
  88. * @since 25.0.0
  89. */
  90. public function getDescription(): ?string {
  91. return $this->description;
  92. }
  93. /**
  94. * @inheritdoc
  95. * @since 25.0.0
  96. */
  97. public function setImageUrl(?string $imageUrl): void {
  98. $this->imageUrl = $imageUrl;
  99. }
  100. /**
  101. * @inheritdoc
  102. * @since 25.0.0
  103. */
  104. public function getImageUrl(): ?string {
  105. return $this->imageUrl;
  106. }
  107. /**
  108. * @inheritdoc
  109. * @since 25.0.0
  110. */
  111. public function setImageContentType(?string $contentType): void {
  112. $this->contentType = $contentType;
  113. }
  114. /**
  115. * @inheritdoc
  116. * @since 25.0.0
  117. */
  118. public function getImageContentType(): ?string {
  119. return $this->contentType;
  120. }
  121. /**
  122. * @inheritdoc
  123. * @since 25.0.0
  124. */
  125. public function setUrl(?string $url): void {
  126. $this->url = $url;
  127. }
  128. /**
  129. * @inheritdoc
  130. * @since 25.0.0
  131. */
  132. public function getUrl(): string {
  133. return $this->url ?? $this->reference;
  134. }
  135. /**
  136. * @inheritdoc
  137. * @since 25.0.0
  138. */
  139. public function setRichObject(string $type, ?array $richObject): void {
  140. $this->richObjectType = $type;
  141. $this->richObject = $richObject;
  142. }
  143. /**
  144. * @inheritdoc
  145. * @since 25.0.0
  146. */
  147. public function getRichObjectType(): string {
  148. if ($this->richObjectType === null) {
  149. return 'open-graph';
  150. }
  151. return $this->richObjectType;
  152. }
  153. /**
  154. * @inheritdoc
  155. * @since 25.0.0
  156. * @return array<string, mixed>
  157. */
  158. public function getRichObject(): array {
  159. if ($this->richObject === null) {
  160. return $this->getOpenGraphObject();
  161. }
  162. return $this->richObject;
  163. }
  164. /**
  165. * @inheritdoc
  166. * @since 25.0.0
  167. * @return OpenGraphObject
  168. */
  169. public function getOpenGraphObject(): array {
  170. return [
  171. 'id' => $this->getId(),
  172. 'name' => $this->getTitle(),
  173. 'description' => $this->getDescription(),
  174. 'thumb' => $this->getImageUrl(),
  175. 'link' => $this->getUrl()
  176. ];
  177. }
  178. /**
  179. * @param IReference $reference
  180. * @return array
  181. * @since 25.0.0
  182. */
  183. public static function toCache(IReference $reference): array {
  184. return [
  185. 'id' => $reference->getId(),
  186. 'title' => $reference->getTitle(),
  187. 'imageUrl' => $reference->getImageUrl(),
  188. 'imageContentType' => $reference->getImageContentType(),
  189. 'description' => $reference->getDescription(),
  190. 'link' => $reference->getUrl(),
  191. 'accessible' => $reference->getAccessible(),
  192. 'richObjectType' => $reference->getRichObjectType(),
  193. 'richObject' => $reference->getRichObject(),
  194. ];
  195. }
  196. /**
  197. * @param array $cache
  198. * @return IReference
  199. * @since 25.0.0
  200. */
  201. public static function fromCache(array $cache): IReference {
  202. $reference = new Reference($cache['id']);
  203. $reference->setTitle($cache['title']);
  204. $reference->setDescription($cache['description']);
  205. $reference->setImageUrl($cache['imageUrl']);
  206. $reference->setImageContentType($cache['imageContentType']);
  207. $reference->setUrl($cache['link']);
  208. $reference->setRichObject($cache['richObjectType'], $cache['richObject']);
  209. $reference->setAccessible($cache['accessible']);
  210. return $reference;
  211. }
  212. /**
  213. * @inheritdoc
  214. * @since 25.0.0
  215. * @return array{richObjectType: string, richObject: array<string, mixed>, openGraphObject: OpenGraphObject, accessible: bool}
  216. */
  217. public function jsonSerialize() {
  218. return [
  219. 'richObjectType' => $this->getRichObjectType(),
  220. 'richObject' => $this->getRichObject(),
  221. 'openGraphObject' => $this->getOpenGraphObject(),
  222. 'accessible' => $this->accessible
  223. ];
  224. }
  225. }