| 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\Entity\JsonPointer; |
| 13 |
|
| 14 |
/** |
| 15 |
* The NumberConstraint Constraints, validates an number against a given schema |
| 16 |
* |
| 17 |
* @author Robert Schönthal <[email protected]> |
| 18 |
* @author Bruno Prieto Reis <[email protected]> |
| 19 |
*/ |
| 20 |
class NumberConstraint extends Constraint |
| 21 |
{ |
| 22 |
/** |
| 23 |
* {@inheritdoc} |
| 24 |
*/ |
| 25 |
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) |
| 26 |
{ |
| 27 |
// Verify minimum |
| 28 |
if (isset($schema->exclusiveMinimum)) { |
| 29 |
if (isset($schema->minimum)) { |
| 30 |
if ($schema->exclusiveMinimum && $element <= $schema->minimum) { |
| 31 |
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'exclusiveMinimum', array('minimum' => $schema->minimum)); |
| 32 |
} elseif ($element < $schema->minimum) { |
| 33 |
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'minimum', array('minimum' => $schema->minimum)); |
| 34 |
} |
| 35 |
} else { |
| 36 |
$this->addError($path, 'Use of exclusiveMinimum requires presence of minimum', 'missingMinimum'); |
| 37 |
} |
| 38 |
} elseif (isset($schema->minimum) && $element < $schema->minimum) { |
| 39 |
$this->addError($path, 'Must have a minimum value of ' . $schema->minimum, 'minimum', array('minimum' => $schema->minimum)); |
| 40 |
} |
| 41 |
|
| 42 |
// Verify maximum |
| 43 |
if (isset($schema->exclusiveMaximum)) { |
| 44 |
if (isset($schema->maximum)) { |
| 45 |
if ($schema->exclusiveMaximum && $element >= $schema->maximum) { |
| 46 |
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'exclusiveMaximum', array('maximum' => $schema->maximum)); |
| 47 |
} elseif ($element > $schema->maximum) { |
| 48 |
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'maximum', array('maximum' => $schema->maximum)); |
| 49 |
} |
| 50 |
} else { |
| 51 |
$this->addError($path, 'Use of exclusiveMaximum requires presence of maximum', 'missingMaximum'); |
| 52 |
} |
| 53 |
} elseif (isset($schema->maximum) && $element > $schema->maximum) { |
| 54 |
$this->addError($path, 'Must have a maximum value of ' . $schema->maximum, 'maximum', array('maximum' => $schema->maximum)); |
| 55 |
} |
| 56 |
|
| 57 |
// Verify divisibleBy - Draft v3 |
| 58 |
if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) { |
| 59 |
$this->addError($path, 'Is not divisible by ' . $schema->divisibleBy, 'divisibleBy', array('divisibleBy' => $schema->divisibleBy)); |
| 60 |
} |
| 61 |
|
| 62 |
// Verify multipleOf - Draft v4 |
| 63 |
if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) { |
| 64 |
$this->addError($path, 'Must be a multiple of ' . $schema->multipleOf, 'multipleOf', array('multipleOf' => $schema->multipleOf)); |
| 65 |
} |
| 66 |
|
| 67 |
$this->checkFormat($element, $schema, $path, $i); |
| 68 |
} |
| 69 |
|
| 70 |
private function fmod($number1, $number2) |
| 71 |
{ |
| 72 |
$modulus = ($number1 - round($number1 / $number2) * $number2); |
| 73 |
$precision = 0.0000000001; |
| 74 |
|
| 75 |
if (-$precision < $modulus && $modulus < $precision) { |
| 76 |
return 0.0; |
| 77 |
} |
| 78 |
|
| 79 |
return $modulus; |
| 80 |
} |
| 81 |
} |