AbstractConverter::convert()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
namespace Goetas\Xsd\XsdToPhp;
3
4
use Goetas\Xsd\XsdToPhp\Naming\NamingStrategy;
5
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle;
6
use GoetasWebservices\XML\XSDReader\Schema\Schema;
7
use GoetasWebservices\XML\XSDReader\Schema\Type\ComplexType;
8
use GoetasWebservices\XML\XSDReader\Schema\Type\SimpleType;
9
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
10
11
abstract class AbstractConverter
12
{
13
14
    protected $baseSchemas = array(
15
        'http://www.w3.org/2001/XMLSchema',
16
        'http://www.w3.org/XML/1998/namespace'
17
    );
18
19
    protected $namespaces = array(
20
        'http://www.w3.org/2001/XMLSchema' => '',
21
        'http://www.w3.org/XML/1998/namespace' => ''
22
    );
23
24
    protected $baseTypes = [
25
        'string',
26
        'float',
27
        'boolean',
28
        'date',
29
        'integer',
30
        'mixed'
31
    ];
32
    /**
33
     * @var \Goetas\Xsd\XsdToPhp\Naming\NamingStrategy
34
     */
35
    private $namingStrategy;
36
37
    public abstract function convert(array $schemas);
38
39
    protected $typeAliases = array();
40
41
    protected $aliasCache = array();
42
43 54
    public function addAliasMap($ns, $name, callable $handler)
44
    {
45 54
        $this->typeAliases[$ns][$name] = $handler;
46 54
    }
47
48 2
    public function addAliasMapType($ns, $name, $type)
49
    {
50
        $this->addAliasMap($ns, $name, function () use ($type) {
51 2
            return $type;
52 2
        });
53 2
    }
54
55 49
    public function getTypeAlias($type, Schema $schemapos = null)
56
    {
57 49
        $schema = $schemapos ?: $type->getSchema();
58
59 49
        $cid = $schema->getTargetNamespace() . "|" . $type->getName();
60 49
        if (isset($this->aliasCache[$cid])) {
61 11
            return $this->aliasCache[$cid];
62
        }
63 49
        if (isset($this->typeAliases[$schema->getTargetNamespace()][$type->getName()])) {
64 49
            return $this->aliasCache[$cid] = call_user_func($this->typeAliases[$schema->getTargetNamespace()][$type->getName()], $type);
65
        }
66 28
    }
67
68 54
    public function __construct(NamingStrategy $namingStrategy)
69
    {
70 54
        $this->namingStrategy = $namingStrategy;
71
72
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYearMonth", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
            return "integer";
74 54
        });
75
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonthDay", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
            return "integer";
77 54
        });
78
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gMonth", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            return "integer";
80 54
        });
81
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "gYear", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
            return "integer";
83 54
        });
84
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NMTOKEN", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
            return "string";
86 54
        });
87
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NMTOKENS", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
            return "string";
89 54
        });
90
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "QName", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return "string";
92 54
        });
93
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NCName", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
            return "string";
95 54
        });
96
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "decimal", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
97 5
            return "float";
98 54
        });
99
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "float", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
            return "float";
101 54
        });
102
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "double", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
            return "float";
104 54
        });
105
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "string", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106 24
            return "string";
107 54
        });
108
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "normalizedString", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
            return "string";
110 54
        });
111
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "integer", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112 5
            return "integer";
113 54
        });
114
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "int", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115 4
            return "integer";
116 54
        });
117
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedInt", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
            return "integer";
119 54
        });
120
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "negativeInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
            return "integer";
122 54
        });
123
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
            return "integer";
125 54
        });
126
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
            return "integer";
128 54
        });
129
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonPositiveInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
            return "integer";
131 54
        });
132
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "long", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133 4
            return "integer";
134 54
        });
135
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "unsignedLong", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
            return "integer";
137 54
        });
138
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "short", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
            return "integer";
140 54
        });
141
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "boolean", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
            return "boolean";
143 54
        });
144
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
            return "integer";
146 54
        });
147
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "positiveInteger", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
            return "integer";
149 54
        });
150
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "language", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151 1
            return "string";
152 54
        });
153
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "token", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
            return "string";
155 54
        });
156
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "anyURI", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
            return "string";
158 54
        });
159
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "byte", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
            return "string";
161 54
        });
162
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "duration", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
            return "DateInterval";
164 54
        });
165
166
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "ID", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
            return "string";
168 54
        });
169
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "IDREF", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
            return "string";
171 54
        });
172
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "IDREFS", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
173
            return "string";
174 54
        });
175
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "Name", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176 1
            return "string";
177 54
        });
178
179 54
        $this->addAliasMap("http://www.w3.org/2001/XMLSchema", "NCName", function (Type $type) {
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
            return "string";
181 54
        });
182 54
    }
183
184
    /**
185
     * @return \Goetas\Xsd\XsdToPhp\Naming\NamingStrategy
186
     */
187 49
    protected function getNamingStrategy()
188
    {
189 49
        return $this->namingStrategy;
190
    }
191
192 52
    public function addNamespace($namesapce, $phpNamespace)
193
    {
194 52
        $this->namespaces[$namesapce] = $phpNamespace;
195 52
        return $this;
196
    }
197
198
    protected function cleanName($name)
199
    {
200
        return preg_replace("/<.*>/", "", $name);
201
    }
202
203
    /**
204
     * @param Type $type
205
     * @return \GoetasWebservices\XML\XSDReader\Schema\Type\Type|null
206
     */
207 29
    protected function isArrayType(Type $type)
208
    {
209 29
        if ($type instanceof SimpleType) {
210 28
            return $type->getList();
211
        }
212 28
    }
213
214
    /**
215
     * @param Type $type
216
     * @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null
217
     */
218 29
    protected function isArrayNestedElement(Type $type)
219
    {
220 29
        if ($type instanceof ComplexType && !$type->getParent() && !$type->getAttributes() && count($type->getElements()) === 1) {
221 21
            $elements = $type->getElements();
222 21
            return $this->isArrayElement(reset($elements));
223
        }
224 28
    }
225
226
    /**
227
     * @param ElementSingle $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
228
     * @return \GoetasWebservices\XML\XSDReader\Schema\Element\ElementSingle|null
229
     */
230 27
    protected function isArrayElement($element)
231
    {
232 27
        if ($element instanceof ElementSingle && ($element->getMax() > 1 || $element->getMax() === -1)) {
233 9
            return $element;
234
        }
235 25
    }
236
237
}
238