Skip to content

Commit e6f8c94

Browse files
Mark Salmonjasonmccreary
authored andcommitted
Minor tweaks
1 parent c1c631b commit e6f8c94

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/Translators/Rules.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,29 @@ public static function fromColumn(Column $column)
3030
'smallInteger',
3131
'mediumInteger',
3232
'bigInteger',
33-
'decimal',
34-
'double',
35-
'float',
3633
'increments',
3734
'tinyIncrements',
3835
'smallIncrements',
3936
'mediumIncrements',
4037
'bigIncrements',
4138
'unsignedBigInteger',
42-
'unsignedDecimal',
4339
'unsignedInteger',
4440
'unsignedMediumInteger',
4541
'unsignedSmallInteger',
4642
'unsignedTinyInteger'
43+
])) {
44+
$rules = array_merge($rules, ['integer']);
45+
46+
if (Str::startsWith($column->dataType(), 'unsigned')) {
47+
$rules = array_merge($rules, ['gt:0']);
48+
}
49+
}
50+
51+
if (in_array($column->dataType(), [
52+
'decimal',
53+
'double',
54+
'float',
55+
'unsignedDecimal',
4756
])) {
4857
$rules = array_merge($rules, ['numeric']);
4958

@@ -71,16 +80,13 @@ public static function fromColumn(Column $column)
7180

7281
private static function overrideStringRuleForSpecialNames($name)
7382
{
74-
switch ($name) {
75-
case Str::startsWith($name, 'email'):
76-
return 'email';
77-
break;
78-
case $name === 'password':
79-
return 'password';
80-
break;
81-
default:
82-
return 'string';
83-
break;
83+
if (Str::startsWith($name, 'email')) {
84+
return 'email';
8485
}
86+
if ($name === 'password') {
87+
return 'password';
88+
}
89+
90+
return 'string';
8591
}
8692
}

tests/Feature/Translators/RulesTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,29 @@ public function forColumn_returns_numeric_rule_for_numeric_types($data_type)
8383
$this->assertContains('numeric', Rules::fromColumn($column));
8484
}
8585

86+
/**
87+
* @test
88+
* @dataProvider integerDataTypesProvider
89+
*/
90+
public function forColumn_returns_integer_rule_for_integer_types($data_type)
91+
{
92+
$column = new Column('test', $data_type);
93+
94+
$this->assertContains('integer', Rules::fromColumn($column));
95+
}
96+
8697
/**
8798
* @test
8899
*/
89100
public function forColumn_returns_gt0_rule_for_unsigned_numeric_types()
90101
{
91102
$column = new Column('test', 'integer');
92103

93-
$this->assertContains('numeric', Rules::fromColumn($column));
94104
$this->assertNotContains('gt:0', Rules::fromColumn($column));
95105

96106
$column = new Column('test', 'unsignedInteger');
97107

98108
$this->assertContains('gt:0', Rules::fromColumn($column));
99-
$this->assertContains('numeric', Rules::fromColumn($column));
100109
}
101110

102111
/**
@@ -132,28 +141,34 @@ public function stringDataTypesProvider()
132141
];
133142
}
134143

135-
public function numericDataTypesProvider()
144+
public function integerDataTypesProvider()
136145
{
137146
return [
138147
['integer'],
139148
['tinyInteger'],
140149
['smallInteger'],
141150
['mediumInteger'],
142151
['bigInteger'],
143-
['decimal'],
144-
['double'],
145-
['float'],
152+
['unsignedBigInteger'],
153+
['unsignedInteger'],
154+
['unsignedMediumInteger'],
155+
['unsignedSmallInteger'],
156+
['unsignedTinyInteger'],
146157
['increments'],
147158
['tinyIncrements'],
148159
['smallIncrements'],
149160
['mediumIncrements'],
150161
['bigIncrements'],
151-
['unsignedBigInteger'],
162+
];
163+
}
164+
165+
public function numericDataTypesProvider()
166+
{
167+
return [
168+
['decimal'],
169+
['double'],
170+
['float'],
152171
['unsignedDecimal'],
153-
['unsignedInteger'],
154-
['unsignedMediumInteger'],
155-
['unsignedSmallInteger'],
156-
['unsignedTinyInteger'],
157172
];
158173
}
159174

0 commit comments

Comments
 (0)