Skip to content

Commit

Permalink
Support lowercase model definition keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Nov 7, 2019
1 parent cbcd529 commit ba1e57d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 72 deletions.
144 changes: 72 additions & 72 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,76 @@
class ModelLexer implements Lexer
{
private static $dataTypes = [
'bigIncrements',
'bigInteger',
'binary',
'boolean',
'char',
'date',
'dateTime',
'dateTimeTz',
'decimal',
'double',
'enum',
'float',
'geometry',
'geometryCollection',
'increments',
'integer',
'ipAddress',
'json',
'jsonb',
'lineString',
'longText',
'macAddress',
'mediumIncrements',
'mediumInteger',
'mediumText',
'morphs',
'uuidMorphs',
'multiLineString',
'multiPoint',
'multiPolygon',
'nullableMorphs',
'nullableUuidMorphs',
'nullableTimestamps',
'point',
'polygon',
'rememberToken',
'set',
'smallIncrements',
'smallInteger',
'softDeletes',
'softDeletesTz',
'string',
'text',
'time',
'timeTz',
'timestamp',
'timestampTz',
'timestamps',
'timestampsTz',
'tinyIncrements',
'tinyInteger',
'unsignedBigInteger',
'unsignedDecimal',
'unsignedInteger',
'unsignedMediumInteger',
'unsignedSmallInteger',
'unsignedTinyInteger',
'uuid',
'year'
'bigincrements' => 'bigIncrements',
'biginteger' => 'bigInteger',
'binary' => 'binary',
'boolean' => 'boolean',
'char' => 'char',
'date' => 'date',
'datetime' => 'dateTime',
'datetimetz' => 'dateTimeTz',
'decimal' => 'decimal',
'double' => 'double',
'enum' => 'enum',
'float' => 'float',
'geometry' => 'geometry',
'geometrycollection' => 'geometryCollection',
'increments' => 'increments',
'integer' => 'integer',
'ipaddress' => 'ipAddress',
'json' => 'json',
'jsonb' => 'jsonb',
'linestring' => 'lineString',
'longtext' => 'longText',
'macaddress' => 'macAddress',
'mediumincrements' => 'mediumIncrements',
'mediuminteger' => 'mediumInteger',
'mediumtext' => 'mediumText',
'morphs' => 'morphs',
'uuidmorphs' => 'uuidMorphs',
'multilinestring' => 'multiLineString',
'multipoint' => 'multiPoint',
'multipolygon' => 'multiPolygon',
'nullablemorphs' => 'nullableMorphs',
'nullableuuidmorphs' => 'nullableUuidMorphs',
'nullabletimestamps' => 'nullableTimestamps',
'point' => 'point',
'polygon' => 'polygon',
'remembertoken' => 'rememberToken',
'set' => 'set',
'smallincrements' => 'smallIncrements',
'smallinteger' => 'smallInteger',
'softdeletes' => 'softDeletes',
'softdeletestz' => 'softDeletesTz',
'string' => 'string',
'text' => 'text',
'time' => 'time',
'timetz' => 'timeTz',
'timestamp' => 'timestamp',
'timestamptz' => 'timestampTz',
'timestamps' => 'timestamps',
'timestampstz' => 'timestampsTz',
'tinyincrements' => 'tinyIncrements',
'tinyinteger' => 'tinyInteger',
'unsignedbiginteger' => 'unsignedBigInteger',
'unsigneddecimal' => 'unsignedDecimal',
'unsignedinteger' => 'unsignedInteger',
'unsignedmediuminteger' => 'unsignedMediumInteger',
'unsignedsmallinteger' => 'unsignedSmallInteger',
'unsignedtinyinteger' => 'unsignedTinyInteger',
'uuid' => 'uuid',
'year' => 'year',
];

private static $modifiers = [
'autoIncrement',
'charset',
'collation',
'default',
'nullable',
'unsigned',
'useCurrent',
'always'
'autoincrement' => 'autoIncrement',
'charset' => 'charset',
'collation' => 'collation',
'default' => 'default',
'nullable' => 'nullable',
'unsigned' => 'unsigned',
'usecurrent' => 'useCurrent',
'always' => 'always',
];

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

if ($value === 'id') {
$data_type = 'id';
} elseif (in_array($value, self::$dataTypes)) {
$data_type = $value;
} elseif (isset(self::$dataTypes[strtolower($value)])) {
$data_type = self::$dataTypes[strtolower($value)];
if (!empty($attributes)) {
$attributes = explode(',', $attributes);
}
}

if (in_array($value, self::$modifiers)) {
if (isset(self::$modifiers[strtolower($value)])) {
if (empty($attributes)) {
$modifiers[] = $value;
$modifiers[] = self::$modifiers[strtolower($value)];
} else {
$modifiers[] = [$value => $attributes];
$modifiers[] = [self::$modifiers[strtolower($value)] => $attributes];
$attributes = [];
}
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/Lexers/ModelLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@ public function it_defaults_to_string_datatype()
$this->assertEquals(['nullable'], $columns['title']->modifiers());
}

/**
* @test
*/
public function it_accepts_lowercase_keywords()
{
$tokens = [
'models' => [
'Model' => [
'sequence' => 'unsignedbiginteger autoincrement',
'content' => 'longtext',
'saved_at' => 'timestamptz usecurrent'
]
],
];

$actual = $this->subject->analyze($tokens);

$this->assertIsArray($actual['models']);
$this->assertCount(1, $actual['models']);

$model = $actual['models']['Model'];
$this->assertEquals('Model', $model->name());
$this->assertTrue($model->usesTimestamps());

$columns = $model->columns();
$this->assertCount(4, $columns);
$this->assertEquals('id', $columns['id']->name());
$this->assertEquals('id', $columns['id']->dataType());
$this->assertEquals([], $columns['id']->attributes());
$this->assertEquals([], $columns['id']->modifiers());
$this->assertEquals('sequence', $columns['sequence']->name());
$this->assertEquals('unsignedBigInteger', $columns['sequence']->dataType());
$this->assertEquals([], $columns['sequence']->attributes());
$this->assertEquals(['autoIncrement'], $columns['sequence']->modifiers());
$this->assertEquals('content', $columns['content']->name());
$this->assertEquals('longText', $columns['content']->dataType());
$this->assertEquals([], $columns['content']->attributes());
$this->assertEquals([], $columns['content']->modifiers());
$this->assertEquals('saved_at', $columns['saved_at']->name());
$this->assertEquals('timestampTz', $columns['saved_at']->dataType());
$this->assertEquals([], $columns['saved_at']->attributes());
$this->assertEquals(['useCurrent'], $columns['saved_at']->modifiers());
}


/**
* @test
Expand Down

0 comments on commit ba1e57d

Please sign in to comment.