Passed
Push — php-7.1 ( b6da63...280be0 )
by SignpostMarv
01:58
created

Group::loadGroupBeforeCheckingChildNodes()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 26
ccs 7
cts 11
cp 0.6364
crap 4.7691
rs 8.5806
1
<?php
2
declare(strict_types = 1);
3
namespace GoetasWebservices\XML\XSDReader\Schema\Element;
4
5
use Closure;
6
use DOMElement;
7
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItemTrait;
8
use GoetasWebservices\XML\XSDReader\Schema\Schema;
9
use GoetasWebservices\XML\XSDReader\SchemaReader;
10
11
class Group implements ElementItem, ElementContainer
12
{
13
    use AttributeItemTrait;
14
    use ElementContainerTrait;
15
16
    /**
17
     *
18
     * @var Schema
19
     */
20
    protected $schema;
21
22
    /**
23
    * @var string
24
    */
25
    protected $doc = '';
26
27 45
    public function __construct(Schema $schema, string $name)
28
    {
29 45
        $this->schema = $schema;
30 45
        $this->name = $name;
31 45
    }
32
33
    public function getDoc() : string
34
    {
35
        return $this->doc;
36
    }
37
38
    /**
39
    * @return $this
40
    */
41 45
    public function setDoc(string $doc) : self
42
    {
43 45
        $this->doc = $doc;
44 45
        return $this;
45
    }
46
47 45
    public function getSchema() : Schema
48
    {
49 45
        return $this->schema;
50
    }
51
52 45
    protected static function loadGroupBeforeCheckingChildNodes(
53
        Schema $schema,
54
        DOMElement $node
55
    ) : Group {
56 45
        $group = new Group($schema, $node->getAttribute("name"));
57 45
        $group->setDoc(SchemaReader::getDocumentation($node));
58
59 45
        if ($node->hasAttribute("maxOccurs")) {
60
            /**
61
            * @var GroupRef $group
62
            */
63
            $group = SchemaReader::maybeSetMax(new GroupRef($group), $node);
64
        }
65 45
        if ($node->hasAttribute("minOccurs")) {
66
            /**
67
            * @var GroupRef $group
68
            */
69
            $group = SchemaReader::maybeSetMin(
70
                $group instanceof GroupRef ? $group : new GroupRef($group),
71
                $node
72
            );
73
        }
74
75 45
        $schema->addGroup($group);
76
77 45
        return $group;
78
    }
79
80 45
    public static function loadGroup(
81
        SchemaReader $reader,
82
        Schema $schema,
83
        DOMElement $node
84
    ) : Closure {
85 45
        $group = static::loadGroupBeforeCheckingChildNodes(
86 45
            $schema,
87 45
            $node
88
        );
89
90 45
        static $methods = [
91
            'sequence' => 'loadSequence',
92
            'choice' => 'loadSequence',
93
            'all' => 'loadSequence',
94
        ];
95
96 45
        return function () use ($reader, $group, $node, $methods) : void {
97 45
            foreach ($node->childNodes as $childNode) {
98 45
                $reader->maybeCallMethod(
99 45
                    $methods,
100 45
                    (string) $childNode->localName,
101 45
                    $childNode,
102 45
                    $group,
103 45
                    $childNode
104
                );
105
            }
106 45
        };
107
    }
108
}
109