Skip to content

Commit 69b1ad1

Browse files
Mark Salmonjasonmccreary
authored andcommitted
Adds unique modifier and fixes attributes problem
1 parent e6f8c94 commit 69b1ad1

File tree

6 files changed

+60
-10
lines changed

6 files changed

+60
-10
lines changed

src/Lexers/ModelLexer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class ModelLexer implements Lexer
7979
'unsigned' => 'unsigned',
8080
'usecurrent' => 'useCurrent',
8181
'always' => 'always',
82+
'unique' => 'unique',
8283
];
8384

8485
public function analyze(array $tokens): array
@@ -137,27 +138,27 @@ private function buildColumn(string $name, string $definition)
137138
foreach ($tokens as $token) {
138139
$parts = explode(':', $token);
139140
$value = $parts[0];
140-
$attributes = $parts[1] ?? null;
141141

142142
if ($value === 'id') {
143143
$data_type = 'id';
144144
} elseif (isset(self::$dataTypes[strtolower($value)])) {
145+
$attributes = $parts[1] ?? null;
145146
$data_type = self::$dataTypes[strtolower($value)];
146147
if (!empty($attributes)) {
147148
$attributes = explode(',', $attributes);
148149
}
149150
}
150151

151152
if (isset(self::$modifiers[strtolower($value)])) {
152-
if (empty($attributes)) {
153+
$modifierAttributes = $parts[1] ?? null;
154+
if (empty($modifierAttributes)) {
153155
$modifiers[] = self::$modifiers[strtolower($value)];
154156
} else {
155-
$modifiers[] = [self::$modifiers[strtolower($value)] => $attributes];
156-
$attributes = [];
157+
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes];
157158
}
158159
}
159160
}
160161

161162
return new Column($name, $data_type, $modifiers, $attributes ?? []);
162163
}
163-
}
164+
}

src/Translators/Rules.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Rules
99
{
10-
public static function fromColumn(Column $column)
10+
public static function fromColumn(Column $column, string $context = null)
1111
{
1212
// TODO: what about nullable?
1313
$rules = ['required'];
@@ -75,6 +75,10 @@ public static function fromColumn(Column $column)
7575
}
7676
}
7777

78+
if (in_array('unique', $column->modifiers()) && $context) {
79+
$rules = array_merge($rules, ['unique:' . $context]);
80+
}
81+
7882
return $rules;
7983
}
8084

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ public function it_handles_modifier_attributes($definition, $modifier, $attribut
289289
$this->assertEquals([[$modifier => $attributes], 'nullable'], $columns['column']->modifiers());
290290
}
291291

292+
/**
293+
* @test
294+
*/
295+
public function it_handles_attributes_and_modifiers_with_attributes()
296+
{
297+
$tokens = [
298+
'models' => [
299+
'Model' => [
300+
'column' => 'string:100 unique charset:utf8'
301+
]
302+
],
303+
];
304+
305+
$actual = $this->subject->analyze($tokens)['models']['Model']->columns()['column'];
306+
307+
$this->assertEquals('column', $actual->name());
308+
$this->assertEquals('string', $actual->dataType());
309+
$this->assertEquals(['unique', ['charset' => 'utf8']], $actual->modifiers());
310+
$this->assertEquals(['100'], $actual->attributes());
311+
}
312+
313+
292314
/**
293315
* @test
294316
*/
@@ -345,4 +367,4 @@ public function modifierAttributesProvider()
345367
['collation:utf8_unicode', 'collation', 'utf8_unicode'],
346368
];
347369
}
348-
}
370+
}

tests/Feature/Translators/RulesTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@ public function forColumn_returns_date_rule_for_date_types($data_type)
132132
$this->assertContains('date', Rules::fromColumn($column));
133133
}
134134

135+
/**
136+
* @test
137+
* @dataProvider stringDataTypesProvider
138+
*/
139+
public function forColumn_does_not_return_unique_rule_for_the_unique_modifier_without_context($data_type)
140+
{
141+
$column = new Column('test', $data_type, ['unique', 'nullable']);
142+
143+
$this->assertNotContains('unique:', Rules::fromColumn($column));
144+
}
145+
146+
/**
147+
* @test
148+
*/
149+
public function forColumn_returns_unique_rule_for_the_unique_modifier()
150+
{
151+
$column = new Column('test', 'string', ['unique'], [100]);
152+
153+
$actual = Rules::fromColumn($column, 'connection.table');
154+
155+
$this->assertContains('unique:connection.table', $actual);
156+
$this->assertContains('max:100', $actual);
157+
}
158+
135159
public function stringDataTypesProvider()
136160
{
137161
return [
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
models:
22
Modifier:
33
title: string nullable
4-
name: string:1000
4+
name: string:1000 unique charset:'utf8'
55
content: string default:''
66
total: decimal:10,2
77
ssn: char:11
88
role: enum:user,admin,owner
9-

tests/fixtures/migrations/modifiers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function up()
1616
Schema::create('modifiers', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('title')->nullable();
19-
$table->string('name', 1000);
19+
$table->string('name', 1000)->unique()->charset('utf8');
2020
$table->string('content')->default('');
2121
$table->decimal('total', 10, 2);
2222
$table->char('ssn', 11);

0 commit comments

Comments
 (0)