Passed
Push — php-7.1 ( 541704...349118 )
by SignpostMarv
01:52
created

GroupRef::loadGroupRef()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
1
<?php
2
declare(strict_types = 1);
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
    public function getMin() : int
33
    {
34
        return $this->min;
35
    }
36
37
    /**
38
    * @return $this
39
    */
40 45
    public function setMin(int $min) : self
41
    {
42 45
        $this->min = $min;
43 45
        return $this;
44
    }
45
46 1
    public function getMax() : int
47
    {
48 1
        return $this->max;
49
    }
50
51
    /**
52
    * @return $this
53
    */
54 45
    public function setMax(int $max) : self
55
    {
56 45
        $this->max = $max;
57 45
        return $this;
58
    }
59
60 1
    public function getName() : string
61
    {
62 1
        return $this->wrapped->getName();
63
    }
64
65
    /**
66
    * @return ElementItem[]
67
    */
68 1
    public function getElements() : array
69
    {
70 1
        $elements = $this->wrapped->getElements();
71 1
        if($this->getMax()>0 || $this->getMax()===-1){
72 1
            foreach ($elements as $k => $element) {
73
                /**
74
                * @var Element|ElementRef|ElementSingle|GroupRef $e
75
                */
76 1
                $e = clone $element;
77 1
                $e->setMax($this->getMax());
78 1
                $elements[$k] = $e;
79
            }
80
        }
81 1
        return $elements;
82
    }
83
84
    public function addElement(ElementItem $element) : void
85
    {
86
        throw new BadMethodCallException("Can't add an element for a ref group");
87
    }
88
89 45
    public static function loadGroupRef(
90
        Group $referenced,
91
        DOMElement $node
92
    ) : self {
93 45
        $ref = new GroupRef($referenced);
94 45
        $ref->setDoc(SchemaReader::getDocumentation($node));
95
96 45
        SchemaReader::maybeSetMax($ref, $node);
97 45
        SchemaReader::maybeSetMin($ref, $node);
98
99 45
        return $ref;
100
    }
101
}
102