|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the php-annotation framework. |
| 5 | + * |
| 6 | + * (c) Rasmus Schultz <rasmus@mindplay.dk> |
| 7 | + * |
| 8 | + * This software is licensed under the GNU LGPL license |
| 9 | + * for more information, please see: |
| 10 | + * |
| 11 | + * <https://github.com/mindplay-dk/php-annotations> |
| 12 | + */ |
| 13 | + |
| 14 | +namespace mindplay\demo\annotations; |
| 15 | + |
| 16 | +use mindplay\annotations\AnnotationException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Specifies validation against a minimum and/or maximum numeric value. |
| 20 | + * |
| 21 | + * @usage('property'=>true, 'inherited'=>true) |
| 22 | + */ |
| 23 | +class RangeAnnotation extends ValidationAnnotationBase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var mixed $min Minimum numeric value (integer or floating point) |
| 27 | + */ |
| 28 | + public $min = null; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var mixed $max Maximum numeric value (integer or floating point) |
| 32 | + */ |
| 33 | + public $max = null; |
| 34 | + |
| 35 | + /** |
| 36 | + * Initialize the annotation. |
| 37 | + */ |
| 38 | + public function initAnnotation(array $properties) |
| 39 | + { |
| 40 | + if (isset($properties[0])) { |
| 41 | + if (isset($properties[1])) { |
| 42 | + $this->min = $properties[0]; |
| 43 | + $this->max = $properties[1]; |
| 44 | + unset($properties[1]); |
| 45 | + } else { |
| 46 | + $this->max = $properties[0]; |
| 47 | + } |
| 48 | + |
| 49 | + unset($properties[0]); |
| 50 | + } |
| 51 | + |
| 52 | + parent::initAnnotation($properties); |
| 53 | + |
| 54 | + if ($this->min !== null && !is_int($this->min) && !is_float($this->min)) { |
| 55 | + throw new AnnotationException('RangeAnnotation requires a numeric (float or int) min property'); |
| 56 | + } |
| 57 | + |
| 58 | + if ($this->max !== null && !is_int($this->max) && !is_float($this->max)) { |
| 59 | + throw new AnnotationException('RangeAnnotation requires a numeric (float or int) max property'); |
| 60 | + } |
| 61 | + |
| 62 | + if ($this->min === null && $this->max === null) { |
| 63 | + throw new AnnotationException('RangeAnnotation requires a min and/or max property'); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments