Test Failed
Pull Request — master (#18)
by SignpostMarv
03:04
created

Type::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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