Reference.php 5.3 KB

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