1
0

ColumnMigrationAttribute.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * generic class related to migration attribute about column changes
  11. *
  12. * @since 30.0.0
  13. */
  14. class ColumnMigrationAttribute extends MigrationAttribute implements JsonSerializable {
  15. /**
  16. * @param string $table name of the database table
  17. * @param string $name name of the column
  18. * @param ColumnType|null $type type of the column
  19. * @param string $description description of the migration
  20. * @param array $notes notes about the migration/column
  21. * @since 30.0.0
  22. */
  23. public function __construct(
  24. string $table,
  25. private string $name = '',
  26. private ?ColumnType $type = null,
  27. string $description = '',
  28. array $notes = [],
  29. ) {
  30. parent::__construct($table, $description, $notes);
  31. }
  32. /**
  33. * @param string $name
  34. *
  35. * @return $this
  36. * @since 30.0.0
  37. */
  38. public function setName(string $name): self {
  39. $this->name = $name;
  40. return $this;
  41. }
  42. /**
  43. * @return string
  44. * @since 30.0.0
  45. */
  46. public function getName(): string {
  47. return $this->name;
  48. }
  49. /**
  50. * @param ColumnType|null $type
  51. *
  52. * @return $this
  53. * @since 30.0.0
  54. */
  55. public function setType(?ColumnType $type): self {
  56. $this->type = $type;
  57. return $this;
  58. }
  59. /**
  60. * @return ColumnType|null
  61. * @since 30.0.0
  62. */
  63. public function getType(): ?ColumnType {
  64. return $this->type;
  65. }
  66. /**
  67. * @param array $data
  68. *
  69. * @return $this
  70. * @since 30.0.0
  71. */
  72. public function import(array $data): self {
  73. parent::import($data);
  74. $this->setName($data['name'] ?? '');
  75. $this->setType(ColumnType::tryFrom($data['type'] ?? ''));
  76. return $this;
  77. }
  78. /**
  79. * @return array
  80. * @since 30.0.0
  81. */
  82. public function jsonSerialize(): array {
  83. return array_merge(
  84. parent::jsonSerialize(),
  85. [
  86. 'name' => $this->getName(),
  87. 'type' => $this->getType() ?? '',
  88. ]
  89. );
  90. }
  91. }