MigrationAttribute.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /**
  10. * @since 30.0.0
  11. */
  12. class MigrationAttribute implements JsonSerializable {
  13. /**
  14. * @param string $table name of the database table
  15. * @param string $description description of the migration
  16. * @param array $notes notes about the migration
  17. * @since 30.0.0
  18. */
  19. public function __construct(
  20. private string $table,
  21. private string $description = '',
  22. private array $notes = [],
  23. ) {
  24. }
  25. /**
  26. * @param string $table
  27. *
  28. * @return $this
  29. * @since 30.0.0
  30. */
  31. public function setTable(string $table): self {
  32. $this->table = $table;
  33. return $this;
  34. }
  35. /**
  36. * @return string
  37. * @since 30.0.0
  38. */
  39. public function getTable(): string {
  40. return $this->table;
  41. }
  42. /**
  43. * @param string $description
  44. *
  45. * @return $this
  46. * @since 30.0.0
  47. */
  48. public function setDescription(string $description): self {
  49. $this->description = $description;
  50. return $this;
  51. }
  52. /**
  53. * @return string
  54. * @since 30.0.0
  55. */
  56. public function getDescription(): string {
  57. return $this->description;
  58. }
  59. /**
  60. * @param array $notes
  61. *
  62. * @return $this
  63. * @since 30.0.0
  64. */
  65. public function setNotes(array $notes): self {
  66. $this->notes = $notes;
  67. return $this;
  68. }
  69. /**
  70. * @return array
  71. * @since 30.0.0
  72. */
  73. public function getNotes(): array {
  74. return $this->notes;
  75. }
  76. /**
  77. * @return string
  78. * @since 30.0.0
  79. */
  80. public function definition(): string {
  81. return json_encode($this->jsonSerialize(), JSON_UNESCAPED_SLASHES);
  82. }
  83. /**
  84. * @param array $data
  85. *
  86. * @return self
  87. * @since 30.0.0
  88. */
  89. public function import(array $data): self {
  90. return $this->setDescription($data['description'] ?? '')
  91. ->setNotes($data['notes'] ?? []);
  92. }
  93. /**
  94. * @return array
  95. * @since 30.0.0
  96. */
  97. public function jsonSerialize(): array {
  98. return [
  99. 'class' => get_class($this),
  100. 'table' => $this->getTable(),
  101. 'description' => $this->getDescription(),
  102. 'notes' => $this->getNotes()
  103. ];
  104. }
  105. }