mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-05-03 04:48:11 +02:00
add instance cache, remove unnecessary execution paths (#288)
fix wrong merge
This commit is contained in:
parent
1296583112
commit
0f87b314cd
@ -23,7 +23,7 @@ class Factory
|
|||||||
* @var SchemaStorage
|
* @var SchemaStorage
|
||||||
*/
|
*/
|
||||||
protected $schemaStorage;
|
protected $schemaStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var UriRetriever $uriRetriever
|
* @var UriRetriever $uriRetriever
|
||||||
*/
|
*/
|
||||||
@ -56,6 +56,11 @@ class Factory
|
|||||||
'validator' => 'JsonSchema\Validator',
|
'validator' => 'JsonSchema\Validator',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array<ConstraintInterface>
|
||||||
|
*/
|
||||||
|
private $instanceCache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param SchemaStorage $schemaStorage
|
* @param SchemaStorage $schemaStorage
|
||||||
* @param UriRetrieverInterface $uriRetriever
|
* @param UriRetrieverInterface $uriRetriever
|
||||||
@ -78,7 +83,7 @@ class Factory
|
|||||||
{
|
{
|
||||||
return $this->uriRetriever;
|
return $this->uriRetriever;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSchemaStorage()
|
public function getSchemaStorage()
|
||||||
{
|
{
|
||||||
return $this->schemaStorage;
|
return $this->schemaStorage;
|
||||||
@ -124,12 +129,15 @@ class Factory
|
|||||||
public function createInstanceFor($constraintName)
|
public function createInstanceFor($constraintName)
|
||||||
{
|
{
|
||||||
if (array_key_exists($constraintName, $this->constraintMap)) {
|
if (array_key_exists($constraintName, $this->constraintMap)) {
|
||||||
return new $this->constraintMap[$constraintName](
|
if (!isset($this->instanceCache[$constraintName])) {
|
||||||
$this->checkMode,
|
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName](
|
||||||
$this->schemaStorage,
|
$this->checkMode,
|
||||||
$this->uriRetriever,
|
$this->schemaStorage,
|
||||||
$this
|
$this->uriRetriever,
|
||||||
);
|
$this
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return clone $this->instanceCache[$constraintName];
|
||||||
}
|
}
|
||||||
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
|
throw new InvalidArgumentException('Unknown constraint ' . $constraintName);
|
||||||
}
|
}
|
||||||
|
@ -105,11 +105,6 @@ class ObjectConstraint extends Constraint
|
|||||||
$this->addError($path, "The presence of the property " . $i . " requires that " . $require . " also be present", 'requires');
|
$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());
|
$property = $this->getProperty($element, $i, new UndefinedConstraint());
|
||||||
if (is_object($property)) {
|
if (is_object($property)) {
|
||||||
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
|
$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);
|
||||||
@ -129,7 +124,11 @@ class ObjectConstraint extends Constraint
|
|||||||
foreach ($objectDefinition as $i => $value) {
|
foreach ($objectDefinition as $i => $value) {
|
||||||
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
|
$property = $this->getProperty($element, $i, $this->getFactory()->createInstanceFor('undefined'));
|
||||||
$definition = $this->getProperty($objectDefinition, $i);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user