Passed
Push — php-7.1 ( ed6d10...451fb2 )
by SignpostMarv
02:49
created

Group::getSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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