Completed
Pull Request — master (#1184)
by Alexey
12:56
created

Repetition::isInfinite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Hoa
4
 *
5
 *
6
 * @license
7
 *
8
 * BSD 3-Clause License
9
 *
10
 * Copyright © 2007-2017, Hoa community. All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions are met:
14
 *
15
 * 1. Redistributions of source code must retain the above copyright notice, this
16
 *    list of conditions and the following disclaimer.
17
 *
18
 * 2. Redistributions in binary form must reproduce the above copyright notice,
19
 *    this list of conditions and the following disclaimer in the documentation
20
 *    and/or other materials provided with the distribution.
21
 *
22
 * 3. Neither the name of the copyright holder nor the names of its
23
 *    contributors may be used to endorse or promote products derived from
24
 *    this software without specific prior written permission.
25
 *
26
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 */
38
39
namespace JMS\Serializer\Type\Compiler\Llk\Rule;
40
41
use JMS\Serializer\Type\Compiler;
42
43
/**
44
 * Class \JMS\Serializer\Type\Compiler\Llk\Rule\Repetition.
45
 *
46
 * The repetition rule.
47
 *
48
 * @copyright  Copyright © 2007-2017 Hoa community
49
 * @license    New BSD License
50
 */
51
final class Repetition extends Rule
0 ignored issues
show
Deprecated Code introduced by
The class JMS\Serializer\Type\Compiler\Llk\Rule\Rule has been deprecated: alias ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

51
final class Repetition extends /** @scrutinizer ignore-deprecated */ Rule
Loading history...
52
{
53
    /**
54
     * Minimum bound.
55
     *
56
     * @var int
57
     */
58
    protected $_min = 0;
59
60
    /**
61
     * Maximum bound.
62
     *
63
     * @var int
64
     */
65
    protected $_max = 0;
66
67
68
69
    /**
70
     * Constructor.
71
     *
72
     * @param   string  $name        Name.
73
     * @param   int     $min         Minimum bound.
74
     * @param   int     $max         Maximum bound.
75
     * @param   mixed   $children    Children.
76
     * @param   string  $nodeId      Node ID.
77
     */
78
    public function __construct($name, $min, $max, $children, $nodeId)
79
    {
80
        parent::__construct($name, $children, $nodeId);
81
82
        $min = max(0, (int) $min);
83
        $max = max(-1, (int) $max);
84
85
        if (-1 !== $max && $min > $max) {
86
            throw new Compiler\Exception\Rule(
87
                'Cannot repeat with a min (%d) greater than max (%d).',
88
                0,
89
                [$min, $max]
90
            );
91
        }
92
93
        $this->_min = $min;
94
        $this->_max = $max;
95
96
        return;
97
    }
98
99
    /**
100
     * Get minimum bound.
101
     *
102
     * @return  int
103
     */
104
    public function getMin()
105
    {
106
        return $this->_min;
107
    }
108
109
    /**
110
     * Get maximum bound.
111
     *
112
     * @return  int
113
     */
114
    public function getMax()
115
    {
116
        return $this->_max;
117
    }
118
119
    /**
120
     * Check whether the maximum repetition is unbounded.
121
     *
122
     * @return   bool
123
     */
124
    public function isInfinite()
125
    {
126
        return -1 === $this->getMax();
127
    }
128
}
129