-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It also provide a way to define namespaces/prefixes to use the custom rules on Respect\Validation.
- Loading branch information
1 parent
3e46726
commit a4cb208
Showing
4 changed files
with
180 additions
and
11 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,49 @@ | ||
<?php | ||
|
||
namespace Respect\Validation; | ||
|
||
use ReflectionClass; | ||
use Respect\Validation\Exceptions\ComponentException; | ||
|
||
class Factory | ||
{ | ||
protected $rulePrefixes = array('Respect\\Validation\\Rules\\'); | ||
|
||
public function getRulePrefixes() | ||
{ | ||
return $this->rulePrefixes; | ||
} | ||
|
||
public function appendRulePrefix($rulePrefix) | ||
{ | ||
array_push($this->rulePrefixes, $rulePrefix); | ||
} | ||
|
||
public function prependRulePrefix($rulePrefix) | ||
{ | ||
array_unshift($this->rulePrefixes, $rulePrefix); | ||
} | ||
|
||
public function rule($ruleName, array $arguments = array()) | ||
{ | ||
if ($ruleName instanceof Validatable) { | ||
return $ruleName; | ||
} | ||
|
||
foreach ($this->getRulePrefixes() as $prefix) { | ||
$className = $prefix.ucfirst($ruleName); | ||
if (! class_exists($className)) { | ||
continue; | ||
} | ||
|
||
$reflection = new ReflectionClass($className); | ||
if (! $reflection->isSubclassOf('Respect\\Validation\\Validatable')) { | ||
throw new ComponentException(sprintf('"%s" is not a valid respect rule', $className)); | ||
} | ||
|
||
return $reflection->newInstanceArgs($arguments); | ||
} | ||
|
||
throw new ComponentException(sprintf('"%s" is not a valid rule name', $ruleName)); | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
namespace Respect\Validation; | ||
|
||
/** | ||
* @covers Respect\Validation\Factory | ||
*/ | ||
class FactoryTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testShouldHaveRulePrefixesByDefault() | ||
{ | ||
$factory = new Factory(); | ||
|
||
$this->assertEquals(array('Respect\\Validation\\Rules\\'), $factory->getRulePrefixes()); | ||
} | ||
|
||
public function testShouldBeAbleToAppendANewPrefix() | ||
{ | ||
$factory = new Factory(); | ||
$factory->appendRulePrefix('My\\Validation\\Rules\\'); | ||
|
||
$this->assertEquals(array('Respect\\Validation\\Rules\\', 'My\\Validation\\Rules\\'), $factory->getRulePrefixes()); | ||
} | ||
|
||
public function testShouldBeAbleToPrependANewRulePrefix() | ||
{ | ||
$factory = new Factory(); | ||
$factory->prependRulePrefix('My\\Validation\\Rules\\'); | ||
|
||
$this->assertEquals(array('My\\Validation\\Rules\\', 'Respect\\Validation\\Rules\\'), $factory->getRulePrefixes()); | ||
} | ||
|
||
public function testShouldCreateARuleByName() | ||
{ | ||
$factory = new Factory(); | ||
|
||
$this->assertInstanceOf('Respect\\Validation\\Rules\\Uppercase', $factory->rule('uppercase')); | ||
} | ||
|
||
public function testShouldDefineConstructorArgumentsWhenCreatingARule() | ||
{ | ||
$factory = new Factory(); | ||
$rule = $factory->rule('date', array('Y-m-d')); | ||
|
||
$this->assertEquals('Y-m-d', $rule->format); | ||
} | ||
|
||
/** | ||
* @expectedException Respect\Validation\Exceptions\ComponentException | ||
* @expectedExceptionMessage "uterere" is not a valid rule name | ||
*/ | ||
public function testShouldThrowsAnExceptionWhenRuleNameIsNotValid() | ||
{ | ||
$factory = new Factory(); | ||
$factory->rule('uterere'); | ||
} | ||
|
||
/** | ||
* @expectedException Respect\Validation\Exceptions\ComponentException | ||
* @expectedExceptionMessage "Respect\Validation\TestNonRule" is not a valid respect rule | ||
*/ | ||
public function testShouldThrowsAnExceptionWhenRuleIsNotInstanceOfRuleInterface() | ||
{ | ||
$factory = new Factory(); | ||
$factory->appendRulePrefix('Respect\\Validation\\Test'); | ||
$factory->rule('nonRule'); | ||
} | ||
} | ||
|
||
class TestNonRule | ||
{ | ||
} |