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..354d071c3af4 --- /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..164bf68dac12 --- /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); + } +}