From 0015556a921052645f9e9eb83361b8d4a4be501b Mon Sep 17 00:00:00 2001 From: narcoticfresh Date: Fri, 5 Aug 2016 09:14:25 +0200 Subject: [PATCH] add instance cache, remove unnecessary execution paths fix wrong merge --- src/JsonSchema/Constraints/Factory.php | 24 ++++++++++++------- .../Constraints/ObjectConstraint.php | 11 ++++----- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/JsonSchema/Constraints/Factory.php b/src/JsonSchema/Constraints/Factory.php index bb12ecec..83355c5e 100644 --- a/src/JsonSchema/Constraints/Factory.php +++ b/src/JsonSchema/Constraints/Factory.php @@ -23,7 +23,7 @@ class Factory * @var SchemaStorage */ protected $schemaStorage; - + /** * @var UriRetriever $uriRetriever */ @@ -56,6 +56,11 @@ class Factory 'validator' => 'JsonSchema\Validator', ); + /** + * @var array + */ + private $instanceCache = array(); + /** * @param SchemaStorage $schemaStorage * @param UriRetrieverInterface $uriRetriever @@ -78,7 +83,7 @@ public function getUriRetriever() { return $this->uriRetriever; } - + public function getSchemaStorage() { return $this->schemaStorage; @@ -124,12 +129,15 @@ public function setConstraintClass($name, $class) public function createInstanceFor($constraintName) { if (array_key_exists($constraintName, $this->constraintMap)) { - return new $this->constraintMap[$constraintName]( - $this->checkMode, - $this->schemaStorage, - $this->uriRetriever, - $this - ); + if (!isset($this->instanceCache[$constraintName])) { + $this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]( + $this->checkMode, + $this->schemaStorage, + $this->uriRetriever, + $this + ); + } + return clone $this->instanceCache[$constraintName]; } throw new InvalidArgumentException('Unknown constraint ' . $constraintName); } diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index 5a12b87f..df8d8088 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -105,11 +105,6 @@ public function validateElement($element, $matches, $objectDefinition = null, Js $this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires'); } - if (!$definition) { - // normal property verification - $this->checkUndefined($value, new \stdClass(), $path, $i); - } - $property = $this->getProperty($element, $i, new UndefinedConstraint()); if (is_object($property)) { $this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path); @@ -129,7 +124,11 @@ public function validateDefinition($element, $objectDefinition = null, JsonPoint foreach ($objectDefinition as $i => $value) { $property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined')); $definition = $this->getProperty($objectDefinition, $i); - $this->checkUndefined($property, $definition, $path, $i); + + if (is_object($definition)) { + // Undefined constraint will check for is_object() and quit if is not - so why pass it? + $this->checkUndefined($property, $definition, $path, $i); + } } }