Issues (4)

Validation/RangeAnnotation.php (2 issues)

1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Annotations\Validation;
13
14
use BlitzPHP\Annotations\BaseAnnotation;
15
use mindplay\annotations\AnnotationException;
16
17
/**
18
 * Spécifie la validation par rapport à une valeur numérique minimale et/ou maximale.
19
 *
20
 * @usage('property'=>true, 'inherited'=>true)
21
 */
22
class RangeAnnotation extends BaseAnnotation
23
{
24
    /**
25
     * @var float|int
26
     *
27
     * Valeur numérique minimale (entier ou virgule flottante)
28
     */
29
    public $min;
30
31
    /**
32
     * @var float|int
33
     *
34
     * * Valeur numérique maximale (entier ou virgule flottante)
35
     */
36
    public $max;
37
38
    /**
39
     * Initialisation de l'annotation.
40
     */
41
    public function initAnnotation(array $properties)
42
    {
43
        if (isset($properties[0])) {
44
            if (isset($properties[1])) {
45
                $this->min = $properties[0];
46
                $this->max = $properties[1];
47
                unset($properties[1]);
48
            } else {
49
                $this->max = $properties[0];
50
            }
51
52
            unset($properties[0]);
53
        }
54
55
        parent::initAnnotation($properties);
56
57
        if ($this->min !== null && ! is_int($this->min) && ! is_float($this->min)) {
0 ignored issues
show
The condition is_float($this->min) is always true.
Loading history...
58
            throw new AnnotationException('RangeAnnotation requires a numeric (float or int) min property');
59
        }
60
61
        if ($this->max !== null && ! is_int($this->max) && ! is_float($this->max)) {
0 ignored issues
show
The condition is_float($this->max) is always true.
Loading history...
62
            throw new AnnotationException('RangeAnnotation requires a numeric (float or int) max property');
63
        }
64
65
        if ($this->min === null && $this->max === null) {
66
            throw new AnnotationException('RangeAnnotation requires a min and/or max property');
67
        }
68
69
        $this->value = [$this->min, $this->max];
70
    }
71
}
72