| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Cycle\Schema\Definition; |
||
| 6 | |||
| 7 | use Cycle\ORM\Schema\GeneratedField; |
||
| 8 | use Cycle\Schema\Definition\Map\OptionMap; |
||
| 9 | use Cycle\Schema\Exception\FieldException; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Field declaration, it's type and mapping to column. |
||
| 13 | */ |
||
| 14 | final class Field |
||
| 15 | { |
||
| 16 | private OptionMap $options; |
||
| 17 | private OptionMap $attributes; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var non-empty-string|null |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 21 | */ |
||
| 22 | private ?string $column = null; |
||
| 23 | 1494 | ||
| 24 | /** |
||
| 25 | 1494 | * @var non-empty-string|null |
|
|
0 ignored issues
–
show
|
|||
| 26 | 1494 | */ |
|
| 27 | private ?string $type = null; |
||
| 28 | 58 | ||
| 29 | private bool $primary = false; |
||
| 30 | 58 | ||
| 31 | 58 | /** |
|
| 32 | * @var callable-array|string|null |
||
|
0 ignored issues
–
show
|
|||
| 33 | 712 | */ |
|
| 34 | private array|string|null $typecast = null; |
||
| 35 | 712 | ||
| 36 | private ?int $generated = null; |
||
| 37 | private bool $referenced = false; |
||
| 38 | 1172 | private ?string $entityClass = null; |
|
| 39 | |||
| 40 | 1172 | public function __construct() |
|
| 41 | 2 | { |
|
| 42 | $this->options = new OptionMap(); |
||
| 43 | $this->attributes = new OptionMap(); |
||
| 44 | 1170 | } |
|
| 45 | |||
| 46 | public function getOptions(): OptionMap |
||
| 47 | 1444 | { |
|
| 48 | return $this->options; |
||
| 49 | 1444 | } |
|
| 50 | |||
| 51 | 1444 | public function getAttributes(): OptionMap |
|
| 52 | { |
||
| 53 | return $this->attributes; |
||
| 54 | 1254 | } |
|
| 55 | |||
| 56 | 1254 | /** |
|
| 57 | * @return non-empty-string |
||
|
0 ignored issues
–
show
|
|||
| 58 | 1254 | */ |
|
| 59 | public function getType(): string |
||
| 60 | { |
||
| 61 | 1386 | if (empty($this->type)) { |
|
| 62 | throw new FieldException('Field type must be set'); |
||
| 63 | 1386 | } |
|
| 64 | |||
| 65 | return $this->type; |
||
| 66 | 1474 | } |
|
| 67 | |||
| 68 | 1474 | /** |
|
| 69 | * @param non-empty-string $type |
||
|
0 ignored issues
–
show
|
|||
| 70 | 1474 | */ |
|
| 71 | public function setType(string $type): self |
||
| 72 | { |
||
| 73 | $this->type = $type; |
||
| 74 | |||
| 75 | return $this; |
||
| 76 | 972 | } |
|
| 77 | |||
| 78 | 972 | public function setPrimary(bool $primary): self |
|
| 79 | 2 | { |
|
| 80 | $this->primary = $primary; |
||
| 81 | |||
| 82 | 970 | return $this; |
|
| 83 | } |
||
| 84 | |||
| 85 | 958 | public function isPrimary(): bool |
|
| 86 | { |
||
| 87 | 958 | return $this->primary || in_array($this->type, ['primary', 'bigPrimary', 'smallPrimary']); |
|
| 88 | } |
||
| 89 | 958 | ||
| 90 | /** |
||
| 91 | * @param non-empty-string $column |
||
|
0 ignored issues
–
show
|
|||
| 92 | 788 | */ |
|
| 93 | public function setColumn(string $column): self |
||
| 94 | 788 | { |
|
| 95 | $this->column = $column; |
||
| 96 | |||
| 97 | 958 | return $this; |
|
| 98 | } |
||
| 99 | 958 | ||
| 100 | /** |
||
| 101 | * @return non-empty-string |
||
|
0 ignored issues
–
show
|
|||
| 102 | 952 | * @throws FieldException |
|
| 103 | * |
||
| 104 | 952 | */ |
|
| 105 | public function getColumn(): string |
||
| 106 | 952 | { |
|
| 107 | if (empty($this->column)) { |
||
| 108 | throw new FieldException('Column mapping must be set'); |
||
| 109 | 796 | } |
|
| 110 | |||
| 111 | 796 | return $this->column; |
|
| 112 | } |
||
| 113 | |||
| 114 | 2 | /** |
|
| 115 | * @param callable-array|string|null $typecast |
||
|
0 ignored issues
–
show
|
|||
| 116 | 2 | */ |
|
| 117 | public function setTypecast(array|string|null $typecast): self |
||
| 118 | { |
||
| 119 | 952 | $this->typecast = $typecast; |
|
| 120 | |||
| 121 | 952 | return $this; |
|
| 122 | } |
||
| 123 | 952 | ||
| 124 | public function hasTypecast(): bool |
||
| 125 | { |
||
| 126 | return $this->typecast !== null; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return callable-array|string|null |
||
|
0 ignored issues
–
show
|
|||
| 131 | */ |
||
| 132 | public function getTypecast(): array|string|null |
||
| 133 | { |
||
| 134 | return $this->typecast; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param int|null $type Generating type {@see GeneratedField*} constants. |
||
| 139 | */ |
||
| 140 | public function setGenerated(int|null $type): self |
||
| 141 | { |
||
| 142 | $this->generated = $type; |
||
| 143 | |||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function getGenerated(): ?int |
||
| 148 | { |
||
| 149 | return $this->generated; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function setReferenced(bool $indexed): self |
||
| 153 | { |
||
| 154 | $this->referenced = $indexed; |
||
| 155 | |||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function isReferenced(): bool |
||
| 160 | { |
||
| 161 | return $this->referenced; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function getEntityClass(): ?string |
||
| 165 | { |
||
| 166 | return $this->entityClass; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function setEntityClass(?string $entityClass): self |
||
| 170 | { |
||
| 171 | $this->entityClass = $entityClass; |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function __clone() |
||
| 177 | { |
||
| 178 | $this->options = clone $this->options; |
||
| 179 | $this->attributes = clone $this->attributes; |
||
| 180 | } |
||
| 181 | } |
||
| 182 |