|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the JsonSchema package. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace JsonSchema\Constraints; |
| 13 | + |
| 14 | +use JsonSchema\ConstraintError; |
| 15 | +use JsonSchema\Entity\JsonPointer; |
| 16 | + |
| 17 | +/** |
| 18 | + * The NumberConstraint Constraints, validates an number against a given schema |
| 19 | + * |
| 20 | + * @author Robert Schönthal <seroscho@googlemail.com> |
| 21 | + * @author Bruno Prieto Reis <bruno.p.reis@gmail.com> |
| 22 | + */ |
| 23 | +class NumberConstraint extends Constraint |
| 24 | +{ |
| 25 | + /** |
| 26 | + * {@inheritdoc} |
| 27 | + */ |
| 28 | + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void |
| 29 | + { |
| 30 | + // Verify minimum |
| 31 | + if (isset($schema->exclusiveMinimum)) { |
| 32 | + if (isset($schema->minimum)) { |
| 33 | + if ($schema->exclusiveMinimum && $element <= $schema->minimum) { |
| 34 | + $this->addError(ConstraintError::EXCLUSIVE_MINIMUM(), $path, ['minimum' => $schema->minimum]); |
| 35 | + } elseif ($element < $schema->minimum) { |
| 36 | + $this->addError(ConstraintError::MINIMUM(), $path, ['minimum' => $schema->minimum]); |
| 37 | + } |
| 38 | + } else { |
| 39 | + $this->addError(ConstraintError::MISSING_MINIMUM(), $path); |
| 40 | + } |
| 41 | + } elseif (isset($schema->minimum) && $element < $schema->minimum) { |
| 42 | + $this->addError(ConstraintError::MINIMUM(), $path, ['minimum' => $schema->minimum]); |
| 43 | + } |
| 44 | + |
| 45 | + // Verify maximum |
| 46 | + if (isset($schema->exclusiveMaximum)) { |
| 47 | + if (isset($schema->maximum)) { |
| 48 | + if ($schema->exclusiveMaximum && $element >= $schema->maximum) { |
| 49 | + $this->addError(ConstraintError::EXCLUSIVE_MAXIMUM(), $path, ['maximum' => $schema->maximum]); |
| 50 | + } elseif ($element > $schema->maximum) { |
| 51 | + $this->addError(ConstraintError::MAXIMUM(), $path, ['maximum' => $schema->maximum]); |
| 52 | + } |
| 53 | + } else { |
| 54 | + $this->addError(ConstraintError::MISSING_MAXIMUM(), $path); |
| 55 | + } |
| 56 | + } elseif (isset($schema->maximum) && $element > $schema->maximum) { |
| 57 | + $this->addError(ConstraintError::MAXIMUM(), $path, ['maximum' => $schema->maximum]); |
| 58 | + } |
| 59 | + |
| 60 | + // Verify divisibleBy - Draft v3 |
| 61 | + if (isset($schema->divisibleBy) && $this->fmod($element, $schema->divisibleBy) != 0) { |
| 62 | + $this->addError(ConstraintError::DIVISIBLE_BY(), $path, ['divisibleBy' => $schema->divisibleBy]); |
| 63 | + } |
| 64 | + |
| 65 | + // Verify multipleOf - Draft v4 |
| 66 | + if (isset($schema->multipleOf) && $this->fmod($element, $schema->multipleOf) != 0) { |
| 67 | + $this->addError(ConstraintError::MULTIPLE_OF(), $path, ['multipleOf' => $schema->multipleOf]); |
| 68 | + } |
| 69 | + |
| 70 | + $this->checkFormat($element, $schema, $path, $i); |
| 71 | + } |
| 72 | + |
| 73 | + private function fmod($number1, $number2) |
| 74 | + { |
| 75 | + $modulus = ($number1 - round($number1 / $number2) * $number2); |
| 76 | + $precision = 0.0000000001; |
| 77 | + |
| 78 | + if (-$precision < $modulus && $modulus < $precision) { |
| 79 | + return 0.0; |
| 80 | + } |
| 81 | + |
| 82 | + return $modulus; |
| 83 | + } |
| 84 | +} |
0 commit comments