Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add fluent interface for the dimensions rule #15852

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
131 changes: 131 additions & 0 deletions src/Illuminate/Validation/Rules/Dimensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Illuminate\Validation\Rules;

class Dimensions
{
/**
* The constraints for the dimensions rule.
*
* @var array
*/
protected $constraints = [];

/**
* Create a new dimensions rule instance.
*
* @param array $constraints;
* @return void
*/
public function __construct(array $constraints = [])
{
$this->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);
}
}
22 changes: 22 additions & 0 deletions tests/Validation/ValidationDimensionsRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Dimensions;

class ValidationDimensionsRuleTest extends PHPUnit_Framework_TestCase
{
public function testItCorrectlyFormatsAStringVersionOfTheRule()
{
$rule = new Dimensions(['min_width' => 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);
}
}