-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fluent interface for the dimensions rule
- Loading branch information
Showing
3 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |