1
0

MetadataValueWrapper.php 10 KB

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