forked from jsonrainbow/json-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstConstraint.php
More file actions
49 lines (41 loc) · 1.32 KB
/
ConstConstraint.php
File metadata and controls
49 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JsonSchema\Constraints;
use Icecave\Parity\Parity;
use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;
/**
* The ConstConstraint Constraints, validates an element against a constant value
*
* @author Martin Helmich <[email protected]>
*/
class ConstConstraint extends Constraint
{
/**
* {@inheritdoc}
*/
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null)
{
// Only validate const if the attribute exists
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {
return;
}
$const = $schema->const;
$type = gettype($element);
$constType = gettype($const);
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $constType == 'object') {
if (Parity::isEqualTo((object) $element, $const)) {
return;
}
}
if (Parity::isEqualTo($element, $const)) {
return;
}
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const));
}
}