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.

117 lines
2.1 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Migration\Attributes;
  8. use JsonSerializable;
  9. use OCP\AppFramework\Attribute\Consumable;
  10. #[Consumable(since: '30.0.0')]
  11. class MigrationAttribute implements JsonSerializable {
  12. /**
  13. * @param string $table name of the database table
  14. * @param string $description description of the migration
  15. * @param array $notes notes about the migration
  16. * @since 30.0.0
  17. */
  18. public function __construct(
  19. private string $table,
  20. private string $description = '',
  21. private array $notes = [],
  22. ) {
  23. }
  24. /**
  25. * @param string $table
  26. *
  27. * @return $this
  28. * @since 30.0.0
  29. */
  30. public function setTable(string $table): self {
  31. $this->table = $table;
  32. return $this;
  33. }
  34. /**
  35. * @return string
  36. * @since 30.0.0
  37. */
  38. public function getTable(): string {
  39. return $this->table;
  40. }
  41. /**
  42. * @param string $description
  43. *
  44. * @return $this
  45. * @since 30.0.0
  46. */
  47. public function setDescription(string $description): self {
  48. $this->description = $description;
  49. return $this;
  50. }
  51. /**
  52. * @return string
  53. * @since 30.0.0
  54. */
  55. public function getDescription(): string {
  56. return $this->description;
  57. }
  58. /**
  59. * @param array $notes
  60. *
  61. * @return $this
  62. * @since 30.0.0
  63. */
  64. public function setNotes(array $notes): self {
  65. $this->notes = $notes;
  66. return $this;
  67. }
  68. /**
  69. * @return array
  70. * @since 30.0.0
  71. */
  72. public function getNotes(): array {
  73. return $this->notes;
  74. }
  75. /**
  76. * @return string
  77. * @since 30.0.0
  78. */
  79. public function definition(): string {
  80. return json_encode($this->jsonSerialize(), JSON_UNESCAPED_SLASHES);
  81. }
  82. /**
  83. * @param array $data
  84. *
  85. * @return self
  86. * @since 30.0.0
  87. */
  88. public function import(array $data): self {
  89. return $this->setDescription($data['description'] ?? '')
  90. ->setNotes($data['notes'] ?? []);
  91. }
  92. /**
  93. * @return array
  94. * @since 30.0.0
  95. */
  96. public function jsonSerialize(): array {
  97. return [
  98. 'class' => get_class($this),
  99. 'table' => $this->getTable(),
  100. 'description' => $this->getDescription(),
  101. 'notes' => $this->getNotes()
  102. ];
  103. }
  104. }