Skip to content

Commit 09ab4df

Browse files
committed
Merge pull request #143 from Maks3w/feature/constraint-factory
Add constraint factory
2 parents 043bc7c + 34f9ae1 commit 09ab4df

File tree

4 files changed

+188
-10
lines changed

4 files changed

+188
-10
lines changed

src/JsonSchema/Constraints/Constraint.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ abstract class Constraint implements ConstraintInterface
2727
const CHECK_MODE_NORMAL = 1;
2828
const CHECK_MODE_TYPE_CAST = 2;
2929

30+
/**
31+
* @var null|Factory
32+
*/
33+
private $factory;
34+
3035
/**
3136
* @param int $checkMode
3237
* @param UriRetriever $uriRetriever
38+
* @param Factory $factory
3339
*/
34-
public function __construct($checkMode = self::CHECK_MODE_NORMAL, UriRetriever $uriRetriever = null)
40+
public function __construct($checkMode = self::CHECK_MODE_NORMAL, UriRetriever $uriRetriever = null, Factory $factory = null)
3541
{
3642
$this->checkMode = $checkMode;
3743
$this->uriRetriever = $uriRetriever;
44+
$this->factory = $factory;
3845
}
3946

4047
/**
@@ -50,6 +57,18 @@ public function getUriRetriever()
5057
return $this->uriRetriever;
5158
}
5259

60+
/**
61+
* @return Factory
62+
*/
63+
public function getFactory()
64+
{
65+
if (!$this->factory) {
66+
$this->factory = new Factory($this->getUriRetriever());
67+
}
68+
69+
return $this->factory;
70+
}
71+
5372
/**
5473
* @param UriRetriever $uriRetriever
5574
*/
@@ -145,7 +164,7 @@ protected function incrementPath($path, $i)
145164
*/
146165
protected function checkArray($value, $schema = null, $path = null, $i = null)
147166
{
148-
$validator = new CollectionConstraint($this->checkMode, $this->uriRetriever);
167+
$validator = $this->getFactory()->createInstanceFor('collection');
149168
$validator->check($value, $schema, $path, $i);
150169

151170
$this->addErrors($validator->getErrors());
@@ -162,7 +181,7 @@ protected function checkArray($value, $schema = null, $path = null, $i = null)
162181
*/
163182
protected function checkObject($value, $schema = null, $path = null, $i = null, $patternProperties = null)
164183
{
165-
$validator = new ObjectConstraint($this->checkMode, $this->uriRetriever);
184+
$validator = $this->getFactory()->createInstanceFor('object');
166185
$validator->check($value, $schema, $path, $i, $patternProperties);
167186

168187
$this->addErrors($validator->getErrors());
@@ -178,7 +197,7 @@ protected function checkObject($value, $schema = null, $path = null, $i = null,
178197
*/
179198
protected function checkType($value, $schema = null, $path = null, $i = null)
180199
{
181-
$validator = new TypeConstraint($this->checkMode, $this->uriRetriever);
200+
$validator = $this->getFactory()->createInstanceFor('type');
182201
$validator->check($value, $schema, $path, $i);
183202

184203
$this->addErrors($validator->getErrors());
@@ -194,7 +213,7 @@ protected function checkType($value, $schema = null, $path = null, $i = null)
194213
*/
195214
protected function checkUndefined($value, $schema = null, $path = null, $i = null)
196215
{
197-
$validator = new UndefinedConstraint($this->checkMode, $this->uriRetriever);
216+
$validator = $this->getFactory()->createInstanceFor('undefined');
198217
$validator->check($value, $schema, $path, $i);
199218

200219
$this->addErrors($validator->getErrors());
@@ -210,7 +229,7 @@ protected function checkUndefined($value, $schema = null, $path = null, $i = nul
210229
*/
211230
protected function checkString($value, $schema = null, $path = null, $i = null)
212231
{
213-
$validator = new StringConstraint($this->checkMode, $this->uriRetriever);
232+
$validator = $this->getFactory()->createInstanceFor('string');
214233
$validator->check($value, $schema, $path, $i);
215234

216235
$this->addErrors($validator->getErrors());
@@ -226,7 +245,7 @@ protected function checkString($value, $schema = null, $path = null, $i = null)
226245
*/
227246
protected function checkNumber($value, $schema = null, $path = null, $i = null)
228247
{
229-
$validator = new NumberConstraint($this->checkMode, $this->uriRetriever);
248+
$validator = $this->getFactory()->createInstanceFor('number');
230249
$validator->check($value, $schema, $path, $i);
231250

232251
$this->addErrors($validator->getErrors());
@@ -242,15 +261,15 @@ protected function checkNumber($value, $schema = null, $path = null, $i = null)
242261
*/
243262
protected function checkEnum($value, $schema = null, $path = null, $i = null)
244263
{
245-
$validator = new EnumConstraint($this->checkMode, $this->uriRetriever);
264+
$validator = $this->getFactory()->createInstanceFor('enum');
246265
$validator->check($value, $schema, $path, $i);
247266

248267
$this->addErrors($validator->getErrors());
249268
}
250269

251270
protected function checkFormat($value, $schema = null, $path = null, $i = null)
252271
{
253-
$validator = new FormatConstraint($this->checkMode, $this->uriRetriever);
272+
$validator = $this->getFactory()->createInstanceFor('format');
254273
$validator->check($value, $schema, $path, $i);
255274

256275
$this->addErrors($validator->getErrors());
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Constraints;
11+
12+
use JsonSchema\Exception\InvalidArgumentException;
13+
use JsonSchema\Uri\UriRetriever;
14+
use JsonSchema\Validator;
15+
16+
/**
17+
* Factory for centralize constraint initialization.
18+
*/
19+
class Factory
20+
{
21+
/**
22+
* @var UriRetriever
23+
*/
24+
protected $uriRetriever;
25+
26+
/**
27+
* @param UriRetriever $uriRetriever
28+
*/
29+
public function __construct(UriRetriever $uriRetriever = null)
30+
{
31+
if (!$uriRetriever) {
32+
$uriRetriever = new UriRetriever();
33+
}
34+
35+
$this->uriRetriever = $uriRetriever;
36+
}
37+
38+
/**
39+
* @return UriRetriever
40+
*/
41+
public function getUriRetriever()
42+
{
43+
return $this->uriRetriever;
44+
}
45+
46+
/**
47+
* Create a constraint instance for the given constraint name.
48+
*
49+
* @param string $constraintName
50+
* @return ConstraintInterface|ObjectConstraint
51+
* @throws InvalidArgumentException if is not possible create the constraint instance.
52+
*/
53+
public function createInstanceFor($constraintName)
54+
{
55+
switch ($constraintName) {
56+
case 'array':
57+
case 'collection':
58+
return new CollectionConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
59+
case 'object':
60+
return new ObjectConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
61+
case 'type':
62+
return new TypeConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
63+
case 'undefined':
64+
return new UndefinedConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
65+
case 'string':
66+
return new StringConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
67+
case 'number':
68+
return new NumberConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
69+
case 'enum':
70+
return new EnumConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
71+
case 'format':
72+
return new FormatConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
73+
case 'schema':
74+
return new SchemaConstraint(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
75+
case 'validator':
76+
return new Validator(Constraint::CHECK_MODE_NORMAL, $this->uriRetriever, $this);
77+
}
78+
79+
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
80+
}
81+
}

src/JsonSchema/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Validator extends Constraint
3232
*/
3333
public function check($value, $schema = null, $path = null, $i = null)
3434
{
35-
$validator = new SchemaConstraint($this->checkMode, $this->uriRetriever);
35+
$validator = $this->getFactory()->createInstanceFor('schema');
3636
$validator->check($value, $schema);
3737

3838
$this->addErrors(array_unique($validator->getErrors(), SORT_REGULAR));
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the JsonSchema package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace JsonSchema\Tests\Constraints;
11+
12+
use JsonSchema\Constraints\Factory;
13+
use PHPUnit_Framework_TestCase as TestCase;
14+
15+
class FactoryTest extends TestCase
16+
{
17+
/**
18+
* @var Factory
19+
*/
20+
protected $factory;
21+
22+
protected function setUp()
23+
{
24+
$this->factory = new Factory();
25+
}
26+
27+
/**
28+
* @dataProvider constraintNameProvider
29+
*
30+
* @param string $constraintName
31+
* @param string $expectedClass
32+
* @return void
33+
*/
34+
public function testCreateInstanceForConstraintName($constraintName, $expectedClass)
35+
{
36+
$constraint = $this->factory->createInstanceFor($constraintName);
37+
38+
$this->assertInstanceOf($expectedClass, $constraint);
39+
$this->assertInstanceOf('JsonSchema\Constraints\ConstraintInterface', $constraint);
40+
$this->assertSame($this->factory->getUriRetriever(), $constraint->getUriRetriever());
41+
}
42+
43+
public function constraintNameProvider()
44+
{
45+
return array(
46+
array('array', 'JsonSchema\Constraints\CollectionConstraint'),
47+
array('collection', 'JsonSchema\Constraints\CollectionConstraint'),
48+
array('object', 'JsonSchema\Constraints\ObjectConstraint'),
49+
array('type', 'JsonSchema\Constraints\TypeConstraint'),
50+
array('undefined', 'JsonSchema\Constraints\UndefinedConstraint'),
51+
array('string', 'JsonSchema\Constraints\StringConstraint'),
52+
array('number', 'JsonSchema\Constraints\NumberConstraint'),
53+
array('enum', 'JsonSchema\Constraints\EnumConstraint'),
54+
array('format', 'JsonSchema\Constraints\FormatConstraint'),
55+
array('schema', 'JsonSchema\Constraints\SchemaConstraint'),
56+
array('validator', 'JsonSchema\Validator'),
57+
);
58+
}
59+
60+
/**
61+
* @dataProvider invalidConstraintNameProvider
62+
*
63+
* @param string $constraintName
64+
* @return void
65+
*/
66+
public function testExceptionWhenCreateInstanceForInvalidConstraintName($constraintName)
67+
{
68+
$this->setExpectedException('JsonSchema\Exception\InvalidArgumentException');
69+
$this->factory->createInstanceFor($constraintName);
70+
}
71+
72+
public function invalidConstraintNameProvider()
73+
{
74+
return array(
75+
array('invalidConstraintName'),
76+
);
77+
}
78+
}

0 commit comments

Comments
 (0)