Passed
Pull Request — 1.1 (#48)
by Patrick
03:58
created

Field::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace LaravelDoctrine\Fluent\Builders;
4
5
use BadMethodCallException;
6
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
9
use Doctrine\ORM\Mapping\Builder\FieldBuilder;
10
use Doctrine\ORM\Mapping\ClassMetadataInfo;
11
use LaravelDoctrine\Fluent\Buildable;
12
use LaravelDoctrine\Fluent\Builders\Traits\Macroable;
13
use LaravelDoctrine\Fluent\Builders\Traits\Queueable;
14
use LaravelDoctrine\Fluent\Builders\Traits\QueuesMacros;
15
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata;
16
use LaravelDoctrine\Fluent\Extensions\Gedmo\GedmoFieldHints;
17
18
/**
19
 * @method $this unique(boolean $flag = true)   Boolean value to determine if the value of the column should be unique
20
 *                                              across all rows of the underlying entities table.
21
 * @method $this nullable(boolean $flag = true) Determines if NULL values are allowed for this column.
22
 * @method $this length(int $length)            Used by the “string” type to determine its maximum length in the
23
 *                                              database. Doctrine does not validate the length of string values
24
 *                                              for you.
25
 * @method $this columnName(string $column)     By default the property name is used for the database column name also,
26
 *                                              however the ‘name’ attribute allows you to determine the column name.
27
 * @method $this precision(int $precision)      The precision for a decimal (exact numeric) column (applies only for
28
 *                                              decimal column), which is the maximum number of digits that are stored
29
 *                                              for the values.
30
 * @method $this scale(int $scale)              The scale for a decimal (exact numeric) column (applies only for
31
 *                                              decimal column), which represents the number of digits to the right of
32
 *                                              the decimal point and must not be greater than precision.
33
 * @method $this default(string $default)       The default value to set for the column if no value is supplied.
34
 * @method $this columnDefinition(string $def)  DDL SQL snippet that starts after the column name and specifies the
35
 *                                              complete (non-portable!) column definition. This attribute allows to
36
 *                                              make use of advanced RMDBS features. However you should make careful
37
 *                                              use of this feature and the consequences. SchemaTool will not detect
38
 *                                              changes on the column correctly anymore if you use “columnDefinition”.
39
 * @method $this option($name, $value)          Set custom options
40
 */
41
class Field implements Buildable
42
{
43
    use Macroable;
44
    use Queueable {
45
        build as buildQueued;
46
    }
47
    use QueuesMacros;
48
    use GedmoFieldHints;
49
50
    /**
51
     * @var FieldBuilder
52
     */
53
    protected $fieldBuilder;
54
55
    /**
56
     * @var ClassMetadataBuilder
57
     */
58
    protected $metaDatabuilder;
59
60
    /**
61
     * @var ClassMetadataInfo
62
     */
63
    protected $classMetadata;
64
65
    /**
66
     * @var Type
67
     */
68
    protected $type;
69
70
    /**
71
     * @var string
72
     */
73
    protected $name;
74
75
    /**
76
     * Protected constructor to force usage of factory method.
77
     *
78
     * @param FieldBuilder         $fieldBuilder
79
     * @param ClassMetadataBuilder $builder
80
     * @param Type                 $type
81
     * @param string               $name
82
     */
83 301
    protected function __construct(FieldBuilder $fieldBuilder, ClassMetadataBuilder $builder, Type $type, $name)
84
    {
85 301
        $this->fieldBuilder = $fieldBuilder;
86 301
        $this->metaDatabuilder = $builder;
87 301
        $this->classMetadata = $builder->getClassMetadata();
88 301
        $this->type = $type;
89 301
        $this->name = $name;
90 301
    }
91
92
    /**
93
     * @param ClassMetadataBuilder $builder
94
     * @param string               $type
95
     * @param string               $name
96
     *
97
     * @throws \Doctrine\DBAL\DBALException
98
     *
99
     * @return Field
100
     */
101 301
    public static function make(ClassMetadataBuilder $builder, $type, $name)
102
    {
103 301
        $type = Type::getType($type);
104
105 301
        $field = $builder->createField($name, $type->getName());
106
107 301
        return new static($field, $builder, $type, $name);
108
    }
109
110
    /**
111
     * @return string
112
     */
113 106
    public function getName()
114
    {
115 106
        return $this->name;
116
    }
117
118
    /**
119
     * @return Type
120
     */
121 1
    public function getType()
122
    {
123 1
        return $this->type;
124
    }
125
126
    /**
127
     * @return ClassMetadata|ExtensibleClassMetadata
128
     */
129 104
    public function getClassMetadata()
130
    {
131 104
        return $this->classMetadata;
132
    }
133
134
    /**
135
     * By default the property name is used for the database column name also, however the ‘name’ attribute
136
     * allows you to determine the column name.
137
     *
138
     * @param string $columnName
139
     *
140
     * @return $this
141
     */
142 6
    public function name($columnName)
143
    {
144 6
        $this->columnName($columnName);
145
146 6
        return $this;
147
    }
148
149
    /**
150
     * @return Field
151
     */
152 8
    public function autoIncrement()
153
    {
154 8
        $this->generatedValue();
155
156 8
        return $this;
157
    }
158
159
    /**
160
     * @param callable|null $callback
161
     *
162
     * @return Field
163
     */
164 13
    public function generatedValue(callable $callback = null)
165
    {
166 11
        $generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata);
167
168 11
        if ($callback) {
169 2
            $callback($generatedValue);
170 2
        }
171
172 11
        $generatedValue->build();
173
174 13
        return $this;
175
    }
176
177
    /**
178
     * Boolean value to determine if the column should be capable of representing only non-negative integers
179
     * (applies only for integer column and might not be supported by all vendors).
180
     *
181
     * @return Field
182
     */
183 11
    public function unsigned()
184
    {
185 11
        $this->fieldBuilder->option('unsigned', true);
186
187 11
        return $this;
188
    }
189
190
    /**
191
     * Boolean value to determine if the specified length of a string column should be fixed or varying
192
     * (applies only for string/binary column and might not be supported by all vendors).
193
     *
194
     * @param bool $fixed
195
     *
196
     * @return Field
197
     */
198 1
    public function fixed($fixed)
199
    {
200 1
        $this->fieldBuilder->option('fixed', $fixed);
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * The comment of the column in the schema (might not be supported by all vendors).
207
     *
208
     * @param string $comment
209
     *
210
     * @return Field
211
     */
212 1
    public function comment($comment)
213
    {
214 1
        $this->fieldBuilder->option('comment', $comment);
215
216 1
        return $this;
217
    }
218
219
    /**
220
     * The collation of the column (only supported by Drizzle, Mysql, PostgreSQL>=9.1, Sqlite and SQLServer).
221
     *
222
     * @param string $collation
223
     *
224
     * @return Field
225
     */
226 1
    public function collation($collation)
227
    {
228 1
        $this->fieldBuilder->option('collation', $collation);
229
230 1
        return $this;
231
    }
232
233
    /**
234
     * @return Field
235
     */
236 9
    public function primary()
237
    {
238 9
        $this->fieldBuilder->makePrimaryKey();
239
240 9
        return $this;
241
    }
242
243
    /**
244
     * @param string|null $name
245
     *
246
     * @return Field
247
     */
248 2
    public function index($name = null)
249
    {
250 2
        $index = new Index(
251 2
            $this->metaDatabuilder,
252 2
            [$this->getName()]
253 2
        );
254
255 2
        if ($name !== null) {
256 1
            $index->name($name);
257 1
        }
258
259 2
        $this->callbackAndQueue($index);
260
261 2
        return $this;
262
    }
263
264
    /**
265
     * @return Field
266
     */
267 21
    public function useForVersioning()
268
    {
269 21
        $this->fieldBuilder->isVersionField();
270
271 21
        return $this;
272
    }
273
274
    /**
275
     * @return Field
276
     */
277 192
    public function build()
278
    {
279 192
        $this->fieldBuilder->build();
280
281 176
        $this->buildQueued();
282
283 175
        return $this;
284
    }
285
286
    /**
287
     * @return FieldBuilder
288
     */
289 69
    public function getBuilder()
290
    {
291 69
        return $this->fieldBuilder;
292
    }
293
294
    /**
295
     * Magic call method works as a proxy for the Doctrine FieldBuilder.
296
     *
297
     * @param string $method
298
     * @param array  $args
299
     *
300
     * @throws BadMethodCallException
301
     *
302
     * @return $this
303
     */
304 164
    public function __call($method, $args)
305
    {
306
        // Work around reserved keywords
307 164
        if ($method === 'default') {
308 1
            return call_user_func_array([$this, 'setDefault'], $args);
309
        }
310
311 163
        if ($this->hasMacro($method)) {
312 108
            return $this->queueMacro($method, $args);
313
        }
314
315 62
        if (method_exists($this->getBuilder(), $method)) {
316 61
            call_user_func_array([$this->getBuilder(), $method], $args);
317
318 61
            return $this;
319
        }
320
321 1
        throw new BadMethodCallException("FieldBuilder method [{$method}] does not exist.");
322
    }
323
324
    /**
325
     * The default value to set for the column if no value is supplied.
326
     *
327
     * @param string $default
328
     *
329
     * @return Field
330
     */
331 1
    protected function setDefault($default)
332
    {
333 1
        $this->fieldBuilder->option('default', $default);
334
335 1
        return $this;
336
    }
337
}
338