| 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\SchemaStorage; |
| 14 |
use JsonSchema\SchemaStorageInterface; |
| 15 |
use JsonSchema\Uri\UriRetriever; |
| 16 |
use JsonSchema\UriRetrieverInterface; |
| 17 |
use JsonSchema\Validator; |
| 18 |
|
| 19 |
/** |
| 20 |
* Factory for centralize constraint initialization. |
| 21 |
*/ |
| 22 |
class Factory |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @var SchemaStorage |
| 26 |
*/ |
| 27 |
protected $schemaStorage; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var UriRetriever |
| 31 |
*/ |
| 32 |
protected $uriRetriever; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $checkMode = Constraint::CHECK_MODE_NORMAL; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var TypeCheck\TypeCheckInterface[] |
| 41 |
*/ |
| 42 |
private $typeCheck = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* @var int Validation context |
| 46 |
*/ |
| 47 |
protected $errorContext = Validator::ERROR_DOCUMENT_VALIDATION; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $constraintMap = array( |
| 53 |
'array' => 'JsonSchema\Constraints\CollectionConstraint', |
| 54 |
'collection' => 'JsonSchema\Constraints\CollectionConstraint', |
| 55 |
'object' => 'JsonSchema\Constraints\ObjectConstraint', |
| 56 |
'type' => 'JsonSchema\Constraints\TypeConstraint', |
| 57 |
'undefined' => 'JsonSchema\Constraints\UndefinedConstraint', |
| 58 |
'string' => 'JsonSchema\Constraints\StringConstraint', |
| 59 |
'number' => 'JsonSchema\Constraints\NumberConstraint', |
| 60 |
'enum' => 'JsonSchema\Constraints\EnumConstraint', |
| 61 |
'format' => 'JsonSchema\Constraints\FormatConstraint', |
| 62 |
'schema' => 'JsonSchema\Constraints\SchemaConstraint', |
| 63 |
'validator' => 'JsonSchema\Validator' |
| 64 |
); |
| 65 |
|
| 66 |
/** |
| 67 |
* @var array<ConstraintInterface> |
| 68 |
*/ |
| 69 |
private $instanceCache = array(); |
| 70 |
|
| 71 |
/** |
| 72 |
* @param SchemaStorage $schemaStorage |
| 73 |
* @param UriRetrieverInterface $uriRetriever |
| 74 |
* @param int $checkMode |
| 75 |
*/ |
| 76 |
public function __construct( |
| 77 |
?SchemaStorageInterface $schemaStorage = null, |
| 78 |
?UriRetrieverInterface $uriRetriever = null, |
| 79 |
$checkMode = Constraint::CHECK_MODE_NORMAL |
| 80 |
) { |
| 81 |
// set provided config options |
| 82 |
$this->setConfig($checkMode); |
| 83 |
|
| 84 |
$this->uriRetriever = $uriRetriever ?: new UriRetriever(); |
| 85 |
$this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Set config values |
| 90 |
* |
| 91 |
* @param int $checkMode Set checkMode options - does not preserve existing flags |
| 92 |
*/ |
| 93 |
public function setConfig($checkMode = Constraint::CHECK_MODE_NORMAL) |
| 94 |
{ |
| 95 |
$this->checkMode = $checkMode; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Enable checkMode flags |
| 100 |
* |
| 101 |
* @param int $options |
| 102 |
*/ |
| 103 |
public function addConfig($options) |
| 104 |
{ |
| 105 |
$this->checkMode |= $options; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Disable checkMode flags |
| 110 |
* |
| 111 |
* @param int $options |
| 112 |
*/ |
| 113 |
public function removeConfig($options) |
| 114 |
{ |
| 115 |
$this->checkMode &= ~$options; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get checkMode option |
| 120 |
* |
| 121 |
* @param int $options Options to get, if null then return entire bitmask |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function getConfig($options = null) |
| 126 |
{ |
| 127 |
if ($options === null) { |
| 128 |
return $this->checkMode; |
| 129 |
} |
| 130 |
|
| 131 |
return $this->checkMode & $options; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @return UriRetrieverInterface |
| 136 |
*/ |
| 137 |
public function getUriRetriever() |
| 138 |
{ |
| 139 |
return $this->uriRetriever; |
| 140 |
} |
| 141 |
|
| 142 |
public function getSchemaStorage() |
| 143 |
{ |
| 144 |
return $this->schemaStorage; |
| 145 |
} |
| 146 |
|
| 147 |
public function getTypeCheck() |
| 148 |
{ |
| 149 |
if (!isset($this->typeCheck[$this->checkMode])) { |
| 150 |
$this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST) |
| 151 |
? new TypeCheck\LooseTypeCheck() |
| 152 |
: new TypeCheck\StrictTypeCheck(); |
| 153 |
} |
| 154 |
|
| 155 |
return $this->typeCheck[$this->checkMode]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* @param string $name |
| 160 |
* @param string $class |
| 161 |
* |
| 162 |
* @return Factory |
| 163 |
*/ |
| 164 |
public function setConstraintClass($name, $class) |
| 165 |
{ |
| 166 |
// Ensure class exists |
| 167 |
if (!class_exists($class)) { |
| 168 |
throw new InvalidArgumentException('Unknown constraint ' . $name); |
| 169 |
} |
| 170 |
// Ensure class is appropriate |
| 171 |
if (!in_array('JsonSchema\Constraints\ConstraintInterface', class_implements($class))) { |
| 172 |
throw new InvalidArgumentException('Invalid class ' . $name); |
| 173 |
} |
| 174 |
$this->constraintMap[$name] = $class; |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Create a constraint instance for the given constraint name. |
| 181 |
* |
| 182 |
* @param string $constraintName |
| 183 |
* |
| 184 |
* @throws InvalidArgumentException if is not possible create the constraint instance |
| 185 |
* |
| 186 |
* @return ConstraintInterface|ObjectConstraint |
| 187 |
*/ |
| 188 |
public function createInstanceFor($constraintName) |
| 189 |
{ |
| 190 |
if (!isset($this->constraintMap[$constraintName])) { |
| 191 |
throw new InvalidArgumentException('Unknown constraint ' . $constraintName); |
| 192 |
} |
| 193 |
|
| 194 |
if (!isset($this->instanceCache[$constraintName])) { |
| 195 |
$this->instanceCache[$constraintName] = new $this->constraintMap[$constraintName]($this); |
| 196 |
} |
| 197 |
|
| 198 |
return clone $this->instanceCache[$constraintName]; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get the error context |
| 203 |
* |
| 204 |
* @return string |
| 205 |
*/ |
| 206 |
public function getErrorContext() |
| 207 |
{ |
| 208 |
return $this->errorContext; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Set the error context |
| 213 |
* |
| 214 |
* @param string $validationContext |
| 215 |
*/ |
| 216 |
public function setErrorContext($errorContext) |
| 217 |
{ |
| 218 |
$this->errorContext = $errorContext; |
| 219 |
} |
| 220 |
} |