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

Type::loadTypeWithCallbackOnChildNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 4
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 1
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace GoetasWebservices\XML\XSDReader\Schema\Type;
4
5
use Closure;
6
use DOMElement;
7
use GoetasWebservices\XML\XSDReader\SchemaReader;
8
use GoetasWebservices\XML\XSDReader\Schema\Schema;
9
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem;
10
use GoetasWebservices\XML\XSDReader\Schema\SchemaItemTrait;
11
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Extension;
12
use GoetasWebservices\XML\XSDReader\Schema\Inheritance\Restriction;
13
14
abstract class Type implements SchemaItem
15
{
16
    use SchemaItemTrait;
17
18
    /**
19
     * @var string|null
20
     */
21
    protected $name;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $abstract = false;
27
28
    /**
29
     * @var Restriction|null
30
     */
31
    protected $restriction;
32
33
    /**
34
     * @var Extension|null
35
     */
36
    protected $extension;
37
38
    /**
39
     * @param string|null $name
40
     */
41 45
    public function __construct(Schema $schema, $name = null)
42
    {
43 45
        $this->name = $name ?: null;
44 45
        $this->schema = $schema;
45 45
    }
46
47
    /**
48
     * @return string|null
49
     */
50 45
    public function getName()
51
    {
52 45
        return $this->name;
53
    }
54
55
    public function __toString()
56
    {
57
        return strval($this->name);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function isAbstract()
64
    {
65
        return $this->abstract;
66
    }
67
68
    /**
69
     * @param bool $abstract
70
     *
71
     * @return $this
72
     */
73 45
    public function setAbstract($abstract)
74
    {
75 45
        $this->abstract = $abstract;
76
77 45
        return $this;
78
    }
79
80
    /**
81
     * @return Restriction|Extension|null
82
     */
83
    public function getParent()
84
    {
85
        return $this->restriction ?: $this->extension;
86
    }
87
88
    /**
89
     * @return Restriction|null
90
     */
91
    public function getRestriction()
92
    {
93
        return $this->restriction;
94
    }
95
96
    /**
97
     * @return $this
98
     */
99 45
    public function setRestriction(Restriction $restriction)
100
    {
101 45
        $this->restriction = $restriction;
102
103 45
        return $this;
104
    }
105
106
    /**
107
     * @return Extension|null
108
     */
109 3
    public function getExtension()
110
    {
111 3
        return $this->extension;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117 45
    public function setExtension(Extension $extension)
118
    {
119 45
        $this->extension = $extension;
120
121 45
        return $this;
122
    }
123
124 45
    public static function loadTypeWithCallbackOnChildNodes(
125
        SchemaReader $schemaReader,
126
        Schema $schema,
127
        DOMElement $node,
128
        Closure $callback
129
    ) {
130 45
        SchemaReader::againstDOMNodeList(
131 45
            $node,
132 45
            function (
133
                DOMElement $node,
134
                DOMElement $childNode
135
            ) use (
136 45
                $schemaReader,
137 45
                $schema,
138 45
                $callback
139
            ) {
140 45
                static::loadTypeWithCallback(
141 45
                    $schemaReader,
142 45
                    $schema,
143 45
                    $childNode,
144
                    $callback
145 45
                );
146 45
            }
147 45
        );
148 45
    }
149
150 45
    public static function loadTypeWithCallback(
151
        SchemaReader $schemaReader,
152
        Schema $schema,
153
        DOMElement $childNode,
154
        Closure $callback
155
    ) {
156
        $methods = [
157 45
            'complexType' => 'loadComplexType',
158 45
            'simpleType' => 'loadSimpleType',
159 45
        ];
160
161
        /**
162
         * @var Closure|null
163
         */
164 45
        $func = $schemaReader->maybeCallMethod(
165 45
            $methods,
166 45
            $childNode->localName,
167 45
            $childNode,
168 45
            $schema,
169 45
            $childNode,
170
            $callback
171 45
        );
172
173 45
        if ($func instanceof Closure) {
174 45
            call_user_func($func);
175 45
        }
176 45
    }
177
}
178