|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Migration\Resources\Database; |
| 4 | + |
| 5 | +use Utopia\Migration\Resource; |
| 6 | +use Utopia\Migration\Transfer; |
| 7 | + |
| 8 | +class Attribute extends Resource |
| 9 | +{ |
| 10 | + public const TYPE_STRING = 'string'; |
| 11 | + public const TYPE_INTEGER = 'integer'; |
| 12 | + public const TYPE_FLOAT = 'double'; |
| 13 | + public const TYPE_BOOLEAN = 'boolean'; |
| 14 | + public const TYPE_DATETIME = 'datetime'; |
| 15 | + public const TYPE_EMAIL = 'email'; |
| 16 | + public const TYPE_ENUM = 'enum'; |
| 17 | + public const TYPE_IP = 'ip'; |
| 18 | + public const TYPE_URL = 'url'; |
| 19 | + public const TYPE_RELATIONSHIP = 'relationship'; |
| 20 | + |
| 21 | + public const TYPE_POINT = 'point'; |
| 22 | + public const TYPE_LINE = 'linestring'; |
| 23 | + public const TYPE_POLYGON = 'polygon'; |
| 24 | + |
| 25 | + public const TYPE_OBJECT = 'object'; |
| 26 | + public const TYPE_VECTOR = 'vector'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param string $key |
| 30 | + * @param Table $table |
| 31 | + * @param string $fieldType The actual field type (e.g., 'string', 'integer', 'email') |
| 32 | + * @param int $size |
| 33 | + * @param bool $required |
| 34 | + * @param mixed|null $default |
| 35 | + * @param bool $array |
| 36 | + * @param bool $signed |
| 37 | + * @param string $format |
| 38 | + * @param array<string, mixed> $formatOptions |
| 39 | + * @param array<string> $filters |
| 40 | + * @param array<string, mixed> $options |
| 41 | + * @param string $createdAt |
| 42 | + * @param string $updatedAt |
| 43 | + */ |
| 44 | + public function __construct( |
| 45 | + protected readonly string $key, |
| 46 | + protected readonly Table $table, |
| 47 | + protected readonly string $fieldType = '', |
| 48 | + protected readonly int $size = 0, |
| 49 | + protected readonly bool $required = false, |
| 50 | + protected readonly mixed $default = null, |
| 51 | + protected readonly bool $array = false, |
| 52 | + protected readonly bool $signed = false, |
| 53 | + protected readonly string $format = '', |
| 54 | + protected readonly array $formatOptions = [], |
| 55 | + protected readonly array $filters = [], |
| 56 | + protected array $options = [], |
| 57 | + protected string $createdAt = '', |
| 58 | + protected string $updatedAt = '', |
| 59 | + ) { |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return array<string, mixed> |
| 64 | + */ |
| 65 | + public function jsonSerialize(): array |
| 66 | + { |
| 67 | + return [ |
| 68 | + 'key' => $this->key, |
| 69 | + 'table' => $this->table, |
| 70 | + 'type' => $this->getType(), |
| 71 | + 'size' => $this->size, |
| 72 | + 'required' => $this->required, |
| 73 | + 'default' => $this->default, |
| 74 | + 'array' => $this->array, |
| 75 | + 'signed' => $this->signed, |
| 76 | + 'format' => $this->format, |
| 77 | + 'formatOptions' => $this->formatOptions, |
| 78 | + 'filters' => $this->filters, |
| 79 | + 'options' => $this->options, |
| 80 | + 'createdAt' => $this->createdAt, |
| 81 | + 'updatedAt' => $this->updatedAt, |
| 82 | + ]; |
| 83 | + } |
| 84 | + |
| 85 | + public static function getName(): string |
| 86 | + { |
| 87 | + return Resource::TYPE_ATTRIBUTE; |
| 88 | + } |
| 89 | + |
| 90 | + public function getType(): string |
| 91 | + { |
| 92 | + return $this->fieldType; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Convert a Column resource to an Attribute resource. |
| 97 | + * |
| 98 | + * @param Column $column |
| 99 | + * @return self |
| 100 | + */ |
| 101 | + public static function fromColumn(Column $column): self |
| 102 | + { |
| 103 | + return new self( |
| 104 | + $column->getKey(), |
| 105 | + $column->getTable(), |
| 106 | + $column->getType(), |
| 107 | + $column->getSize(), |
| 108 | + $column->isRequired(), |
| 109 | + $column->getDefault(), |
| 110 | + $column->isArray(), |
| 111 | + $column->isSigned(), |
| 112 | + $column->getFormat(), |
| 113 | + $column->getFormatOptions(), |
| 114 | + $column->getFilters(), |
| 115 | + $column->getOptions(), |
| 116 | + $column->getCreatedAt(), |
| 117 | + $column->getUpdatedAt() |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + public function getGroup(): string |
| 122 | + { |
| 123 | + return Transfer::GROUP_DATABASES; |
| 124 | + } |
| 125 | + |
| 126 | + public function getKey(): string |
| 127 | + { |
| 128 | + return $this->key; |
| 129 | + } |
| 130 | + |
| 131 | + public function getTable(): Table |
| 132 | + { |
| 133 | + return $this->table; |
| 134 | + } |
| 135 | + |
| 136 | + public function getSize(): int |
| 137 | + { |
| 138 | + return $this->size; |
| 139 | + } |
| 140 | + |
| 141 | + public function isRequired(): bool |
| 142 | + { |
| 143 | + return $this->required; |
| 144 | + } |
| 145 | + |
| 146 | + public function getDefault(): mixed |
| 147 | + { |
| 148 | + return $this->default; |
| 149 | + } |
| 150 | + |
| 151 | + public function isArray(): bool |
| 152 | + { |
| 153 | + return $this->array; |
| 154 | + } |
| 155 | + |
| 156 | + public function isSigned(): bool |
| 157 | + { |
| 158 | + return $this->signed; |
| 159 | + } |
| 160 | + |
| 161 | + public function getFormat(): string |
| 162 | + { |
| 163 | + return $this->format; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @return array<string, mixed> |
| 168 | + */ |
| 169 | + public function getFormatOptions(): array |
| 170 | + { |
| 171 | + return $this->formatOptions; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @return array<string> |
| 176 | + */ |
| 177 | + public function getFilters(): array |
| 178 | + { |
| 179 | + return $this->filters; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @return array<string, mixed> |
| 184 | + */ |
| 185 | + public function &getOptions(): array |
| 186 | + { |
| 187 | + return $this->options; |
| 188 | + } |
| 189 | +} |
0 commit comments