1 | <?php |
||
19 | class RecordSchema implements SchemaInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var ReflectionEntity |
||
23 | */ |
||
24 | private $reflection; |
||
25 | |||
26 | /** |
||
27 | * @invisible |
||
28 | * @var MutatorsConfig |
||
29 | */ |
||
30 | private $mutatorsConfig; |
||
31 | |||
32 | /** |
||
33 | * @var ColumnRenderer |
||
34 | */ |
||
35 | private $renderer; |
||
36 | |||
37 | /** |
||
38 | * @param ReflectionEntity $reflection |
||
39 | * @param MutatorsConfig $mutators |
||
40 | * @param ColumnRenderer|null $rendered |
||
41 | */ |
||
42 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | public function getClass(): string |
||
59 | |||
60 | /** |
||
61 | * @return ReflectionEntity |
||
62 | */ |
||
63 | public function getReflection(): ReflectionEntity |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function getInstantiator(): string |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function getDatabase() |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function getTable(): string |
||
104 | |||
105 | /** |
||
106 | * Returns set of declared indexes. |
||
107 | * |
||
108 | * Example: |
||
109 | * const INDEXES = [ |
||
110 | * [self::UNIQUE, 'email'], |
||
111 | * [self::INDEX, 'status', 'balance'], |
||
112 | * [self::INDEX, 'public_id'] |
||
113 | * ]; |
||
114 | * |
||
115 | * @do generator |
||
116 | * |
||
117 | * @return \Generator|IndexDefinition[] |
||
118 | * |
||
119 | * @throws DefinitionException |
||
120 | */ |
||
121 | public function getIndexes(): \Generator |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | public function renderTable(AbstractTable $table): AbstractTable |
||
134 | { |
||
135 | return $this->renderer->renderColumns( |
||
136 | $this->getFields(), |
||
137 | $this->createDefaults(), |
||
138 | $table |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function getRelations(): array |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function packSchema(SchemaBuilder $builder, AbstractTable $table = null): array |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | protected function getFields(): array |
||
162 | { |
||
163 | $fields = $this->reflection->getSchema(); |
||
164 | |||
165 | foreach ($fields as $field => $type) { |
||
166 | if ($this->isRelation($type)) { |
||
167 | unset($fields[$field]); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $fields; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Check if field schema/type defines relation. |
||
176 | * |
||
177 | * @param mixed $type |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | protected function isRelation($type): bool |
||
189 | |||
190 | /** |
||
191 | * @param array $definition |
||
192 | * |
||
193 | * @return IndexDefinition |
||
194 | * |
||
195 | * @throws DefinitionException |
||
196 | */ |
||
197 | protected function castIndex(array $definition) |
||
225 | |||
226 | /** |
||
227 | * Default defined values. |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | protected function createDefaults(): array |
||
236 | } |