VersionEntity.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Louis Chmn <louis@chmn.me>
  5. *
  6. * @author Louis Chmn <louis@chmn.me>
  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 OCA\Files_Versions\Db;
  25. use JsonSerializable;
  26. use OCP\AppFramework\Db\Entity;
  27. use OCP\DB\Types;
  28. /**
  29. * @method int getFileId()
  30. * @method void setFileId(int $fileId)
  31. * @method int getTimestamp()
  32. * @method void setTimestamp(int $timestamp)
  33. * @method int|float getSize()
  34. * @method void setSize(int|float $size)
  35. * @method int getMimetype()
  36. * @method void setMimetype(int $mimetype)
  37. * @method array|null getMetadata()
  38. * @method void setMetadata(array $metadata)
  39. */
  40. class VersionEntity extends Entity implements JsonSerializable {
  41. protected ?int $fileId = null;
  42. protected ?int $timestamp = null;
  43. protected ?int $size = null;
  44. protected ?int $mimetype = null;
  45. protected ?array $metadata = null;
  46. public function __construct() {
  47. $this->addType('id', Types::INTEGER);
  48. $this->addType('file_id', Types::INTEGER);
  49. $this->addType('timestamp', Types::INTEGER);
  50. $this->addType('size', Types::INTEGER);
  51. $this->addType('mimetype', Types::INTEGER);
  52. $this->addType('metadata', Types::JSON);
  53. }
  54. public function jsonSerialize(): array {
  55. return [
  56. 'id' => $this->id,
  57. 'file_id' => $this->fileId,
  58. 'timestamp' => $this->timestamp,
  59. 'size' => $this->size,
  60. 'mimetype' => $this->mimetype,
  61. 'metadata' => $this->metadata,
  62. ];
  63. }
  64. /**
  65. * @abstract given a key, return the value associated with the key in the metadata column
  66. * if nothing is found, we return an empty string
  67. * @param string $key key associated with the value
  68. */
  69. public function getMetadataValue(string $key): ?string {
  70. return $this->metadata[$key] ?? null;
  71. }
  72. /**
  73. * @abstract sets a key value pair in the metadata column
  74. * @param string $key key associated with the value
  75. * @param string $value value associated with the key
  76. */
  77. public function setMetadataValue(string $key, string $value): void {
  78. $this->metadata[$key] = $value;
  79. $this->markFieldUpdated('metadata');
  80. }
  81. }