|
9 | 9 | use Cycle\ORM\Entity\Behavior\Exception\BehaviorCompilationException;
|
10 | 10 | use Cycle\ORM\Parser\Typecast;
|
11 | 11 | use Cycle\ORM\Parser\TypecastInterface;
|
| 12 | +use Cycle\ORM\SchemaInterface; |
| 13 | +use Cycle\Schema\Defaults; |
12 | 14 | use Cycle\Schema\Definition\Entity;
|
13 | 15 | use Cycle\Schema\Definition\Field;
|
14 | 16 | use Cycle\Schema\Definition\Map\FieldMap;
|
@@ -39,12 +41,14 @@ class RegistryModifier
|
39 | 41 | protected FieldMap $fields;
|
40 | 42 | protected AbstractTable $table;
|
41 | 43 | protected Entity $entity;
|
| 44 | + protected Defaults $defaults; |
42 | 45 |
|
43 | 46 | public function __construct(Registry $registry, string $role)
|
44 | 47 | {
|
45 | 48 | $this->entity = $registry->getEntity($role);
|
46 | 49 | $this->fields = $this->entity->getFields();
|
47 | 50 | $this->table = $registry->getTableSchema($this->entity);
|
| 51 | + $this->defaults = $registry->getDefaults(); |
48 | 52 | }
|
49 | 53 |
|
50 | 54 | public function addDatetimeColumn(string $columnName, string $fieldName): AbstractColumn
|
@@ -135,15 +139,19 @@ public function setTypecast(Field $field, array|string|null $rule, string $handl
|
135 | 139 | $field->setTypecast($rule);
|
136 | 140 | }
|
137 | 141 |
|
138 |
| - $handlers = $this->entity->getTypecast(); |
139 |
| - if ($handlers === null) { |
140 |
| - $this->entity->setTypecast($handler); |
141 |
| - return $field; |
| 142 | + $defaultHandlers = $this->defaults[SchemaInterface::TYPECAST_HANDLER] ?? []; |
| 143 | + if (!\is_array($defaultHandlers)) { |
| 144 | + $defaultHandlers = [$defaultHandlers]; |
142 | 145 | }
|
143 | 146 |
|
144 |
| - $handlers = (array) $handlers; |
145 |
| - $handlers[] = $handler; |
146 |
| - $this->entity->setTypecast(array_unique($handlers)); |
| 147 | + $handlers = $this->entity->getTypecast() ?? []; |
| 148 | + if (!is_array($handlers)) { |
| 149 | + $handlers = [$handlers]; |
| 150 | + } |
| 151 | + |
| 152 | + if (!\in_array($handler, $handlers, true) && !\in_array($handler, $defaultHandlers, true)) { |
| 153 | + $this->entity->setTypecast(\array_merge($handlers, [$handler])); |
| 154 | + } |
147 | 155 |
|
148 | 156 | return $field;
|
149 | 157 | }
|
|
0 commit comments