1
0

MetadataValueWrapper.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\FilesMetadata\Model;
  8. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  9. use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException;
  10. use OCP\FilesMetadata\Model\IMetadataValueWrapper;
  11. /**
  12. * @inheritDoc
  13. * @see IFilesMetadata
  14. * @since 28.0.0
  15. */
  16. class MetadataValueWrapper implements IMetadataValueWrapper {
  17. private string $type;
  18. /** @var string|int|float|bool|array|string[]|int[] */
  19. private mixed $value = null;
  20. private string $etag = '';
  21. private bool $indexed = false;
  22. private int $editPermission = self::EDIT_FORBIDDEN;
  23. /**
  24. * @param string $type value type
  25. *
  26. * @inheritDoc
  27. * @see self::TYPE_INT
  28. * @see self::TYPE_FLOAT
  29. * @see self::TYPE_BOOL
  30. * @see self::TYPE_ARRAY
  31. * @see self::TYPE_STRING_LIST
  32. * @see self::TYPE_INT_LIST
  33. * @see self::TYPE_STRING
  34. * @since 28.0.0
  35. */
  36. public function __construct(string $type = '') {
  37. $this->type = $type;
  38. }
  39. /**
  40. * @inheritDoc
  41. * @return string value type
  42. * @see self::TYPE_INT
  43. * @see self::TYPE_FLOAT
  44. * @see self::TYPE_BOOL
  45. * @see self::TYPE_ARRAY
  46. * @see self::TYPE_STRING_LIST
  47. * @see self::TYPE_INT_LIST
  48. * @see self::TYPE_STRING
  49. * @since 28.0.0
  50. */
  51. public function getType(): string {
  52. return $this->type;
  53. }
  54. /**
  55. * @param string $type value type
  56. *
  57. * @inheritDoc
  58. * @return bool
  59. * @see self::TYPE_INT
  60. * @see self::TYPE_FLOAT
  61. * @see self::TYPE_BOOL
  62. * @see self::TYPE_ARRAY
  63. * @see self::TYPE_STRING_LIST
  64. * @see self::TYPE_INT_LIST
  65. * @see self::TYPE_STRING
  66. * @since 28.0.0
  67. */
  68. public function isType(string $type): bool {
  69. return (strtolower($type) === strtolower($this->type));
  70. }
  71. /**
  72. * @param string $type value type
  73. *
  74. * @inheritDoc
  75. * @return self
  76. * @throws FilesMetadataTypeException if type cannot be confirmed
  77. * @see self::TYPE_INT
  78. * @see self::TYPE_BOOL
  79. * @see self::TYPE_ARRAY
  80. * @see self::TYPE_STRING_LIST
  81. * @see self::TYPE_INT_LIST
  82. * @see self::TYPE_STRING
  83. * @see self::TYPE_FLOAT
  84. * @since 28.0.0
  85. */
  86. public function assertType(string $type): self {
  87. if (!$this->isType($type)) {
  88. throw new FilesMetadataTypeException('type is \'' . $this->getType() . '\', expecting \'' . $type . '\'');
  89. }
  90. return $this;
  91. }
  92. /**
  93. * @param string $value string to be set as value
  94. *
  95. * @inheritDoc
  96. * @return self
  97. * @throws FilesMetadataTypeException if wrapper was not set to store a string
  98. * @since 28.0.0
  99. */
  100. public function setValueString(string $value): self {
  101. $this->assertType(self::TYPE_STRING);
  102. $this->value = $value;
  103. return $this;
  104. }
  105. /**
  106. * @param int $value int to be set as value
  107. *
  108. * @inheritDoc
  109. * @return self
  110. * @throws FilesMetadataTypeException if wrapper was not set to store an int
  111. * @since 28.0.0
  112. */
  113. public function setValueInt(int $value): self {
  114. $this->assertType(self::TYPE_INT);
  115. $this->value = $value;
  116. return $this;
  117. }
  118. /**
  119. * @param float $value float to be set as value
  120. *
  121. * @inheritDoc
  122. * @return self
  123. * @throws FilesMetadataTypeException if wrapper was not set to store a float
  124. * @since 28.0.0
  125. */
  126. public function setValueFloat(float $value): self {
  127. $this->assertType(self::TYPE_FLOAT);
  128. $this->value = $value;
  129. return $this;
  130. }
  131. /**
  132. * @param bool $value bool to be set as value
  133. *
  134. * @inheritDoc
  135. * @return self
  136. * @throws FilesMetadataTypeException if wrapper was not set to store a bool
  137. * @since 28.0.0
  138. */
  139. public function setValueBool(bool $value): self {
  140. $this->assertType(self::TYPE_BOOL);
  141. $this->value = $value;
  142. return $this;
  143. }
  144. /**
  145. * @param array $value array to be set as value
  146. *
  147. * @inheritDoc
  148. * @return self
  149. * @throws FilesMetadataTypeException if wrapper was not set to store an array
  150. * @since 28.0.0
  151. */
  152. public function setValueArray(array $value): self {
  153. $this->assertType(self::TYPE_ARRAY);
  154. $this->value = $value;
  155. return $this;
  156. }
  157. /**
  158. * @param string[] $value string list to be set as value
  159. *
  160. * @inheritDoc
  161. * @return self
  162. * @throws FilesMetadataTypeException if wrapper was not set to store a string list
  163. * @since 28.0.0
  164. */
  165. public function setValueStringList(array $value): self {
  166. $this->assertType(self::TYPE_STRING_LIST);
  167. // TODO confirm value is an array or string ?
  168. $this->value = $value;
  169. return $this;
  170. }
  171. /**
  172. * @param int[] $value int list to be set as value
  173. *
  174. * @inheritDoc
  175. * @return self
  176. * @throws FilesMetadataTypeException if wrapper was not set to store an int list
  177. * @since 28.0.0
  178. */
  179. public function setValueIntList(array $value): self {
  180. $this->assertType(self::TYPE_INT_LIST);
  181. // TODO confirm value is an array of int ?
  182. $this->value = $value;
  183. return $this;
  184. }
  185. /**
  186. * @inheritDoc
  187. * @return string set value
  188. * @throws FilesMetadataTypeException if wrapper was not set to store a string
  189. * @throws FilesMetadataNotFoundException if value is not set
  190. * @since 28.0.0
  191. */
  192. public function getValueString(): string {
  193. $this->assertType(self::TYPE_STRING);
  194. if ($this->value === null) {
  195. throw new FilesMetadataNotFoundException('value is not set');
  196. }
  197. return (string)$this->value;
  198. }
  199. /**
  200. * @inheritDoc
  201. * @return int set value
  202. * @throws FilesMetadataTypeException if wrapper was not set to store an int
  203. * @throws FilesMetadataNotFoundException if value is not set
  204. * @since 28.0.0
  205. */
  206. public function getValueInt(): int {
  207. $this->assertType(self::TYPE_INT);
  208. if ($this->value === null) {
  209. throw new FilesMetadataNotFoundException('value is not set');
  210. }
  211. return (int)$this->value;
  212. }
  213. /**
  214. * @inheritDoc
  215. * @return float set value
  216. * @throws FilesMetadataTypeException if wrapper was not set to store a float
  217. * @throws FilesMetadataNotFoundException if value is not set
  218. * @since 28.0.0
  219. */
  220. public function getValueFloat(): float {
  221. $this->assertType(self::TYPE_FLOAT);
  222. if ($this->value === null) {
  223. throw new FilesMetadataNotFoundException('value is not set');
  224. }
  225. return (float)$this->value;
  226. }
  227. /**
  228. * @inheritDoc
  229. * @return bool set value
  230. * @throws FilesMetadataTypeException if wrapper was not set to store a bool
  231. * @throws FilesMetadataNotFoundException if value is not set
  232. * @since 28.0.0
  233. */
  234. public function getValueBool(): bool {
  235. $this->assertType(self::TYPE_BOOL);
  236. if ($this->value === null) {
  237. throw new FilesMetadataNotFoundException('value is not set');
  238. }
  239. return (bool)$this->value;
  240. }
  241. /**
  242. * @inheritDoc
  243. * @return array set value
  244. * @throws FilesMetadataTypeException if wrapper was not set to store an array
  245. * @throws FilesMetadataNotFoundException if value is not set
  246. * @since 28.0.0
  247. */
  248. public function getValueArray(): array {
  249. $this->assertType(self::TYPE_ARRAY);
  250. if ($this->value === null) {
  251. throw new FilesMetadataNotFoundException('value is not set');
  252. }
  253. return (array)$this->value;
  254. }
  255. /**
  256. * @inheritDoc
  257. * @return string[] set value
  258. * @throws FilesMetadataTypeException if wrapper was not set to store a string list
  259. * @throws FilesMetadataNotFoundException if value is not set
  260. * @since 28.0.0
  261. */
  262. public function getValueStringList(): array {
  263. $this->assertType(self::TYPE_STRING_LIST);
  264. if ($this->value === null) {
  265. throw new FilesMetadataNotFoundException('value is not set');
  266. }
  267. return (array)$this->value;
  268. }
  269. /**
  270. * @inheritDoc
  271. * @return int[] set value
  272. * @throws FilesMetadataTypeException if wrapper was not set to store an int list
  273. * @throws FilesMetadataNotFoundException if value is not set
  274. * @since 28.0.0
  275. */
  276. public function getValueIntList(): array {
  277. $this->assertType(self::TYPE_INT_LIST);
  278. if ($this->value === null) {
  279. throw new FilesMetadataNotFoundException('value is not set');
  280. }
  281. return (array)$this->value;
  282. }
  283. /**
  284. * @inheritDoc
  285. * @return string|int|float|bool|array|string[]|int[] set value
  286. * @throws FilesMetadataNotFoundException if value is not set
  287. * @since 28.0.0
  288. */
  289. public function getValueAny(): mixed {
  290. if ($this->value === null) {
  291. throw new FilesMetadataNotFoundException('value is not set');
  292. }
  293. return $this->value;
  294. }
  295. /**
  296. * @inheritDoc
  297. * @return string stored etag
  298. * @since 29.0.0
  299. */
  300. public function getEtag(): string {
  301. return $this->etag;
  302. }
  303. /**
  304. * @param string $etag etag value
  305. *
  306. * @inheritDoc
  307. * @return self
  308. * @since 29.0.0
  309. */
  310. public function setEtag(string $etag): self {
  311. $this->etag = $etag;
  312. return $this;
  313. }
  314. /**
  315. * @param bool $indexed TRUE to set the stored value as an indexed value
  316. *
  317. * @inheritDoc
  318. * @return self
  319. * @since 28.0.0
  320. */
  321. public function setIndexed(bool $indexed): self {
  322. $this->indexed = $indexed;
  323. return $this;
  324. }
  325. /**
  326. * @inheritDoc
  327. * @return bool TRUE if value is an indexed value
  328. * @since 28.0.0
  329. */
  330. public function isIndexed(): bool {
  331. return $this->indexed;
  332. }
  333. /**
  334. * @param int $permission edit permission
  335. *
  336. * @inheritDoc
  337. * @return self
  338. * @since 28.0.0
  339. */
  340. public function setEditPermission(int $permission): self {
  341. $this->editPermission = $permission;
  342. return $this;
  343. }
  344. /**
  345. * @inheritDoc
  346. * @return int edit permission
  347. * @since 28.0.0
  348. */
  349. public function getEditPermission(): int {
  350. return $this->editPermission;
  351. }
  352. /**
  353. * @param array $data serialized version of the object
  354. *
  355. * @inheritDoc
  356. * @return self
  357. * @see jsonSerialize
  358. * @since 28.0.0
  359. */
  360. public function import(array $data): self {
  361. $this->value = $data['value'] ?? null;
  362. $this->type = $data['type'] ?? '';
  363. $this->setEtag($data['etag'] ?? '');
  364. $this->setIndexed($data['indexed'] ?? false);
  365. $this->setEditPermission($data['editPermission'] ?? self::EDIT_FORBIDDEN);
  366. return $this;
  367. }
  368. public function jsonSerialize(bool $emptyValues = false): array {
  369. return [
  370. 'value' => ($emptyValues) ? null : $this->value,
  371. 'type' => $this->getType(),
  372. 'etag' => $this->getEtag(),
  373. 'indexed' => $this->isIndexed(),
  374. 'editPermission' => $this->getEditPermission()
  375. ];
  376. }
  377. }