1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\ORM\Mapping\Builder; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Field Builder |
24
|
|
|
* |
25
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
26
|
|
|
* @link www.doctrine-project.com |
27
|
|
|
* @since 2.2 |
28
|
|
|
* @author Benjamin Eberlei <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class FieldBuilder |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var ClassMetadataBuilder |
34
|
|
|
*/ |
35
|
|
|
private $builder; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $mapping; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $version; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $generatedValue; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $sequenceDef; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
private $customIdGenerator; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ClassMetadataBuilder $builder |
64
|
|
|
* @param array $mapping |
65
|
|
|
*/ |
66
|
5 |
|
public function __construct(ClassMetadataBuilder $builder, array $mapping) |
67
|
|
|
{ |
68
|
5 |
|
$this->builder = $builder; |
69
|
5 |
|
$this->mapping = $mapping; |
70
|
5 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Sets length. |
74
|
|
|
* |
75
|
|
|
* @param int $length |
76
|
|
|
* |
77
|
|
|
* @return FieldBuilder |
78
|
|
|
*/ |
79
|
1 |
|
public function length($length) |
80
|
|
|
{ |
81
|
1 |
|
$this->mapping['length'] = $length; |
82
|
|
|
|
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets nullable. |
88
|
|
|
* |
89
|
|
|
* @param bool $flag |
90
|
|
|
* |
91
|
|
|
* @return FieldBuilder |
92
|
|
|
*/ |
93
|
1 |
|
public function nullable($flag = true) |
94
|
|
|
{ |
95
|
1 |
|
$this->mapping['nullable'] = (bool) $flag; |
96
|
|
|
|
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Sets Unique. |
102
|
|
|
* |
103
|
|
|
* @param bool $flag |
104
|
|
|
* |
105
|
|
|
* @return FieldBuilder |
106
|
|
|
*/ |
107
|
1 |
|
public function unique($flag = true) |
108
|
|
|
{ |
109
|
1 |
|
$this->mapping['unique'] = (bool) $flag; |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Sets column name. |
116
|
|
|
* |
117
|
|
|
* @param string $name |
118
|
|
|
* |
119
|
|
|
* @return FieldBuilder |
120
|
|
|
*/ |
121
|
1 |
|
public function columnName($name) |
122
|
|
|
{ |
123
|
1 |
|
$this->mapping['columnName'] = $name; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets Precision. |
130
|
|
|
* |
131
|
|
|
* @param int $p |
132
|
|
|
* |
133
|
|
|
* @return FieldBuilder |
134
|
|
|
*/ |
135
|
|
|
public function precision($p) |
136
|
|
|
{ |
137
|
|
|
$this->mapping['precision'] = $p; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets scale. |
144
|
|
|
* |
145
|
|
|
* @param int $s |
146
|
|
|
* |
147
|
|
|
* @return FieldBuilder |
148
|
|
|
*/ |
149
|
|
|
public function scale($s) |
150
|
|
|
{ |
151
|
|
|
$this->mapping['scale'] = $s; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Sets field as primary key. |
158
|
|
|
* |
159
|
|
|
* @deprecated Use makePrimaryKey() instead |
160
|
|
|
* @return FieldBuilder |
161
|
|
|
*/ |
162
|
|
|
public function isPrimaryKey() |
163
|
|
|
{ |
164
|
|
|
return $this->makePrimaryKey(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Sets field as primary key. |
169
|
|
|
* |
170
|
|
|
* @return FieldBuilder |
171
|
|
|
*/ |
172
|
1 |
|
public function makePrimaryKey() |
173
|
|
|
{ |
174
|
1 |
|
$this->mapping['id'] = true; |
175
|
|
|
|
176
|
1 |
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets an option. |
181
|
|
|
* |
182
|
|
|
* @param string $name |
183
|
|
|
* @param mixed $value |
184
|
|
|
* |
185
|
|
|
* @return FieldBuilder |
186
|
|
|
*/ |
187
|
1 |
|
public function option($name, $value) |
188
|
|
|
{ |
189
|
1 |
|
$this->mapping['options'][$name] = $value; |
190
|
|
|
|
191
|
1 |
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $strategy |
196
|
|
|
* |
197
|
|
|
* @return FieldBuilder |
198
|
|
|
*/ |
199
|
2 |
|
public function generatedValue($strategy = 'AUTO') |
200
|
|
|
{ |
201
|
2 |
|
$this->generatedValue = $strategy; |
202
|
|
|
|
203
|
2 |
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sets field versioned. |
208
|
|
|
* |
209
|
|
|
* @return FieldBuilder |
210
|
|
|
*/ |
211
|
1 |
|
public function isVersionField() |
212
|
|
|
{ |
213
|
1 |
|
$this->version = true; |
214
|
|
|
|
215
|
1 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Sets Sequence Generator. |
220
|
|
|
* |
221
|
|
|
* @param string $sequenceName |
222
|
|
|
* @param int $allocationSize |
223
|
|
|
* @param int $initialValue |
224
|
|
|
* |
225
|
|
|
* @return FieldBuilder |
226
|
|
|
*/ |
227
|
|
|
public function setSequenceGenerator($sequenceName, $allocationSize = 1, $initialValue = 1) |
228
|
|
|
{ |
229
|
|
|
$this->sequenceDef = array( |
230
|
|
|
'sequenceName' => $sequenceName, |
231
|
|
|
'allocationSize' => $allocationSize, |
232
|
|
|
'initialValue' => $initialValue, |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Sets column definition. |
240
|
|
|
* |
241
|
|
|
* @param string $def |
242
|
|
|
* |
243
|
|
|
* @return FieldBuilder |
244
|
|
|
*/ |
245
|
1 |
|
public function columnDefinition($def) |
246
|
|
|
{ |
247
|
1 |
|
$this->mapping['columnDefinition'] = $def; |
248
|
|
|
|
249
|
1 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Set the FQCN of the custom ID generator. |
254
|
|
|
* This class must extend \Doctrine\ORM\Id\AbstractIdGenerator. |
255
|
|
|
* |
256
|
|
|
* @param string $customIdGenerator |
257
|
|
|
* |
258
|
|
|
* @return $this |
259
|
|
|
*/ |
260
|
1 |
|
public function setCustomIdGenerator($customIdGenerator) |
261
|
|
|
{ |
262
|
1 |
|
$this->customIdGenerator = (string) $customIdGenerator; |
263
|
|
|
|
264
|
1 |
|
return $this; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Finalizes this field and attach it to the ClassMetadata. |
269
|
|
|
* |
270
|
|
|
* Without this call a FieldBuilder has no effect on the ClassMetadata. |
271
|
|
|
* |
272
|
|
|
* @return ClassMetadataBuilder |
273
|
|
|
*/ |
274
|
5 |
|
public function build() |
275
|
|
|
{ |
276
|
5 |
|
$cm = $this->builder->getClassMetadata(); |
277
|
5 |
|
if ($this->generatedValue) { |
278
|
2 |
|
$cm->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_' . $this->generatedValue)); |
279
|
|
|
} |
280
|
|
|
|
281
|
5 |
|
if ($this->version) { |
282
|
1 |
|
$cm->setVersionMapping($this->mapping); |
283
|
|
|
} |
284
|
|
|
|
285
|
5 |
|
$cm->mapField($this->mapping); |
286
|
5 |
|
if ($this->sequenceDef) { |
|
|
|
|
287
|
|
|
$cm->setSequenceGeneratorDefinition($this->sequenceDef); |
288
|
|
|
} |
289
|
|
|
|
290
|
5 |
|
if ($this->customIdGenerator) { |
|
|
|
|
291
|
1 |
|
$cm->setCustomGeneratorDefinition(['class' => $this->customIdGenerator]); |
292
|
|
|
} |
293
|
|
|
|
294
|
5 |
|
return $this->builder; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.