Skip to content

Commit

Permalink
Merge pull request #68 from rakit/improve-rules-check-method
Browse files Browse the repository at this point in the history
Rule::check must return bool
  • Loading branch information
emsifa authored Nov 19, 2018
2 parents 4dc604c + a2076fc commit 12d06ff
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Rule
/** @var string */
protected $message = "The :attribute is invalid";

abstract public function check($value);
abstract public function check($value): bool;

/**
* Set Validation class instance
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class Defaults extends Rule
* Check the $value is valid
*
* @param mixed $value
* @return mixed
* @return bool
*/
public function check($value)
public function check($value): bool
{
$this->requireParameters($this->fillableParams);

$default = $this->parameter('default');
return $default;
return true;
}
}
2 changes: 1 addition & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function validateAttribute(Attribute $attribute)
$ruleValidator->setAttribute($attribute);

if ($isEmptyValue && $ruleValidator instanceof Defaults) {
$value = $ruleValidator->check(null);
$value = $ruleValidator->parameter('default');
$isEmptyValue = $this->isEmptyValue($value);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Even.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Even extends Rule

protected $message = "The :attribute must be even";

public function check($value)
public function check($value): bool
{
if (! is_numeric($value)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Required.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Required extends Rule
{

public function check($value)
public function check($value): bool
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/Rules/DefaultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

class DefaultsTest extends TestCase
{

public function setUp()
{
$this->rule = new Defaults;
}

public function testDefaults()
{
$this->assertEquals($this->rule->fillParameters([10])->check(null), 10);
$this->assertEquals($this->rule->fillParameters(['something'])->check(null), 'something');
$this->assertEquals($this->rule->fillParameters([[1,2,3]])->check('anything'), [1,2,3]);
$this->assertTrue($this->rule->fillParameters([10])->check(0));
$this->assertTrue($this->rule->fillParameters(['something'])->check(null));
$this->assertTrue($this->rule->fillParameters([[1,2,3]])->check(false));
$this->assertTrue($this->rule->fillParameters([[1,2,3]])->check([]));
}
}

0 comments on commit 12d06ff

Please sign in to comment.