From ae82160c0e9ab398095f6a037c7311c54116a29f Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Sat, 2 May 2020 14:59:25 -0300 Subject: [PATCH 1/2] Between rule for numeric types --- src/Translators/Rules.php | 41 ++++++ tests/Feature/Translators/RulesTest.php | 182 ++++++++++++++++++++++++ 2 files changed, 223 insertions(+) diff --git a/src/Translators/Rules.php b/src/Translators/Rules.php index 350385d7..8c72b1e7 100644 --- a/src/Translators/Rules.php +++ b/src/Translators/Rules.php @@ -80,6 +80,18 @@ public static function fromColumn(string $context, Column $column) array_push($rules, 'unique:' . $context . ',' . $column->name()); } + if (Str::contains($column->dataType(), 'decimal')) { + array_push($rules, self::betweenRuleForNumericTypes($column, 'decimal')); + } + + if (Str::contains($column->dataType(), 'float')) { + array_push($rules, self::betweenRuleForNumericTypes($column, 'float')); + } + + if (Str::contains($column->dataType(), 'double')) { + array_push($rules, self::betweenRuleForNumericTypes($column, 'double')); + } + return $rules; } @@ -94,4 +106,33 @@ private static function overrideStringRuleForSpecialNames($name) return 'string'; } + + + private static function betweenRuleForNumericTypes(Column $column, $numericType) + { + $defaultPrecisionAndScale = [8, 2]; + $parameters = explode(",", Str::between($column->dataType(), "$numericType:", " ")); + + if (count($parameters) === 1) { + [$precision, $scale] = $defaultPrecisionAndScale; + } else { + [$precision, $scale] = $parameters; + } + + $max = substr_replace(str_pad("", $precision, '9'), ".", $precision - $scale, 0); + $min = "-" . $max; + + if (intval($scale) === 0) { + $min = trim($min, "."); + $max = trim($max, "."); + } + + if (Str::contains($column->dataType(), 'unsigned')) { + $min = '0'; + } + + $betweenRule = 'between:' . $min . ',' . $max; + + return $betweenRule; + } } diff --git a/tests/Feature/Translators/RulesTest.php b/tests/Feature/Translators/RulesTest.php index bb4f96ff..2fb587ce 100644 --- a/tests/Feature/Translators/RulesTest.php +++ b/tests/Feature/Translators/RulesTest.php @@ -168,6 +168,156 @@ public function forColumn_return_json_rule_for_the_json_type() $this->assertContains('json', Rules::fromColumn('context', $column)); } + /** + * @test + */ + public function forColumn_return_default_values_rule_for_decimal() + { + $column = new Column('column', "decimal"); + + $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + } + + /** + * @test + */ + public function forColumn_return_default_values_rule_for_unsigned_decimal() + { + $unsignedBeforeDecimalColumn = new Column('column', "unsigned decimal"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeDecimalColumn)); + + $unsignedAfterDecimalColumn = new Column('column', "decimal unsigned"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterDecimalColumn)); + } + + /** + * @test + * @dataProvider numericDataProvider + */ + public function forColumn_return_between_rule_for_decimal($precision, $scale, $interval) + { + $column = new Column('column', "decimal:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $column)); + } + + /** + * @test + * @dataProvider unsignedNumericDataProvider + */ + public function forColumn_return_between_rule_for_unsigned_decimal($precision, $scale, $interval) + { + $unsignedBeforeDecimalColumn = new Column('column', "unsigned decimal:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeDecimalColumn)); + + $unsignedAfterDecimalColumn = new Column('column', "decimal:$precision,$scale unsigned"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterDecimalColumn)); + } + + /** + * @test + */ + public function forColumn_return_default_values_rule_for_float() + { + $column = new Column('column', "float"); + + $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + } + + /** + * @test + */ + public function forColumn_return_default_values_rule_for_unsigned_float() + { + $unsignedBeforeFloatColumn = new Column('column', "unsigned float"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeFloatColumn)); + + $unsignedAfterFloatColumn = new Column('column', "float unsigned"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterFloatColumn)); + } + + /** + * @test + * @dataProvider numericDataProvider + */ + public function forColumn_return_between_rule_for_float($precision, $scale, $interval) + { + $column = new Column('column', "float:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $column)); + } + + /** + * @test + * @dataProvider unsignedNumericDataProvider + */ + public function forColumn_return_between_rule_for_unsigned_float($precision, $scale, $interval) + { + $unsignedBeforeFloatColumn = new Column('column', "unsigned float:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeFloatColumn)); + + $unsignedAfterFloatColumn = new Column('column', "float:$precision,$scale unsigned"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterFloatColumn)); + } + + /** + * @test + */ + public function forColumn_return_default_values_rule_for_double() + { + $column = new Column('column', "double"); + + $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + } + + /** + * @test + */ + public function forColumn_return_default_values_rule_for_unsigned_double() + { + $unsignedBeforeDoubleColumn = new Column('column', "unsigned double"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeDoubleColumn)); + + $unsignedAfterDoubleColumn = new Column('column', "double unsigned"); + + $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterDoubleColumn)); + } + + /** + * @test + * @dataProvider numericDataProvider + */ + public function forColumn_return_between_rule_for_double($precision, $scale, $interval) + { + $column = new Column('column', "double:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $column)); + } + + /** + * @test + * @dataProvider unsignedNumericDataProvider + */ + public function forColumn_return_between_rule_for_unsigned_double($precision, $scale, $interval) + { + $unsignedBeforeDoubleColumn = new Column('column', "unsigned double:$precision,$scale"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedBeforeDoubleColumn)); + + $unsignedAfterDoubleColumn = new Column('column', "double:$precision,$scale unsigned"); + + $this->assertContains("between:$interval", Rules::fromColumn('context', $unsignedAfterDoubleColumn)); + } + public function stringDataTypesProvider() { return [ @@ -225,4 +375,36 @@ public function relationshipColumnProvider() ['sheep_id', 'sheep'], ]; } + + public function numericDataProvider() + { + return [ + ['10', '0', '-9999999999,9999999999'], + ['10', '1', '-999999999.9,999999999.9'], + ['10', '2', '-99999999.99,99999999.99'], + ['10', '3', '-9999999.999,9999999.999'], + ['10', '4', '-999999.9999,999999.9999'], + ['10', '5', '-99999.99999,99999.99999'], + ['10', '6', '-9999.999999,9999.999999'], + ['10', '7', '-999.9999999,999.9999999'], + ['10', '8', '-99.99999999,99.99999999'], + ['10', '9', '-9.999999999,9.999999999'], + ]; + } + + public function unsignedNumericDataProvider() + { + return [ + ['10', '0', '0,9999999999'], + ['10', '1', '0,999999999.9'], + ['10', '2', '0,99999999.99'], + ['10', '3', '0,9999999.999'], + ['10', '4', '0,999999.9999'], + ['10', '5', '0,99999.99999'], + ['10', '6', '0,9999.999999'], + ['10', '7', '0,999.9999999'], + ['10', '8', '0,99.99999999'], + ['10', '9', '0,9.999999999'], + ]; + } } From 0301e288dea7576701dce86c13f6850e208f2cea Mon Sep 17 00:00:00 2001 From: Gianluca Bine Date: Sat, 2 May 2020 15:39:14 -0300 Subject: [PATCH 2/2] Removed default values for between rule --- src/Translators/Rules.php | 7 +++--- tests/Feature/Translators/RulesTest.php | 30 ++++++++++++------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/Translators/Rules.php b/src/Translators/Rules.php index 8c72b1e7..27763dda 100644 --- a/src/Translators/Rules.php +++ b/src/Translators/Rules.php @@ -110,15 +110,14 @@ private static function overrideStringRuleForSpecialNames($name) private static function betweenRuleForNumericTypes(Column $column, $numericType) { - $defaultPrecisionAndScale = [8, 2]; $parameters = explode(",", Str::between($column->dataType(), "$numericType:", " ")); if (count($parameters) === 1) { - [$precision, $scale] = $defaultPrecisionAndScale; - } else { - [$precision, $scale] = $parameters; + return; } + [$precision, $scale] = $parameters; + $max = substr_replace(str_pad("", $precision, '9'), ".", $precision - $scale, 0); $min = "-" . $max; diff --git a/tests/Feature/Translators/RulesTest.php b/tests/Feature/Translators/RulesTest.php index 2fb587ce..b8b18d64 100644 --- a/tests/Feature/Translators/RulesTest.php +++ b/tests/Feature/Translators/RulesTest.php @@ -171,25 +171,25 @@ public function forColumn_return_json_rule_for_the_json_type() /** * @test */ - public function forColumn_return_default_values_rule_for_decimal() + public function forColumn_does_not_return_between_rule_for_decimal_without_precion_and_scale() { $column = new Column('column', "decimal"); - $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + $this->assertNotContains("between", Rules::fromColumn('context', $column)); } /** * @test */ - public function forColumn_return_default_values_rule_for_unsigned_decimal() + public function forColumn_does_not_return_between_rule_for_unsigned_decimal_without_precion_and_scale() { $unsignedBeforeDecimalColumn = new Column('column', "unsigned decimal"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeDecimalColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeDecimalColumn)); $unsignedAfterDecimalColumn = new Column('column', "decimal unsigned"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterDecimalColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterDecimalColumn)); } /** @@ -221,25 +221,25 @@ public function forColumn_return_between_rule_for_unsigned_decimal($precision, $ /** * @test */ - public function forColumn_return_default_values_rule_for_float() + public function forColumn_does_not_return_between_rule_for_float_without_precion_and_scale() { $column = new Column('column', "float"); - $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + $this->assertNotContains("between", Rules::fromColumn('context', $column)); } /** * @test */ - public function forColumn_return_default_values_rule_for_unsigned_float() + public function forColumn_does_not_return_between_rule_for_unsigned_float_without_precion_and_scale() { $unsignedBeforeFloatColumn = new Column('column', "unsigned float"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeFloatColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeFloatColumn)); $unsignedAfterFloatColumn = new Column('column', "float unsigned"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterFloatColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterFloatColumn)); } /** @@ -271,25 +271,25 @@ public function forColumn_return_between_rule_for_unsigned_float($precision, $sc /** * @test */ - public function forColumn_return_default_values_rule_for_double() + public function forColumn_does_not_return_between_rule_for_double_without_precion_and_scale() { $column = new Column('column', "double"); - $this->assertContains("between:-999999.99,999999.99", Rules::fromColumn('context', $column)); + $this->assertNotContains("between", Rules::fromColumn('context', $column)); } /** * @test */ - public function forColumn_return_default_values_rule_for_unsigned_double() + public function forColumn_does_not_return_between_rule_for_unsigned_double_without_precion_and_scale() { $unsignedBeforeDoubleColumn = new Column('column', "unsigned double"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedBeforeDoubleColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedBeforeDoubleColumn)); $unsignedAfterDoubleColumn = new Column('column', "double unsigned"); - $this->assertContains("between:0,999999.99", Rules::fromColumn('context', $unsignedAfterDoubleColumn)); + $this->assertNotContains("between", Rules::fromColumn('context', $unsignedAfterDoubleColumn)); } /**