-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from Maks3w/feature/constraint-factory
Add constraint factory
- Loading branch information
Showing
4 changed files
with
188 additions
and
10 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,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Constraints; | ||
|
||
use JsonSchema\Exception\InvalidArgumentException; | ||
use JsonSchema\Uri\UriRetriever; | ||
use JsonSchema\Validator; | ||
|
||
/** | ||
* Factory for centralize constraint initialization. | ||
*/ | ||
class Factory | ||
{ | ||
/** | ||
* @var UriRetriever | ||
*/ | ||
protected $uriRetriever; | ||
|
||
/** | ||
* @param UriRetriever $uriRetriever | ||
*/ | ||
public function __construct(UriRetriever $uriRetriever = null) | ||
{ | ||
if (!$uriRetriever) { | ||
$uriRetriever = new UriRetriever(); | ||
} | ||
|
||
$this->uriRetriever = $uriRetriever; | ||
} | ||
|
||
/** | ||
* @return UriRetriever | ||
*/ | ||
public function getUriRetriever() | ||
{ | ||
return $this->uriRetriever; | ||
} | ||
|
||
/** | ||
* Create a constraint instance for the given constraint name. | ||
* | ||
* @param string $constraintName | ||
* @return ConstraintInterface|ObjectConstraint | ||
* @throws InvalidArgumentException if is not possible create the constraint instance. | ||
*/ | ||
public function createInstanceFor($constraintName) | ||
{ | ||
switch ($constraintName) { | ||
case 'array': | ||
case 'collection': | ||
return new CollectionConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'object': | ||
return new ObjectConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'type': | ||
return new TypeConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'undefined': | ||
return new UndefinedConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'string': | ||
return new StringConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'number': | ||
return new NumberConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'enum': | ||
return new EnumConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'format': | ||
return new FormatConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'schema': | ||
return new SchemaConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
case 'validator': | ||
return new Validator(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this); | ||
} | ||
|
||
throw new InvalidArgumentException('Unknown constraint ' . $constraintName); | ||
} | ||
} |
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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the JsonSchema package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace JsonSchema\Tests\Constraints; | ||
|
||
use JsonSchema\Constraints\Factory; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
|
||
class FactoryTest extends TestCase | ||
{ | ||
/** | ||
* @var Factory | ||
*/ | ||
protected $factory; | ||
|
||
protected function setUp() | ||
{ | ||
$this->factory = new Factory(); | ||
} | ||
|
||
/** | ||
* @dataProvider constraintNameProvider | ||
* | ||
* @param string $constraintName | ||
* @param string $expectedClass | ||
* @return void | ||
*/ | ||
public function testCreateInstanceForConstraintName($constraintName, $expectedClass) | ||
{ | ||
$constraint = $this->factory->createInstanceFor($constraintName); | ||
|
||
$this->assertInstanceOf($expectedClass, $constraint); | ||
$this->assertInstanceOf('JsonSchema\Constraints\ConstraintInterface', $constraint); | ||
$this->assertSame($this->factory->getUriRetriever(), $constraint->getUriRetriever()); | ||
} | ||
|
||
public function constraintNameProvider() | ||
{ | ||
return array( | ||
array('array', 'JsonSchema\Constraints\CollectionConstraint'), | ||
array('collection', 'JsonSchema\Constraints\CollectionConstraint'), | ||
array('object', 'JsonSchema\Constraints\ObjectConstraint'), | ||
array('type', 'JsonSchema\Constraints\TypeConstraint'), | ||
array('undefined', 'JsonSchema\Constraints\UndefinedConstraint'), | ||
array('string', 'JsonSchema\Constraints\StringConstraint'), | ||
array('number', 'JsonSchema\Constraints\NumberConstraint'), | ||
array('enum', 'JsonSchema\Constraints\EnumConstraint'), | ||
array('format', 'JsonSchema\Constraints\FormatConstraint'), | ||
array('schema', 'JsonSchema\Constraints\SchemaConstraint'), | ||
array('validator', 'JsonSchema\Validator'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidConstraintNameProvider | ||
* | ||
* @param string $constraintName | ||
* @return void | ||
*/ | ||
public function testExceptionWhenCreateInstanceForInvalidConstraintName($constraintName) | ||
{ | ||
$this->setExpectedException('JsonSchema\Exception\InvalidArgumentException'); | ||
$this->factory->createInstanceFor($constraintName); | ||
} | ||
|
||
public function invalidConstraintNameProvider() | ||
{ | ||
return array( | ||
array('invalidConstraintName'), | ||
); | ||
} | ||
} |