Completed
Pull Request — master (#18)
by SignpostMarv
02:40
created

GroupRef::setMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace GoetasWebservices\XML\XSDReader\Schema\Element;
4
5
use BadMethodCallException;
6
use DOMElement;
7
use GoetasWebservices\XML\XSDReader\SchemaReader;
8
9
class GroupRef extends Group implements InterfaceSetMinMax
10
{
11
    /**
12
     * @var Group
13
     */
14
    protected $wrapped;
15
16
    /**
17
     * @var int
18
     */
19
    protected $min = 1;
20
21
    /**
22
     * @var int
23
     */
24
    protected $max = 1;
25
26 45
    public function __construct(Group $group)
27
    {
28 45
        parent::__construct($group->getSchema(), '');
29 45
        $this->wrapped = $group;
30 45
    }
31
32
    /**
33
     * @return int
34
     */
35
    public function getMin()
36
    {
37
        return $this->min;
38
    }
39
40
    /**
41
     * @param int $min
42
     *
43
     * @return $this
44
     */
45 45
    public function setMin($min)
46
    {
47 45
        $this->min = $min;
48
49 45
        return $this;
50
    }
51
52
    /**
53
     * @return int
54
     */
55 1
    public function getMax()
56
    {
57 1
        return $this->max;
58
    }
59
60
    /**
61
     * @param int $max
62
     *
63
     * @return $this
64
     */
65 45
    public function setMax($max)
66
    {
67 45
        $this->max = $max;
68
69 45
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public function getName()
76
    {
77 1
        return $this->wrapped->getName();
78
    }
79
80
    /**
81
     * @return ElementItem[]
82
     */
83 1
    public function getElements()
84
    {
85 1
        $elements = $this->wrapped->getElements();
86 1
        if ($this->getMax() > 0 || $this->getMax() === -1) {
87 1
            foreach ($elements as $k => $element) {
88
                /**
89
                 * @var Element|ElementRef|ElementSingle|GroupRef $e
90
                 */
91 1
                $e = clone $element;
92 1
                $e->setMax($this->getMax());
93 1
                $elements[$k] = $e;
94 1
            }
95 1
        }
96
97 1
        return $elements;
98
    }
99
100
    public function addElement(ElementItem $element)
101
    {
102
        throw new BadMethodCallException("Can't add an element for a ref group");
103
    }
104
105
    /**
106
     * @return GroupRef
107
     */
108 45
    public static function loadGroupRef(Group $referenced, DOMElement $node)
109
    {
110 45
        $ref = new self($referenced);
111 45
        $ref->setDoc(SchemaReader::getDocumentation($node));
112
113 45
        SchemaReader::maybeSetMax($ref, $node);
114 45
        SchemaReader::maybeSetMin($ref, $node);
115
116 45
        return $ref;
117
    }
118
}
119