IndexMigrationAttribute.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 index changes
  11. *
  12. * @since 30.0.0
  13. */
  14. class IndexMigrationAttribute extends MigrationAttribute implements JsonSerializable {
  15. /**
  16. * @param string $table name of the database table
  17. * @param IndexType|null $type type of the index
  18. * @param string $description description of the migration
  19. * @param array $notes notes abour the migration/index
  20. * @since 30.0.0
  21. */
  22. public function __construct(
  23. string $table,
  24. private ?IndexType $type = null,
  25. string $description = '',
  26. array $notes = [],
  27. ) {
  28. parent::__construct($table, $description, $notes);
  29. }
  30. /**
  31. * @param IndexType|null $type
  32. *
  33. * @return $this
  34. * @since 30.0.0
  35. */
  36. public function setType(?IndexType $type): self {
  37. $this->type = $type;
  38. return $this;
  39. }
  40. /**
  41. * @return IndexType|null
  42. * @since 30.0.0
  43. */
  44. public function getType(): ?IndexType {
  45. return $this->type;
  46. }
  47. /**
  48. * @param array $data
  49. *
  50. * @return $this
  51. * @since 30.0.0
  52. */
  53. public function import(array $data): self {
  54. parent::import($data);
  55. $this->setType(IndexType::tryFrom($data['type'] ?? ''));
  56. return $this;
  57. }
  58. /**
  59. * @return array
  60. * @since 30.0.0
  61. */
  62. public function jsonSerialize(): array {
  63. return array_merge(
  64. parent::jsonSerialize(),
  65. [
  66. 'type' => $this->getType() ?? '',
  67. ]
  68. );
  69. }
  70. }