-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9db6594
commit 6d04026
Showing
5 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?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 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 ((object) $element == $const) { | ||
return; | ||
} | ||
} | ||
|
||
if ($type === gettype($const)) { | ||
if ($type == 'object') { | ||
if ($element == $const) { | ||
return; | ||
} | ||
} elseif ($element === $const) { | ||
return; | ||
} | ||
} | ||
|
||
$this->addError(ConstraintError::CONSTANT(), $path, array('const' => $schema->const)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters