VersionEntity.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Versions\Db;
  8. use JsonSerializable;
  9. use OCP\AppFramework\Db\Entity;
  10. use OCP\DB\Types;
  11. /**
  12. * @method int getFileId()
  13. * @method void setFileId(int $fileId)
  14. * @method int getTimestamp()
  15. * @method void setTimestamp(int $timestamp)
  16. * @method int|float getSize()
  17. * @method void setSize(int|float $size)
  18. * @method int getMimetype()
  19. * @method void setMimetype(int $mimetype)
  20. * @method array|null getMetadata()
  21. * @method void setMetadata(array $metadata)
  22. */
  23. class VersionEntity extends Entity implements JsonSerializable {
  24. protected ?int $fileId = null;
  25. protected ?int $timestamp = null;
  26. protected ?int $size = null;
  27. protected ?int $mimetype = null;
  28. protected ?array $metadata = null;
  29. public function __construct() {
  30. $this->addType('id', Types::INTEGER);
  31. $this->addType('file_id', Types::INTEGER);
  32. $this->addType('timestamp', Types::INTEGER);
  33. $this->addType('size', Types::INTEGER);
  34. $this->addType('mimetype', Types::INTEGER);
  35. $this->addType('metadata', Types::JSON);
  36. }
  37. public function jsonSerialize(): array {
  38. return [
  39. 'id' => $this->id,
  40. 'file_id' => $this->fileId,
  41. 'timestamp' => $this->timestamp,
  42. 'size' => $this->size,
  43. 'mimetype' => $this->mimetype,
  44. 'metadata' => $this->metadata,
  45. ];
  46. }
  47. /**
  48. * @abstract given a key, return the value associated with the key in the metadata column
  49. * if nothing is found, we return an empty string
  50. * @param string $key key associated with the value
  51. */
  52. public function getMetadataValue(string $key): ?string {
  53. return $this->metadata[$key] ?? null;
  54. }
  55. /**
  56. * @abstract sets a key value pair in the metadata column
  57. * @param string $key key associated with the value
  58. * @param string $value value associated with the key
  59. */
  60. public function setMetadataValue(string $key, string $value): void {
  61. $this->metadata[$key] = $value;
  62. $this->markFieldUpdated('metadata');
  63. }
  64. }