Completed
Pull Request — master (#1184)
by Alexey
14:33
created

Repetition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isInfinite() 0 3 1
A getMax() 0 3 1
A __construct() 0 19 3
A getMin() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Hoa
7
 *
8
 *
9
 *
10
 *
11
 * BSD 3-Clause License
12
 *
13
 * Copyright © 2007-2017, Hoa community. All rights reserved.
14
 *
15
 * Redistribution and use in source and binary forms, with or without
16
 * modification, are permitted provided that the following conditions are met:
17
 *
18
 * 1. Redistributions of source code must retain the above copyright notice, this
19
 *    list of conditions and the following disclaimer.
20
 *
21
 * 2. Redistributions in binary form must reproduce the above copyright notice,
22
 *    this list of conditions and the following disclaimer in the documentation
23
 *    and/or other materials provided with the distribution.
24
 *
25
 * 3. Neither the name of the copyright holder nor the names of its
26
 *    contributors may be used to endorse or promote products derived from
27
 *    this software without specific prior written permission.
28
 *
29
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
 */
40
41
namespace JMS\Serializer\Type\Compiler\Llk\Rule;
42
43
use JMS\Serializer\Type\Compiler;
44
use JMS\Serializer\Type\Compiler\Llk\Rule;
45
46
/**
47
 * Class \JMS\Serializer\Type\Compiler\Llk\Rule\Repetition.
48
 *
49
 * The repetition rule.
50
 */
51
final class Repetition extends Rule
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
     * @param   mixed  $name     Name.
71
     * @param   int     $min      Minimum bound.
72
     * @param   int     $max      Maximum bound.
73
     * @param   mixed   $children Children.
74
     * @param   string  $nodeId   Node ID.
75
     */
76
    public function __construct($name, int $min, int $max, $children, $nodeId)
77
    {
78
        parent::__construct($name, $children, $nodeId);
79
80
        $min = max(0, (int) $min);
81
        $max = max(-1, (int) $max);
82
83
        if (-1 !== $max && $min > $max) {
84
            throw new Compiler\Exception\Rule(
85
                'Cannot repeat with a min (%d) greater than max (%d).',
86
                0,
87
                [$min, $max]
88
            );
89
        }
90
91
        $this->_min = $min;
92
        $this->_max = $max;
93
94
        return;
95
    }
96
97
    /**
98
     * Get minimum bound.
99
     */
100
    public function getMin(): int
101
    {
102
        return $this->_min;
103
    }
104
105
    /**
106
     * Get maximum bound.
107
     */
108
    public function getMax(): int
109
    {
110
        return $this->_max;
111
    }
112
113
    /**
114
     * Check whether the maximum repetition is unbounded.
115
     */
116
    public function isInfinite(): bool
117
    {
118
        return -1 === $this->getMax();
119
    }
120
}
121