Skip to content

Commit 0952428

Browse files
authored
Add between rule for numeric types (#174)
1 parent 30917cc commit 0952428

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed

src/Translators/Rules.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public static function fromColumn(string $context, Column $column)
8080
array_push($rules, 'unique:' . $context . ',' . $column->name());
8181
}
8282

83+
if (Str::contains($column->dataType(), 'decimal')) {
84+
array_push($rules, self::betweenRuleForNumericTypes($column, 'decimal'));
85+
}
86+
87+
if (Str::contains($column->dataType(), 'float')) {
88+
array_push($rules, self::betweenRuleForNumericTypes($column, 'float'));
89+
}
90+
91+
if (Str::contains($column->dataType(), 'double')) {
92+
array_push($rules, self::betweenRuleForNumericTypes($column, 'double'));
93+
}
94+
8395
return $rules;
8496
}
8597

@@ -94,4 +106,32 @@ private static function overrideStringRuleForSpecialNames($name)
94106

95107
return 'string';
96108
}
109+
110+
111+
private static function betweenRuleForNumericTypes(Column $column, $numericType)
112+
{
113+
$parameters = explode(",", Str::between($column->dataType(), "$numericType:", " "));
114+
115+
if (count($parameters) === 1) {
116+
return;
117+
}
118+
119+
[$precision, $scale] = $parameters;
120+
121+
$max = substr_replace(str_pad("", $precision, '9'), ".", $precision - $scale, 0);
122+
$min = "-" . $max;
123+
124+
if (intval($scale) === 0) {
125+
$min = trim($min, ".");
126+
$max = trim($max, ".");
127+
}
128+
129+
if (Str::contains($column->dataType(), 'unsigned')) {
130+
$min = '0';
131+
}
132+
133+
$betweenRule = 'between:' . $min . ',' . $max;
134+
135+
return $betweenRule;
136+
}
97137
}

tests/Feature/Translators/RulesTest.php

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,156 @@ public function forColumn_return_json_rule_for_the_json_type()
168168
$this->assertContains('json', Rules::fromColumn('context', $column));
169169
}
170170

171+
/**
172+
* @test
173+
*/
174+
public function forColumn_does_not_return_between_rule_for_decimal_without_precion_and_scale()
175+
{
176+
$column = new Column('column', "decimal");
177+
178+
$this->assertNotContains("between", Rules::fromColumn('context', $column));
179+
}
180+
181+
/**
182+
* @test
183+
*/
184+
public function forColumn_does_not_return_between_rule_for_unsigned_decimal_without_precion_and_scale()
185+
{
186+
$unsignedBeforeDecimalColumn = new Column('column', "unsigned decimal");
187+
188+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeDecimalColumn));
189+
190+
$unsignedAfterDecimalColumn = new Column('column', "decimal unsigned");
191+
192+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterDecimalColumn));
193+
}
194+
195+
/**
196+
* @test
197+
* @dataProvider numericDataProvider
198+
*/
199+
public function forColumn_return_between_rule_for_decimal($precision, $scale, $interval)
200+
{
201+
$column = new Column('column', "decimal:$precision,$scale");
202+
203+
$this->assertContains("between:$interval", Rules::fromColumn('context', $column));
204+
}
205+
206+
/**
207+
* @test
208+
* @dataProvider unsignedNumericDataProvider
209+
*/
210+
public function forColumn_return_between_rule_for_unsigned_decimal($precision, $scale, $interval)
211+
{
212+
$unsignedBeforeDecimalColumn = new Column('column', "unsigned decimal:$precision,$scale");
213+
214+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeDecimalColumn));
215+
216+
$unsignedAfterDecimalColumn = new Column('column', "decimal:$precision,$scale unsigned");
217+
218+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterDecimalColumn));
219+
}
220+
221+
/**
222+
* @test
223+
*/
224+
public function forColumn_does_not_return_between_rule_for_float_without_precion_and_scale()
225+
{
226+
$column = new Column('column', "float");
227+
228+
$this->assertNotContains("between", Rules::fromColumn('context', $column));
229+
}
230+
231+
/**
232+
* @test
233+
*/
234+
public function forColumn_does_not_return_between_rule_for_unsigned_float_without_precion_and_scale()
235+
{
236+
$unsignedBeforeFloatColumn = new Column('column', "unsigned float");
237+
238+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeFloatColumn));
239+
240+
$unsignedAfterFloatColumn = new Column('column', "float unsigned");
241+
242+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterFloatColumn));
243+
}
244+
245+
/**
246+
* @test
247+
* @dataProvider numericDataProvider
248+
*/
249+
public function forColumn_return_between_rule_for_float($precision, $scale, $interval)
250+
{
251+
$column = new Column('column', "float:$precision,$scale");
252+
253+
$this->assertContains("between:$interval", Rules::fromColumn('context', $column));
254+
}
255+
256+
/**
257+
* @test
258+
* @dataProvider unsignedNumericDataProvider
259+
*/
260+
public function forColumn_return_between_rule_for_unsigned_float($precision, $scale, $interval)
261+
{
262+
$unsignedBeforeFloatColumn = new Column('column', "unsigned float:$precision,$scale");
263+
264+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeFloatColumn));
265+
266+
$unsignedAfterFloatColumn = new Column('column', "float:$precision,$scale unsigned");
267+
268+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterFloatColumn));
269+
}
270+
271+
/**
272+
* @test
273+
*/
274+
public function forColumn_does_not_return_between_rule_for_double_without_precion_and_scale()
275+
{
276+
$column = new Column('column', "double");
277+
278+
$this->assertNotContains("between", Rules::fromColumn('context', $column));
279+
}
280+
281+
/**
282+
* @test
283+
*/
284+
public function forColumn_does_not_return_between_rule_for_unsigned_double_without_precion_and_scale()
285+
{
286+
$unsignedBeforeDoubleColumn = new Column('column', "unsigned double");
287+
288+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeDoubleColumn));
289+
290+
$unsignedAfterDoubleColumn = new Column('column', "double unsigned");
291+
292+
$this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterDoubleColumn));
293+
}
294+
295+
/**
296+
* @test
297+
* @dataProvider numericDataProvider
298+
*/
299+
public function forColumn_return_between_rule_for_double($precision, $scale, $interval)
300+
{
301+
$column = new Column('column', "double:$precision,$scale");
302+
303+
$this->assertContains("between:$interval", Rules::fromColumn('context', $column));
304+
}
305+
306+
/**
307+
* @test
308+
* @dataProvider unsignedNumericDataProvider
309+
*/
310+
public function forColumn_return_between_rule_for_unsigned_double($precision, $scale, $interval)
311+
{
312+
$unsignedBeforeDoubleColumn = new Column('column', "unsigned double:$precision,$scale");
313+
314+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeDoubleColumn));
315+
316+
$unsignedAfterDoubleColumn = new Column('column', "double:$precision,$scale unsigned");
317+
318+
$this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterDoubleColumn));
319+
}
320+
171321
public function stringDataTypesProvider()
172322
{
173323
return [
@@ -225,4 +375,36 @@ public function relationshipColumnProvider()
225375
['sheep_id', 'sheep'],
226376
];
227377
}
378+
379+
public function numericDataProvider()
380+
{
381+
return [
382+
['10', '0', '-9999999999,9999999999'],
383+
['10', '1', '-999999999.9,999999999.9'],
384+
['10', '2', '-99999999.99,99999999.99'],
385+
['10', '3', '-9999999.999,9999999.999'],
386+
['10', '4', '-999999.9999,999999.9999'],
387+
['10', '5', '-99999.99999,99999.99999'],
388+
['10', '6', '-9999.999999,9999.999999'],
389+
['10', '7', '-999.9999999,999.9999999'],
390+
['10', '8', '-99.99999999,99.99999999'],
391+
['10', '9', '-9.999999999,9.999999999'],
392+
];
393+
}
394+
395+
public function unsignedNumericDataProvider()
396+
{
397+
return [
398+
['10', '0', '0,9999999999'],
399+
['10', '1', '0,999999999.9'],
400+
['10', '2', '0,99999999.99'],
401+
['10', '3', '0,9999999.999'],
402+
['10', '4', '0,999999.9999'],
403+
['10', '5', '0,99999.99999'],
404+
['10', '6', '0,9999.999999'],
405+
['10', '7', '0,999.9999999'],
406+
['10', '8', '0,99.99999999'],
407+
['10', '9', '0,9.999999999'],
408+
];
409+
}
228410
}

0 commit comments

Comments
 (0)