Skip to content

Commit

Permalink
Add fluent interface for the dimensions rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sileence committed Oct 10, 2016
1 parent ea7b79b commit d6c14bb
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
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);
}
}

0 comments on commit d6c14bb

Please sign in to comment.