1 | <?php |
||
2 | |||
3 | namespace Bdf\Prime\Schema\Builder; |
||
4 | |||
5 | use Bdf\Prime\Platform\PlatformTypeInterface; |
||
6 | use Bdf\Prime\Schema\Bag\Column; |
||
7 | use Bdf\Prime\Schema\ColumnInterface; |
||
8 | use Bdf\Prime\Schema\IndexInterface; |
||
9 | |||
10 | /** |
||
11 | * Class ColumnBuilder |
||
12 | * Used internally by @see TableBuilder |
||
13 | * This class must not be used (or declared) manually |
||
14 | */ |
||
15 | final class ColumnBuilder implements ColumnBuilderInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $name; |
||
21 | |||
22 | /** |
||
23 | * @var PlatformTypeInterface |
||
24 | */ |
||
25 | private $type; |
||
26 | |||
27 | /** |
||
28 | * @var mixed |
||
29 | */ |
||
30 | private $defaultValue; |
||
31 | |||
32 | /** |
||
33 | * @var int|null |
||
34 | */ |
||
35 | private $length; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | private $autoIncrement = false; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $unsigned = false; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $fixed = false; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $nillable = false; |
||
56 | |||
57 | /** |
||
58 | * @var string|null |
||
59 | */ |
||
60 | private $comment; |
||
61 | |||
62 | /** |
||
63 | * @var int|null |
||
64 | */ |
||
65 | private $precision; |
||
66 | |||
67 | /** |
||
68 | * @var int|null |
||
69 | */ |
||
70 | private $scale; |
||
71 | |||
72 | /** |
||
73 | * @var IndexInterface::TYPE_*[] |
||
74 | */ |
||
75 | private $indexes = []; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | private $options = []; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * ColumnBuilder constructor. |
||
85 | * |
||
86 | * @param string $name |
||
87 | * @param PlatformTypeInterface $type |
||
88 | * @param array $options |
||
89 | */ |
||
90 | 792 | public function __construct(string $name, PlatformTypeInterface $type, array $options = []) |
|
91 | { |
||
92 | 792 | $this->name = $name; |
|
93 | 792 | $this->type = $type; |
|
94 | 792 | $this->options = $options; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | 145 | public function autoincrement(bool $flag = true) |
|
101 | { |
||
102 | 145 | $this->autoIncrement = $flag; |
|
103 | |||
104 | 145 | return $this; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 220 | public function length(?int $length) |
|
111 | { |
||
112 | 220 | $this->length = $length; |
|
113 | |||
114 | 220 | return $this; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 4 | public function comment(?string $comment) |
|
121 | { |
||
122 | 4 | $this->comment = $comment; |
|
123 | |||
124 | 4 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 248 | public function setDefault($value) |
|
131 | { |
||
132 | 248 | $this->defaultValue = $value; |
|
133 | |||
134 | 248 | return $this; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | 6 | public function precision(?int $precision, ?int $scale = 0) |
|
141 | { |
||
142 | 6 | $this->precision = $precision; |
|
143 | 6 | $this->scale = $scale; |
|
144 | |||
145 | 6 | return $this; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 144 | public function nillable(bool $flag = true) |
|
152 | { |
||
153 | 144 | $this->nillable = $flag; |
|
154 | |||
155 | 144 | return $this; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 12 | public function unsigned(bool $flag = true) |
|
162 | { |
||
163 | 12 | $this->unsigned = $flag; |
|
164 | |||
165 | 12 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 3 | public function unique($index = true) |
|
172 | { |
||
173 | 3 | if (is_string($index)) { |
|
174 | 2 | $this->indexes[$index] = IndexInterface::TYPE_UNIQUE; |
|
175 | } else { |
||
176 | 2 | $this->indexes[] = IndexInterface::TYPE_UNIQUE; |
|
177 | } |
||
178 | |||
179 | 3 | return $this; |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 4 | public function fixed(bool $flag = true) |
|
186 | { |
||
187 | 4 | $this->fixed = $flag; |
|
188 | |||
189 | 4 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 1 | public function name(string $name) |
|
196 | { |
||
197 | 1 | $this->name = $name; |
|
198 | |||
199 | 1 | return $this; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 4 | public function options(array $options) |
|
206 | { |
||
207 | 4 | $this->options = $options; |
|
208 | |||
209 | 4 | return $this; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 1 | public function type(PlatformTypeInterface $type) |
|
216 | { |
||
217 | 1 | $this->type = $type; |
|
218 | |||
219 | 1 | return $this; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | 788 | public function build(): ColumnInterface |
|
226 | { |
||
227 | 788 | return new Column( |
|
228 | 788 | $this->name, |
|
229 | 788 | $this->type, |
|
230 | // #16653 : The value is converted to DB value, because the defaultValue is a PHP value and may be imcompatible with DB value (boolean is an integer on SQL) |
||
231 | 788 | $this->type->toDatabase($this->defaultValue), |
|
232 | 788 | $this->length, |
|
233 | 788 | $this->autoIncrement, |
|
234 | 788 | $this->unsigned, |
|
235 | 788 | $this->fixed, |
|
236 | 788 | $this->nillable, |
|
237 | 788 | $this->comment, |
|
238 | 788 | $this->precision, |
|
239 | 788 | $this->scale, |
|
240 | 788 | $this->options |
|
241 | 788 | ); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 750 | public function indexes(): array |
|
248 | { |
||
249 | 750 | return $this->indexes; |
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | 3 | public function getName(): string |
|
256 | { |
||
257 | 3 | return $this->name; |
|
258 | } |
||
259 | } |
||
260 |