Skip to content

Commit ba1e57d

Browse files
committed
Support lowercase model definition keywords
1 parent cbcd529 commit ba1e57d

File tree

2 files changed

+116
-72
lines changed

2 files changed

+116
-72
lines changed

src/Lexers/ModelLexer.php

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,76 @@
99
class ModelLexer implements Lexer
1010
{
1111
private static $dataTypes = [
12-
'bigIncrements',
13-
'bigInteger',
14-
'binary',
15-
'boolean',
16-
'char',
17-
'date',
18-
'dateTime',
19-
'dateTimeTz',
20-
'decimal',
21-
'double',
22-
'enum',
23-
'float',
24-
'geometry',
25-
'geometryCollection',
26-
'increments',
27-
'integer',
28-
'ipAddress',
29-
'json',
30-
'jsonb',
31-
'lineString',
32-
'longText',
33-
'macAddress',
34-
'mediumIncrements',
35-
'mediumInteger',
36-
'mediumText',
37-
'morphs',
38-
'uuidMorphs',
39-
'multiLineString',
40-
'multiPoint',
41-
'multiPolygon',
42-
'nullableMorphs',
43-
'nullableUuidMorphs',
44-
'nullableTimestamps',
45-
'point',
46-
'polygon',
47-
'rememberToken',
48-
'set',
49-
'smallIncrements',
50-
'smallInteger',
51-
'softDeletes',
52-
'softDeletesTz',
53-
'string',
54-
'text',
55-
'time',
56-
'timeTz',
57-
'timestamp',
58-
'timestampTz',
59-
'timestamps',
60-
'timestampsTz',
61-
'tinyIncrements',
62-
'tinyInteger',
63-
'unsignedBigInteger',
64-
'unsignedDecimal',
65-
'unsignedInteger',
66-
'unsignedMediumInteger',
67-
'unsignedSmallInteger',
68-
'unsignedTinyInteger',
69-
'uuid',
70-
'year'
12+
'bigincrements' => 'bigIncrements',
13+
'biginteger' => 'bigInteger',
14+
'binary' => 'binary',
15+
'boolean' => 'boolean',
16+
'char' => 'char',
17+
'date' => 'date',
18+
'datetime' => 'dateTime',
19+
'datetimetz' => 'dateTimeTz',
20+
'decimal' => 'decimal',
21+
'double' => 'double',
22+
'enum' => 'enum',
23+
'float' => 'float',
24+
'geometry' => 'geometry',
25+
'geometrycollection' => 'geometryCollection',
26+
'increments' => 'increments',
27+
'integer' => 'integer',
28+
'ipaddress' => 'ipAddress',
29+
'json' => 'json',
30+
'jsonb' => 'jsonb',
31+
'linestring' => 'lineString',
32+
'longtext' => 'longText',
33+
'macaddress' => 'macAddress',
34+
'mediumincrements' => 'mediumIncrements',
35+
'mediuminteger' => 'mediumInteger',
36+
'mediumtext' => 'mediumText',
37+
'morphs' => 'morphs',
38+
'uuidmorphs' => 'uuidMorphs',
39+
'multilinestring' => 'multiLineString',
40+
'multipoint' => 'multiPoint',
41+
'multipolygon' => 'multiPolygon',
42+
'nullablemorphs' => 'nullableMorphs',
43+
'nullableuuidmorphs' => 'nullableUuidMorphs',
44+
'nullabletimestamps' => 'nullableTimestamps',
45+
'point' => 'point',
46+
'polygon' => 'polygon',
47+
'remembertoken' => 'rememberToken',
48+
'set' => 'set',
49+
'smallincrements' => 'smallIncrements',
50+
'smallinteger' => 'smallInteger',
51+
'softdeletes' => 'softDeletes',
52+
'softdeletestz' => 'softDeletesTz',
53+
'string' => 'string',
54+
'text' => 'text',
55+
'time' => 'time',
56+
'timetz' => 'timeTz',
57+
'timestamp' => 'timestamp',
58+
'timestamptz' => 'timestampTz',
59+
'timestamps' => 'timestamps',
60+
'timestampstz' => 'timestampsTz',
61+
'tinyincrements' => 'tinyIncrements',
62+
'tinyinteger' => 'tinyInteger',
63+
'unsignedbiginteger' => 'unsignedBigInteger',
64+
'unsigneddecimal' => 'unsignedDecimal',
65+
'unsignedinteger' => 'unsignedInteger',
66+
'unsignedmediuminteger' => 'unsignedMediumInteger',
67+
'unsignedsmallinteger' => 'unsignedSmallInteger',
68+
'unsignedtinyinteger' => 'unsignedTinyInteger',
69+
'uuid' => 'uuid',
70+
'year' => 'year',
7171
];
7272

7373
private static $modifiers = [
74-
'autoIncrement',
75-
'charset',
76-
'collation',
77-
'default',
78-
'nullable',
79-
'unsigned',
80-
'useCurrent',
81-
'always'
74+
'autoincrement' => 'autoIncrement',
75+
'charset' => 'charset',
76+
'collation' => 'collation',
77+
'default' => 'default',
78+
'nullable' => 'nullable',
79+
'unsigned' => 'unsigned',
80+
'usecurrent' => 'useCurrent',
81+
'always' => 'always',
8282
];
8383

8484
public function analyze(array $tokens): array
@@ -136,18 +136,18 @@ private function buildColumn(string $name, string $definition)
136136

137137
if ($value === 'id') {
138138
$data_type = 'id';
139-
} elseif (in_array($value, self::$dataTypes)) {
140-
$data_type = $value;
139+
} elseif (isset(self::$dataTypes[strtolower($value)])) {
140+
$data_type = self::$dataTypes[strtolower($value)];
141141
if (!empty($attributes)) {
142142
$attributes = explode(',', $attributes);
143143
}
144144
}
145145

146-
if (in_array($value, self::$modifiers)) {
146+
if (isset(self::$modifiers[strtolower($value)])) {
147147
if (empty($attributes)) {
148-
$modifiers[] = $value;
148+
$modifiers[] = self::$modifiers[strtolower($value)];
149149
} else {
150-
$modifiers[] = [$value => $attributes];
150+
$modifiers[] = [self::$modifiers[strtolower($value)] => $attributes];
151151
$attributes = [];
152152
}
153153
}

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,50 @@ public function it_defaults_to_string_datatype()
168168
$this->assertEquals(['nullable'], $columns['title']->modifiers());
169169
}
170170

171+
/**
172+
* @test
173+
*/
174+
public function it_accepts_lowercase_keywords()
175+
{
176+
$tokens = [
177+
'models' => [
178+
'Model' => [
179+
'sequence' => 'unsignedbiginteger autoincrement',
180+
'content' => 'longtext',
181+
'saved_at' => 'timestamptz usecurrent'
182+
]
183+
],
184+
];
185+
186+
$actual = $this->subject->analyze($tokens);
187+
188+
$this->assertIsArray($actual['models']);
189+
$this->assertCount(1, $actual['models']);
190+
191+
$model = $actual['models']['Model'];
192+
$this->assertEquals('Model', $model->name());
193+
$this->assertTrue($model->usesTimestamps());
194+
195+
$columns = $model->columns();
196+
$this->assertCount(4, $columns);
197+
$this->assertEquals('id', $columns['id']->name());
198+
$this->assertEquals('id', $columns['id']->dataType());
199+
$this->assertEquals([], $columns['id']->attributes());
200+
$this->assertEquals([], $columns['id']->modifiers());
201+
$this->assertEquals('sequence', $columns['sequence']->name());
202+
$this->assertEquals('unsignedBigInteger', $columns['sequence']->dataType());
203+
$this->assertEquals([], $columns['sequence']->attributes());
204+
$this->assertEquals(['autoIncrement'], $columns['sequence']->modifiers());
205+
$this->assertEquals('content', $columns['content']->name());
206+
$this->assertEquals('longText', $columns['content']->dataType());
207+
$this->assertEquals([], $columns['content']->attributes());
208+
$this->assertEquals([], $columns['content']->modifiers());
209+
$this->assertEquals('saved_at', $columns['saved_at']->name());
210+
$this->assertEquals('timestampTz', $columns['saved_at']->dataType());
211+
$this->assertEquals([], $columns['saved_at']->attributes());
212+
$this->assertEquals(['useCurrent'], $columns['saved_at']->modifiers());
213+
}
214+
171215

172216
/**
173217
* @test

0 commit comments

Comments
 (0)