class NumberConstraint
The NumberConstraint Constraints, validates an number against a given schema
@author Robert Schönthal <seroscho@googlemail.com> @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
Hierarchy
- class \JsonSchema\Constraints\BaseConstraint
- class \JsonSchema\Constraints\Constraint extends \JsonSchema\Constraints\BaseConstraint implements \JsonSchema\Constraints\ConstraintInterface
- class \JsonSchema\Constraints\NumberConstraint extends \JsonSchema\Constraints\Constraint
- class \JsonSchema\Constraints\Constraint extends \JsonSchema\Constraints\BaseConstraint implements \JsonSchema\Constraints\ConstraintInterface
Expanded class hierarchy of NumberConstraint
File
-
vendor/
justinrainbow/ json-schema/ src/ JsonSchema/ Constraints/ NumberConstraint.php, line 20
Namespace
JsonSchema\ConstraintsView source
class NumberConstraint extends Constraint {
/**
* {@inheritdoc}
*/
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) {
// Verify minimum
if (isset($schema->exclusiveMinimum)) {
if (isset($schema->minimum)) {
if ($schema->exclusiveMinimum && $element <= $schema->minimum) {
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'exclusiveMinimum', array(
'minimum' => $schema->minimum,
));
}
elseif ($element < $schema->minimum) {
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'minimum', array(
'minimum' => $schema->minimum,
));
}
}
else {
$this->addError($path, 'Use of exclusiveMinimum requires presence of minimum', 'missingMinimum');
}
}
elseif (isset($schema->minimum) && $element < $schema->minimum) {
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'minimum', array(
'minimum' => $schema->minimum,
));
}
// Verify maximum
if (isset($schema->exclusiveMaximum)) {
if (isset($schema->maximum)) {
if ($schema->exclusiveMaximum && $element >= $schema->maximum) {
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'exclusiveMaximum', array(
'maximum' => $schema->maximum,
));
}
elseif ($element > $schema->maximum) {
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'maximum', array(
'maximum' => $schema->maximum,
));
}
}
else {
$this->addError($path, 'Use of exclusiveMaximum requires presence of maximum', 'missingMaximum');
}
}
elseif (isset($schema->maximum) && $element > $schema->maximum) {
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'maximum', array(
'maximum' => $schema->maximum,
));
}
// Verify divisibleBy - Draft v3
if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) {
$this->addError($path, 'Is not divisible by ' . $schema->divisibleBy, 'divisibleBy', array(
'divisibleBy' => $schema->divisibleBy,
));
}
// Verify multipleOf - Draft v4
if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) {
$this->addError($path, 'Must be a multiple of ' . $schema->multipleOf, 'multipleOf', array(
'multipleOf' => $schema->multipleOf,
));
}
$this->checkFormat($element, $schema, $path, $i);
}
private function fmod($number1, $number2) {
$modulus = $number1 - round($number1 / $number2) * $number2;
$precision = 1.0E-10;
if (-$precision < $modulus && $modulus < $precision) {
return 0.0;
}
return $modulus;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
BaseConstraint::$errorMask | protected | property | ||
BaseConstraint::$errors | protected | property | ||
BaseConstraint::$factory | protected | property | ||
BaseConstraint::addError | public | function | ||
BaseConstraint::addErrors | public | function | ||
BaseConstraint::arrayToObjectRecursive | public static | function | Recursively cast an associative array to an object | |
BaseConstraint::getErrorMask | public | function | Get the error mask | |
BaseConstraint::getErrors | public | function | ||
BaseConstraint::isValid | public | function | ||
BaseConstraint::numErrors | public | function | ||
BaseConstraint::reset | public | function | Clears any reported errors. Should be used between multiple validation checks. |
|
BaseConstraint::__construct | public | function | ||
Constraint::$inlineSchemaProperty | protected | property | ||
Constraint::checkArray | protected | function | Validates an array | |
Constraint::checkEnum | protected | function | Checks a enum element | |
Constraint::checkFormat | protected | function | Checks format of an element | |
Constraint::checkNumber | protected | function | Checks a number element | |
Constraint::checkObject | protected | function | Validates an object | |
Constraint::checkString | protected | function | Checks a string element | |
Constraint::checkType | protected | function | Validates the type of a property | |
Constraint::checkUndefined | protected | function | Checks a undefined element | |
Constraint::CHECK_MODE_APPLY_DEFAULTS | constant | |||
Constraint::CHECK_MODE_COERCE_TYPES | constant | |||
Constraint::CHECK_MODE_DISABLE_FORMAT | constant | |||
Constraint::CHECK_MODE_EXCEPTIONS | constant | |||
Constraint::CHECK_MODE_NONE | constant | |||
Constraint::CHECK_MODE_NORMAL | constant | |||
Constraint::CHECK_MODE_ONLY_REQUIRED_DEFAULTS | constant | |||
Constraint::CHECK_MODE_TYPE_CAST | constant | |||
Constraint::CHECK_MODE_VALIDATE_SCHEMA | constant | |||
Constraint::convertJsonPointerIntoPropertyPath | protected | function | ||
Constraint::getTypeCheck | protected | function | Get the type check based on the set check mode. | |
Constraint::incrementPath | protected | function | Bubble down the path | |
NumberConstraint::check | public | function | invokes the validation of an element | Overrides ConstraintInterface::check |
NumberConstraint::fmod | private | function |