You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
9.5 KiB

  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 bool $indexed = false;
  38. /**
  39. * @param string $type value type
  40. *
  41. * @inheritDoc
  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 __construct(string $type = '') {
  52. $this->type = $type;
  53. }
  54. /**
  55. * @inheritDoc
  56. * @return string value type
  57. * @see self::TYPE_INT
  58. * @see self::TYPE_FLOAT
  59. * @see self::TYPE_BOOL
  60. * @see self::TYPE_ARRAY
  61. * @see self::TYPE_STRING_LIST
  62. * @see self::TYPE_INT_LIST
  63. * @see self::TYPE_STRING
  64. * @since 28.0.0
  65. */
  66. public function getType(): string {
  67. return $this->type;
  68. }
  69. /**
  70. * @param string $type value type
  71. *
  72. * @inheritDoc
  73. * @return bool
  74. * @see self::TYPE_INT
  75. * @see self::TYPE_FLOAT
  76. * @see self::TYPE_BOOL
  77. * @see self::TYPE_ARRAY
  78. * @see self::TYPE_STRING_LIST
  79. * @see self::TYPE_INT_LIST
  80. * @see self::TYPE_STRING
  81. * @since 28.0.0
  82. */
  83. public function isType(string $type): bool {
  84. return (strtolower($type) === strtolower($this->type));
  85. }
  86. /**
  87. * @param string $type value type
  88. *
  89. * @inheritDoc
  90. * @return self
  91. * @throws FilesMetadataTypeException if type cannot be confirmed
  92. * @see self::TYPE_INT
  93. * @see self::TYPE_BOOL
  94. * @see self::TYPE_ARRAY
  95. * @see self::TYPE_STRING_LIST
  96. * @see self::TYPE_INT_LIST
  97. * @see self::TYPE_STRING
  98. * @see self::TYPE_FLOAT
  99. * @since 28.0.0
  100. */
  101. public function assertType(string $type): self {
  102. if (!$this->isType($type)) {
  103. throw new FilesMetadataTypeException('type is \'' . $this->getType() . '\', expecting \'' . $type . '\'');
  104. }
  105. return $this;
  106. }
  107. /**
  108. * @param string $value string to be set as value
  109. *
  110. * @inheritDoc
  111. * @return self
  112. * @throws FilesMetadataTypeException if wrapper was not set to store a string
  113. * @since 28.0.0
  114. */
  115. public function setValueString(string $value): self {
  116. $this->assertType(self::TYPE_STRING);
  117. $this->value = $value;
  118. return $this;
  119. }
  120. /**
  121. * @param int $value int to be set as value
  122. *
  123. * @inheritDoc
  124. * @return self
  125. * @throws FilesMetadataTypeException if wrapper was not set to store an int
  126. * @since 28.0.0
  127. */
  128. public function setValueInt(int $value): self {
  129. $this->assertType(self::TYPE_INT);
  130. $this->value = $value;
  131. return $this;
  132. }
  133. /**
  134. * @param float $value float to be set as value
  135. *
  136. * @inheritDoc
  137. * @return self
  138. * @throws FilesMetadataTypeException if wrapper was not set to store a float
  139. * @since 28.0.0
  140. */
  141. public function setValueFloat(float $value): self {
  142. $this->assertType(self::TYPE_FLOAT);
  143. $this->value = $value;
  144. return $this;
  145. }
  146. /**
  147. * @param bool $value bool to be set as value
  148. *
  149. * @inheritDoc
  150. * @return self
  151. * @throws FilesMetadataTypeException if wrapper was not set to store a bool
  152. * @since 28.0.0
  153. */
  154. public function setValueBool(bool $value): self {
  155. $this->assertType(self::TYPE_BOOL);
  156. $this->value = $value;
  157. return $this;
  158. }
  159. /**
  160. * @param array $value array to be set as value
  161. *
  162. * @inheritDoc
  163. * @return self
  164. * @throws FilesMetadataTypeException if wrapper was not set to store an array
  165. * @since 28.0.0
  166. */
  167. public function setValueArray(array $value): self {
  168. $this->assertType(self::TYPE_ARRAY);
  169. $this->value = $value;
  170. return $this;
  171. }
  172. /**
  173. * @param string[] $value string list to be set as value
  174. *
  175. * @inheritDoc
  176. * @return self
  177. * @throws FilesMetadataTypeException if wrapper was not set to store a string list
  178. * @since 28.0.0
  179. */
  180. public function setValueStringList(array $value): self {
  181. $this->assertType(self::TYPE_STRING_LIST);
  182. // TODO confirm value is an array or string ?
  183. $this->value = $value;
  184. return $this;
  185. }
  186. /**
  187. * @param int[] $value int list to be set as value
  188. *
  189. * @inheritDoc
  190. * @return self
  191. * @throws FilesMetadataTypeException if wrapper was not set to store an int list
  192. * @since 28.0.0
  193. */
  194. public function setValueIntList(array $value): self {
  195. $this->assertType(self::TYPE_INT_LIST);
  196. // TODO confirm value is an array of int ?
  197. $this->value = $value;
  198. return $this;
  199. }
  200. /**
  201. * @inheritDoc
  202. * @return string set value
  203. * @throws FilesMetadataTypeException if wrapper was not set to store a string
  204. * @throws FilesMetadataNotFoundException if value is not set
  205. * @since 28.0.0
  206. */
  207. public function getValueString(): string {
  208. $this->assertType(self::TYPE_STRING);
  209. if (null === $this->value) {
  210. throw new FilesMetadataNotFoundException('value is not set');
  211. }
  212. return (string)$this->value;
  213. }
  214. /**
  215. * @inheritDoc
  216. * @return int set value
  217. * @throws FilesMetadataTypeException if wrapper was not set to store an int
  218. * @throws FilesMetadataNotFoundException if value is not set
  219. * @since 28.0.0
  220. */
  221. public function getValueInt(): int {
  222. $this->assertType(self::TYPE_INT);
  223. if (null === $this->value) {
  224. throw new FilesMetadataNotFoundException('value is not set');
  225. }
  226. return (int)$this->value;
  227. }
  228. /**
  229. * @inheritDoc
  230. * @return float set value
  231. * @throws FilesMetadataTypeException if wrapper was not set to store a float
  232. * @throws FilesMetadataNotFoundException if value is not set
  233. * @since 28.0.0
  234. */
  235. public function getValueFloat(): float {
  236. $this->assertType(self::TYPE_FLOAT);
  237. if (null === $this->value) {
  238. throw new FilesMetadataNotFoundException('value is not set');
  239. }
  240. return (float)$this->value;
  241. }
  242. /**
  243. * @inheritDoc
  244. * @return bool set value
  245. * @throws FilesMetadataTypeException if wrapper was not set to store a bool
  246. * @throws FilesMetadataNotFoundException if value is not set
  247. * @since 28.0.0
  248. */
  249. public function getValueBool(): bool {
  250. $this->assertType(self::TYPE_BOOL);
  251. if (null === $this->value) {
  252. throw new FilesMetadataNotFoundException('value is not set');
  253. }
  254. return (bool)$this->value;
  255. }
  256. /**
  257. * @inheritDoc
  258. * @return array set value
  259. * @throws FilesMetadataTypeException if wrapper was not set to store an array
  260. * @throws FilesMetadataNotFoundException if value is not set
  261. * @since 28.0.0
  262. */
  263. public function getValueArray(): array {
  264. $this->assertType(self::TYPE_ARRAY);
  265. if (null === $this->value) {
  266. throw new FilesMetadataNotFoundException('value is not set');
  267. }
  268. return (array)$this->value;
  269. }
  270. /**
  271. * @inheritDoc
  272. * @return string[] set value
  273. * @throws FilesMetadataTypeException if wrapper was not set to store a string list
  274. * @throws FilesMetadataNotFoundException if value is not set
  275. * @since 28.0.0
  276. */
  277. public function getValueStringList(): array {
  278. $this->assertType(self::TYPE_STRING_LIST);
  279. if (null === $this->value) {
  280. throw new FilesMetadataNotFoundException('value is not set');
  281. }
  282. return (array)$this->value;
  283. }
  284. /**
  285. * @inheritDoc
  286. * @return int[] set value
  287. * @throws FilesMetadataTypeException if wrapper was not set to store an int list
  288. * @throws FilesMetadataNotFoundException if value is not set
  289. * @since 28.0.0
  290. */
  291. public function getValueIntList(): array {
  292. $this->assertType(self::TYPE_INT_LIST);
  293. if (null === $this->value) {
  294. throw new FilesMetadataNotFoundException('value is not set');
  295. }
  296. return (array)$this->value;
  297. }
  298. /**
  299. * @inheritDoc
  300. * @return string|int|float|bool|array|string[]|int[] set value
  301. * @throws FilesMetadataNotFoundException if value is not set
  302. * @since 28.0.0
  303. */
  304. public function getValueAny(): mixed {
  305. if (null === $this->value) {
  306. throw new FilesMetadataNotFoundException('value is not set');
  307. }
  308. return $this->value;
  309. }
  310. /**
  311. * @param bool $indexed TRUE to set the stored value as an indexed value
  312. *
  313. * @inheritDoc
  314. * @return self
  315. * @since 28.0.0
  316. */
  317. public function setIndexed(bool $indexed): self {
  318. $this->indexed = $indexed;
  319. return $this;
  320. }
  321. /**
  322. * @inheritDoc
  323. * @return bool TRUE if value is an indexed value
  324. * @since 28.0.0
  325. */
  326. public function isIndexed(): bool {
  327. return $this->indexed;
  328. }
  329. /**
  330. * @param array $data serialized version of the object
  331. *
  332. * @inheritDoc
  333. * @return self
  334. * @see jsonSerialize
  335. * @since 28.0.0
  336. */
  337. public function import(array $data): self {
  338. $this->value = $data['value'] ?? null;
  339. $this->type = $data['type'] ?? '';
  340. $this->setIndexed($data['indexed'] ?? false);
  341. return $this;
  342. }
  343. public function jsonSerialize(bool $emptyValues = false): array {
  344. return [
  345. 'value' => ($emptyValues) ? null : $this->value,
  346. 'type' => $this->getType(),
  347. 'indexed' => $this->isIndexed()
  348. ];
  349. }
  350. }