Completed
Pull Request — master (#18)
by SignpostMarv
03:30
created

Type::loadTypeWithCallback()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

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