Skip to content

Commit 99f1182

Browse files
Feature/indexable columns (#47)
* Support indexes on fields (#43) * Apply fixes from StyleCI [ci skip] [skip ci]
1 parent 8c3359f commit 99f1182

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

src/Builders/Field.php

+46-19
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ class Field implements Buildable
5050
/**
5151
* @var FieldBuilder
5252
*/
53-
protected $builder;
53+
protected $fieldBuilder;
54+
55+
/**
56+
* @var ClassMetadataBuilder
57+
*/
58+
protected $metaDatabuilder;
5459

5560
/**
5661
* @var ClassMetadataInfo
@@ -70,15 +75,16 @@ class Field implements Buildable
7075
/**
7176
* Protected constructor to force usage of factory method.
7277
*
73-
* @param FieldBuilder $builder
74-
* @param ClassMetadataInfo $classMetadata
75-
* @param Type $type
76-
* @param string $name
78+
* @param FieldBuilder $fieldBuilder
79+
* @param ClassMetadataBuilder $builder
80+
* @param Type $type
81+
* @param string $name
7782
*/
78-
protected function __construct(FieldBuilder $builder, ClassMetadataInfo $classMetadata, Type $type, $name)
83+
protected function __construct(FieldBuilder $fieldBuilder, ClassMetadataBuilder $builder, Type $type, $name)
7984
{
80-
$this->builder = $builder;
81-
$this->classMetadata = $classMetadata;
85+
$this->fieldBuilder = $fieldBuilder;
86+
$this->metaDatabuilder = $builder;
87+
$this->classMetadata = $builder->getClassMetadata();
8288
$this->type = $type;
8389
$this->name = $name;
8490
}
@@ -98,7 +104,7 @@ public static function make(ClassMetadataBuilder $builder, $type, $name)
98104

99105
$field = $builder->createField($name, $type->getName());
100106

101-
return new static($field, $builder->getClassMetadata(), $type, $name);
107+
return new static($field, $builder, $type, $name);
102108
}
103109

104110
/**
@@ -157,7 +163,7 @@ public function autoIncrement()
157163
*/
158164
public function generatedValue(callable $callback = null)
159165
{
160-
$generatedValue = new GeneratedValue($this->builder, $this->classMetadata);
166+
$generatedValue = new GeneratedValue($this->fieldBuilder, $this->classMetadata);
161167

162168
if ($callback) {
163169
$callback($generatedValue);
@@ -176,7 +182,7 @@ public function generatedValue(callable $callback = null)
176182
*/
177183
public function unsigned()
178184
{
179-
$this->builder->option('unsigned', true);
185+
$this->fieldBuilder->option('unsigned', true);
180186

181187
return $this;
182188
}
@@ -191,7 +197,7 @@ public function unsigned()
191197
*/
192198
public function fixed($fixed)
193199
{
194-
$this->builder->option('fixed', $fixed);
200+
$this->fieldBuilder->option('fixed', $fixed);
195201

196202
return $this;
197203
}
@@ -205,7 +211,7 @@ public function fixed($fixed)
205211
*/
206212
public function comment($comment)
207213
{
208-
$this->builder->option('comment', $comment);
214+
$this->fieldBuilder->option('comment', $comment);
209215

210216
return $this;
211217
}
@@ -219,7 +225,7 @@ public function comment($comment)
219225
*/
220226
public function collation($collation)
221227
{
222-
$this->builder->option('collation', $collation);
228+
$this->fieldBuilder->option('collation', $collation);
223229

224230
return $this;
225231
}
@@ -229,7 +235,28 @@ public function collation($collation)
229235
*/
230236
public function primary()
231237
{
232-
$this->builder->makePrimaryKey();
238+
$this->fieldBuilder->makePrimaryKey();
239+
240+
return $this;
241+
}
242+
243+
/**
244+
* @param string|null $name
245+
*
246+
* @return Field
247+
*/
248+
public function index($name = null)
249+
{
250+
$index = new Index(
251+
$this->metaDatabuilder,
252+
[$this->getName()]
253+
);
254+
255+
if ($name !== null) {
256+
$index->name($name);
257+
}
258+
259+
$this->callbackAndQueue($index);
233260

234261
return $this;
235262
}
@@ -239,7 +266,7 @@ public function primary()
239266
*/
240267
public function useForVersioning()
241268
{
242-
$this->builder->isVersionField();
269+
$this->fieldBuilder->isVersionField();
243270

244271
return $this;
245272
}
@@ -249,7 +276,7 @@ public function useForVersioning()
249276
*/
250277
public function build()
251278
{
252-
$this->builder->build();
279+
$this->fieldBuilder->build();
253280

254281
$this->buildQueued();
255282

@@ -261,7 +288,7 @@ public function build()
261288
*/
262289
public function getBuilder()
263290
{
264-
return $this->builder;
291+
return $this->fieldBuilder;
265292
}
266293

267294
/**
@@ -303,7 +330,7 @@ public function __call($method, $args)
303330
*/
304331
protected function setDefault($default)
305332
{
306-
$this->builder->option('default', $default);
333+
$this->fieldBuilder->option('default', $default);
307334

308335
return $this;
309336
}

tests/Builders/FieldTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
3030
protected function setUp()
3131
{
3232
$this->builder = new ClassMetadataBuilder(new ClassMetadataInfo(StubEntity::class));
33+
$this->builder->setTable('stub_entities');
3334

3435
$this->field = Field::make($this->builder, 'string', 'name');
3536
}
@@ -48,6 +49,32 @@ public function test_can_make_field_nullable()
4849
$this->assertTrue($this->builder->getClassMetadata()->getFieldMapping('name')['nullable']);
4950
}
5051

52+
public function test_can_make_field_index()
53+
{
54+
$this->field->index();
55+
56+
$this->field->build();
57+
58+
$indexes = $this->builder->getClassMetadata()->table['indexes'];
59+
60+
$this->assertArrayHasKey('stub_entities_name_index', $indexes);
61+
$this->assertCount(1, $indexes['stub_entities_name_index']['columns']);
62+
$this->assertContains('name', $indexes['stub_entities_name_index']['columns']);
63+
}
64+
65+
public function test_can_make_field_index_with_custom_name()
66+
{
67+
$this->field->index('index_name');
68+
69+
$this->field->build();
70+
71+
$indexes = $this->builder->getClassMetadata()->table['indexes'];
72+
73+
$this->assertArrayHasKey('index_name', $indexes);
74+
$this->assertCount(1, $indexes['index_name']['columns']);
75+
$this->assertContains('name', $indexes['index_name']['columns']);
76+
}
77+
5178
public function test_can_set_column_name()
5279
{
5380
$this->field->columnName('name_column');

0 commit comments

Comments
 (0)