1 | <?php |
||
35 | class Field implements Buildable |
||
36 | { |
||
37 | /** |
||
38 | * @var FieldBuilder |
||
39 | */ |
||
40 | protected $builder; |
||
41 | |||
42 | /** |
||
43 | * @var ClassMetadataInfo |
||
44 | */ |
||
45 | protected $classMetadata; |
||
46 | |||
47 | /** |
||
48 | * Protected constructor to force usage of factory method |
||
49 | * |
||
50 | * @param FieldBuilder $builder |
||
51 | * @param ClassMetadataInfo $classMetadata |
||
52 | */ |
||
53 | 64 | protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata) |
|
54 | { |
||
55 | 64 | $this->builder = $builder; |
|
56 | 64 | $this->classMetadata = $classMetadata; |
|
57 | 64 | } |
|
58 | |||
59 | /** |
||
60 | * @param ClassMetadataBuilder $builder |
||
61 | * @param string $type |
||
62 | * @param string $name |
||
63 | * |
||
64 | * @throws \Doctrine\DBAL\DBALException |
||
65 | * @return Field |
||
66 | */ |
||
67 | 64 | public static function make(ClassMetadataBuilder $builder, $type, $name) |
|
68 | { |
||
69 | 64 | $type = Type::getType($type); |
|
70 | |||
71 | 64 | $field = $builder->createField($name, $type->getName()); |
|
72 | |||
73 | 64 | return new static($field, $builder->getClassMetadata()); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * By default the property name is used for the database column name also, however the ‘name’ attribute |
||
78 | * allows you to determine the column name. |
||
79 | * |
||
80 | * @param string $columnName |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | 6 | public function name($columnName) |
|
85 | { |
||
86 | 6 | $this->columnName($columnName); |
|
87 | |||
88 | 6 | return $this; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return Field |
||
93 | */ |
||
94 | 7 | public function autoIncrement() |
|
95 | { |
||
96 | 7 | $this->generatedValue(); |
|
97 | |||
98 | 7 | return $this; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param callable|null $callback |
||
103 | * |
||
104 | * @return Field |
||
105 | */ |
||
106 | 10 | public function generatedValue(callable $callback = null) |
|
107 | { |
||
108 | 10 | $generatedValue = new GeneratedValue($this->builder, $this->classMetadata); |
|
109 | |||
110 | 10 | if ($callback) { |
|
111 | 2 | $callback($generatedValue); |
|
112 | 2 | } |
|
113 | |||
114 | 10 | $generatedValue->build(); |
|
115 | |||
116 | 10 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Boolean value to determine if the column should be capable of representing only non-negative integers |
||
121 | * (applies only for integer column and might not be supported by all vendors). |
||
122 | * |
||
123 | * @return Field |
||
124 | */ |
||
125 | 10 | public function unsigned() |
|
126 | { |
||
127 | 10 | $this->builder->option('unsigned', true); |
|
128 | |||
129 | 10 | return $this; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Boolean value to determine if the specified length of a string column should be fixed or varying |
||
134 | * (applies only for string/binary column and might not be supported by all vendors). |
||
135 | * |
||
136 | * @param bool $fixed |
||
137 | * |
||
138 | * @return Field |
||
139 | */ |
||
140 | 1 | public function fixed($fixed) |
|
141 | { |
||
142 | 1 | $this->builder->option('fixed', $fixed); |
|
143 | |||
144 | 1 | return $this; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * The comment of the column in the schema (might not be supported by all vendors). |
||
149 | * |
||
150 | * @param string $comment |
||
151 | * |
||
152 | * @return Field |
||
153 | */ |
||
154 | 1 | public function comment($comment) |
|
155 | { |
||
156 | 1 | $this->builder->option('comment', $comment); |
|
157 | |||
158 | 1 | return $this; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * The collation of the column (only supported by Drizzle, Mysql, PostgreSQL>=9.1, Sqlite and SQLServer). |
||
163 | * |
||
164 | * @param string $collation |
||
165 | * |
||
166 | * @return Field |
||
167 | */ |
||
168 | 1 | public function collation($collation) |
|
169 | { |
||
170 | 1 | $this->builder->option('collation', $collation); |
|
171 | |||
172 | 1 | return $this; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * @return Field |
||
177 | */ |
||
178 | 8 | public function primary() |
|
179 | { |
||
180 | 8 | $this->builder->makePrimaryKey(); |
|
181 | |||
182 | 8 | return $this; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return Field |
||
187 | */ |
||
188 | 21 | public function useForVersioning() |
|
189 | { |
||
190 | 21 | $this->builder->isVersionField(); |
|
191 | |||
192 | 21 | return $this; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return Field |
||
197 | */ |
||
198 | 49 | public function build() |
|
199 | { |
||
200 | 49 | $this->builder->build(); |
|
201 | |||
202 | 33 | return $this; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return FieldBuilder |
||
207 | */ |
||
208 | 20 | public function getBuilder() |
|
212 | |||
213 | /** |
||
214 | * Magic call method works as a proxy for the Doctrine FieldBuilder |
||
215 | * |
||
216 | * @param string $method |
||
217 | * @param array $args |
||
218 | * |
||
219 | * @throws BadMethodCallException |
||
220 | * @return $this |
||
221 | */ |
||
222 | 14 | public function __call($method, $args) |
|
223 | { |
||
224 | // Work around reserved keywords |
||
237 | |||
238 | /** |
||
239 | * The default value to set for the column if no value is supplied. |
||
240 | * |
||
241 | * @param string $default |
||
242 | * |
||
243 | * @return Field |
||
244 | */ |
||
245 | 1 | protected function setDefault($default) |
|
251 | } |
||
252 |