Completed
Pull Request — master (#5883)
by Sebastian
19:21
created

FieldBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 20
c 1
b 1
f 0
lcom 1
cbo 2
dl 0
loc 267
ccs 45
cts 60
cp 0.75
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A length() 0 6 1
A nullable() 0 6 1
A unique() 0 6 1
A columnName() 0 6 1
A precision() 0 6 1
A scale() 0 6 1
A makePrimaryKey() 0 6 1
A option() 0 6 1
A generatedValue() 0 6 1
A isVersionField() 0 6 1
A setSequenceGenerator() 0 10 1
A columnDefinition() 0 6 1
A __construct() 0 5 1
A setCustomIdGenerator() 0 6 1
B build() 0 22 5
A isPrimaryKey() 0 4 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sequenceDef of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
287
            $cm->setSequenceGeneratorDefinition($this->sequenceDef);
288
        }
289
290 5
        if ($this->customIdGenerator) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->customIdGenerator of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
291 1
            $cm->setCustomGeneratorDefinition(['class' => $this->customIdGenerator]);
292
        }
293
294 5
        return $this->builder;
295
    }
296
}
297