Skip to content

Commit c1cd87c

Browse files
committed
performance improvements
1 parent 2128f9d commit c1cd87c

File tree

5 files changed

+57
-65
lines changed

5 files changed

+57
-65
lines changed

src/JsonSchema/Constraints/Constraint.php

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,38 @@
2222
*/
2323
abstract class Constraint implements ConstraintInterface
2424
{
25-
protected $schemaStorage;
2625
protected $checkMode = self::CHECK_MODE_NORMAL;
27-
protected $uriRetriever;
2826
protected $errors = array();
2927
protected $inlineSchemaProperty = '$schema';
3028

3129
const CHECK_MODE_NORMAL = 1;
3230
const CHECK_MODE_TYPE_CAST = 2;
3331

3432
/**
35-
* @var null|Factory
33+
* @var Factory
3634
*/
3735
private $factory;
3836

3937
/**
40-
* @param int $checkMode
41-
* @param SchemaStorage $schemaStorage
42-
* @param UriRetrieverInterface $uriRetriever
4338
* @param Factory $factory
4439
*/
45-
public function __construct(
46-
$checkMode = self::CHECK_MODE_NORMAL,
47-
SchemaStorage $schemaStorage = null,
48-
UriRetrieverInterface $uriRetriever = null,
49-
Factory $factory = null
50-
) {
51-
$this->checkMode = $checkMode;
52-
$this->uriRetriever = $uriRetriever;
53-
$this->factory = $factory;
54-
$this->schemaStorage = $schemaStorage;
40+
public function __construct(Factory $factory = null)
41+
{
42+
$this->factory = $factory ? : $this->getFactory();
43+
$this->checkMode = $this->factory->getCheckMode();
5544
}
5645

5746
/**
5847
* @return UriRetrieverInterface $uriRetriever
5948
*/
6049
public function getUriRetriever()
6150
{
62-
if (is_null($this->uriRetriever)) {
63-
$this->setUriRetriever(new UriRetriever);
64-
}
51+
return $this->factory->getUriRetriever();
52+
}
6553

66-
return $this->uriRetriever;
54+
public function setUriRetriever(UriRetrieverInterface $retriever)
55+
{
56+
$this->factory->setUriRetriever($retriever);
6757
}
6858

6959
/**
@@ -72,7 +62,7 @@ public function getUriRetriever()
7262
public function getFactory()
7363
{
7464
if (!$this->factory) {
75-
$this->factory = new Factory($this->getSchemaStorage(), $this->getUriRetriever(), $this->checkMode);
65+
$this->factory = new Factory();
7666
}
7767

7868
return $this->factory;
@@ -83,19 +73,7 @@ public function getFactory()
8373
*/
8474
public function getSchemaStorage()
8575
{
86-
if (is_null($this->schemaStorage)) {
87-
$this->schemaStorage = new SchemaStorage($this->getUriRetriever());
88-
}
89-
90-
return $this->schemaStorage;
91-
}
92-
93-
/**
94-
* @param UriRetrieverInterface $uriRetriever
95-
*/
96-
public function setUriRetriever(UriRetrieverInterface $uriRetriever)
97-
{
98-
$this->uriRetriever = $uriRetriever;
76+
return $this->getFactory()->getSchemaStorage();
9977
}
10078

10179
/**
@@ -123,7 +101,9 @@ public function addError(JsonPointer $path = null, $message, $constraint='', arr
123101
*/
124102
public function addErrors(array $errors)
125103
{
126-
$this->errors = array_merge($this->errors, $errors);
104+
if ($errors) {
105+
$this->errors = array_merge($this->errors, $errors);
106+
}
127107
}
128108

129109
/**
@@ -181,7 +161,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
181161
*/
182162
protected function checkArray($value, $schema = null, JsonPointer $path = null, $i = null)
183163
{
184-
$validator = $this->getFactory()->createInstanceFor('collection');
164+
$validator = $this->factory->createInstanceFor('collection');
185165
$validator->check($value, $schema, $path, $i);
186166

187167
$this->addErrors($validator->getErrors());
@@ -198,7 +178,7 @@ protected function checkArray($value, $schema = null, JsonPointer $path = null,
198178
*/
199179
protected function checkObject($value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null)
200180
{
201-
$validator = $this->getFactory()->createInstanceFor('object');
181+
$validator = $this->factory->createInstanceFor('object');
202182
$validator->check($value, $schema, $path, $i, $patternProperties);
203183

204184
$this->addErrors($validator->getErrors());
@@ -214,7 +194,7 @@ protected function checkObject($value, $schema = null, JsonPointer $path = null,
214194
*/
215195
protected function checkType($value, $schema = null, JsonPointer $path = null, $i = null)
216196
{
217-
$validator = $this->getFactory()->createInstanceFor('type');
197+
$validator = $this->factory->createInstanceFor('type');
218198
$validator->check($value, $schema, $path, $i);
219199

220200
$this->addErrors($validator->getErrors());
@@ -230,8 +210,8 @@ protected function checkType($value, $schema = null, JsonPointer $path = null, $
230210
*/
231211
protected function checkUndefined($value, $schema = null, JsonPointer $path = null, $i = null)
232212
{
233-
$validator = $this->getFactory()->createInstanceFor('undefined');
234-
$validator->check($value, $this->schemaStorage->resolveRefSchema($schema), $path, $i);
213+
$validator = $this->factory->createInstanceFor('undefined');
214+
$validator->check($value, $this->getSchemaStorage()->resolveRefSchema($schema), $path, $i);
235215

236216
$this->addErrors($validator->getErrors());
237217
}
@@ -246,7 +226,7 @@ protected function checkUndefined($value, $schema = null, JsonPointer $path = nu
246226
*/
247227
protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null)
248228
{
249-
$validator = $this->getFactory()->createInstanceFor('string');
229+
$validator = $this->factory->createInstanceFor('string');
250230
$validator->check($value, $schema, $path, $i);
251231

252232
$this->addErrors($validator->getErrors());
@@ -262,7 +242,7 @@ protected function checkString($value, $schema = null, JsonPointer $path = null,
262242
*/
263243
protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null)
264244
{
265-
$validator = $this->getFactory()->createInstanceFor('number');
245+
$validator = $this->factory->createInstanceFor('number');
266246
$validator->check($value, $schema, $path, $i);
267247

268248
$this->addErrors($validator->getErrors());
@@ -278,7 +258,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null,
278258
*/
279259
protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null)
280260
{
281-
$validator = $this->getFactory()->createInstanceFor('enum');
261+
$validator = $this->factory->createInstanceFor('enum');
282262
$validator->check($value, $schema, $path, $i);
283263

284264
$this->addErrors($validator->getErrors());
@@ -294,7 +274,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $
294274
*/
295275
protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null)
296276
{
297-
$validator = $this->getFactory()->createInstanceFor('format');
277+
$validator = $this->factory->createInstanceFor('format');
298278
$validator->check($value, $schema, $path, $i);
299279

300280
$this->addErrors($validator->getErrors());
@@ -307,7 +287,7 @@ protected function checkFormat($value, $schema = null, JsonPointer $path = null,
307287
*/
308288
protected function getTypeCheck()
309289
{
310-
return $this->getFactory()->getTypeCheck();
290+
return $this->factory->getTypeCheck();
311291
}
312292

313293
/**

src/JsonSchema/Constraints/Factory.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,27 @@ public function setConstraintClass($name, $class)
128128
*/
129129
public function createInstanceFor($constraintName)
130130
{
131-
if (array_key_exists($constraintName, $this->constraintMap)) {
132-
if (!isset($this->instanceCache[$constraintName])) {
133-
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
134-
$this->checkMode,
135-
$this->schemaStorage,
136-
$this->uriRetriever,
137-
$this
138-
);
139-
}
140-
return clone $this->instanceCache[$constraintName];
131+
if (!isset($this->constraintMap[$constraintName])) {
132+
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
141133
}
142-
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
134+
135+
if (!isset($this->instanceCache[$constraintName])) {
136+
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this);
137+
}
138+
139+
return clone $this->instanceCache[$constraintName];
140+
}
141+
142+
public function setUriRetriever(UriRetrieverInterface $retriever)
143+
{
144+
$this->uriRetriever = $retriever;
145+
}
146+
147+
/**
148+
* @return int
149+
*/
150+
public function getCheckMode()
151+
{
152+
return $this->checkMode;
143153
}
144154
}

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint
143143
*/
144144
protected function getProperty($element, $property, $fallback = null)
145145
{
146-
if (is_array($element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
147-
return array_key_exists($property, $element) ? $element[$property] : $fallback;
148-
} elseif (is_object($element)) {
149-
return property_exists($element, $property) ? $element->$property : $fallback;
146+
if (is_array($element) && isset($element[$property]) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
147+
return $element[$property];
148+
} elseif (is_object($element) && property_exists($element, $property)) {
149+
return $element->$property;
150150
}
151151

152152
return $fallback;

src/JsonSchema/Validator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace JsonSchema;
1111

12-
use JsonSchema\Constraints\SchemaConstraint;
1312
use JsonSchema\Constraints\Constraint;
1413
use JsonSchema\Entity\JsonPointer;
1514

tests/Constraints/BaseTestCase.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace JsonSchema\Tests\Constraints;
1111

1212
use JsonSchema\Constraints\Constraint;
13+
use JsonSchema\Constraints\Factory;
1314
use JsonSchema\SchemaStorage;
1415
use JsonSchema\Uri\UriResolver;
1516
use JsonSchema\Validator;
@@ -36,7 +37,7 @@ public function testInvalidCases($input, $schema, $checkMode = Constraint::CHECK
3637
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
3738
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
3839

39-
$validator = new Validator($checkMode, $schemaStorage);
40+
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
4041
$validator->check(json_decode($input), $schema);
4142

4243
if (array() !== $errors) {
@@ -58,7 +59,7 @@ public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constra
5859
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
5960
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
6061

61-
$validator = new Validator($checkMode, $schemaStorage);
62+
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
6263
$validator->check(json_decode($input, true), $schema);
6364

6465
if (array() !== $errors) {
@@ -75,7 +76,7 @@ public function testValidCases($input, $schema, $checkMode = Constraint::CHECK_M
7576
$schemaStorage = new SchemaStorage($this->getUriRetrieverMock(json_decode($schema)));
7677
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
7778

78-
$validator = new Validator($checkMode, $schemaStorage);
79+
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
7980
$validator->check(json_decode($input), $schema);
8081

8182
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
@@ -95,7 +96,8 @@ public function testValidCasesUsingAssoc($input, $schema, $checkMode = Constrain
9596
$schema = $schemaStorage->getSchema('http://www.my-domain.com/schema.json');
9697

9798
$value = json_decode($input, true);
98-
$validator = new Validator($checkMode, $schemaStorage);
99+
100+
$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
99101

100102
$validator->check($value, $schema);
101103
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
@@ -142,6 +144,7 @@ private function getUriRetrieverMock($schema)
142144
$uriRetriever->retrieve('http://www.my-domain.com/schema.json')
143145
->willReturn($schema)
144146
->shouldBeCalled();
147+
145148
$uriRetriever->retrieve(Argument::any())
146149
->will(function ($args) use ($jsonSchemaDraft03, $jsonSchemaDraft04, $relativeTestsRoot) {
147150
if ('http://json-schema.org/draft-03/schema' === $args[0]) {

0 commit comments

Comments
 (0)