From d6c14bbc064a157b6b57a0d4b15114db6c536055 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Mon, 10 Oct 2016 22:41:11 +0100 Subject: [PATCH] Add fluent interface for the dimensions rule --- src/Illuminate/Validation/Rule.php | 11 ++ .../Validation/Rules/Dimensions.php | 131 ++++++++++++++++++ .../ValidationDimensionsRuleTest.php | 22 +++ 3 files changed, 164 insertions(+) create mode 100644 src/Illuminate/Validation/Rules/Dimensions.php create mode 100644 tests/Validation/ValidationDimensionsRuleTest.php diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index 726156da59f0..4212c3888815 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -27,4 +27,15 @@ public static function unique($table, $column = 'NULL') { return new Rules\Unique($table, $column); } + + /** + * Get a dimensions constraint builder instance. + * + * @param array $constraints + * @return \Illuminate\Validation\Rules\Dimensions + */ + public static function dimensions(array $constraints = []) + { + return new Rules\Dimensions($constraints); + } } diff --git a/src/Illuminate/Validation/Rules/Dimensions.php b/src/Illuminate/Validation/Rules/Dimensions.php new file mode 100644 index 000000000000..81bf964631d1 --- /dev/null +++ b/src/Illuminate/Validation/Rules/Dimensions.php @@ -0,0 +1,131 @@ +constraints = $constraints; + } + + /** + * Set the "width" constraint. + * + * @param int $value + * @return $this + */ + public function width($value) + { + $this->constraints['width'] = $value; + + return $this; + } + + /** + * Set the "height" constraint. + * + * @param int $value + * @return $this + */ + public function height($value) + { + $this->constraints['height'] = $value; + + return $this; + } + + /** + * Set the "min width" constraint. + * + * @param int $value + * @return $this + */ + public function minWidth($value) + { + $this->constraints['min_width'] = $value; + + return $this; + } + + /** + * Set the "min height" constraint. + * + * @param int $value + * @return $this + */ + public function minHeight($value) + { + $this->constraints['min_height'] = $value; + + return $this; + } + + /** + * Set the "max width" constraint. + * + * @param int $value + * @return $this + */ + public function maxWidth($value) + { + $this->constraints['max_width'] = $value; + + return $this; + } + + /** + * Set the "max height" constraint. + * + * @param int $value + * @return $this + */ + public function maxHeight($value) + { + $this->constraints['max_height'] = $value; + + return $this; + } + + /** + * Set the "ratio" constraint. + * + * @param float $value + * @return $this + */ + public function ratio($value) + { + $this->constraints['ratio'] = $value; + + return $this; + } + + /** + * Convert the rule to a validation string. + * + * @return string + */ + public function __toString() + { + $result = ""; + + foreach ($this->constraints as $key => $value) { + $result .= "$key=$value,"; + } + + return 'dimensions:'.substr($result, 0, -1); + } +} diff --git a/tests/Validation/ValidationDimensionsRuleTest.php b/tests/Validation/ValidationDimensionsRuleTest.php new file mode 100644 index 000000000000..f58ec8ef2e94 --- /dev/null +++ b/tests/Validation/ValidationDimensionsRuleTest.php @@ -0,0 +1,22 @@ + 100, 'min_height' => 100]); + + $this->assertEquals('dimensions:min_width=100,min_height=100', (string) $rule); + + $rule = Rule::dimensions()->width(200)->height(100); + + $this->assertEquals('dimensions:width=200,height=100', (string) $rule); + + $rule = Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3/2); + + $this->assertEquals('dimensions:max_width=1000,max_height=500,ratio=1.5', (string) $rule); + } +}