|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2013-2015 |
|
4
|
|
|
* Piotr Olaszewski <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
* SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
namespace WSDL\XML\Styles; |
|
25
|
|
|
|
|
26
|
|
|
use Ouzo\Utilities\Inflector; |
|
27
|
|
|
use WSDL\Parser\MethodParser; |
|
28
|
|
|
use WSDL\Types\Type; |
|
29
|
|
|
use WSDL\Utilities\Strings; |
|
30
|
|
|
use WSDL\Utilities\TypeHelper; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Style |
|
34
|
|
|
* |
|
35
|
|
|
* @author Piotr Olaszewski <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract class Style |
|
38
|
|
|
{ |
|
39
|
|
|
abstract public function bindingStyle(); |
|
40
|
|
|
|
|
41
|
|
|
abstract public function bindingUse(); |
|
42
|
|
|
|
|
43
|
|
|
abstract public function methodInput(MethodParser $method); |
|
44
|
|
|
|
|
45
|
|
|
abstract public function methodOutput(MethodParser $method); |
|
46
|
|
|
|
|
47
|
|
|
abstract public function typeParameters(MethodParser $method); |
|
48
|
|
|
|
|
49
|
|
|
abstract public function typeReturning(MethodParser $method); |
|
50
|
|
|
|
|
51
|
7 |
|
protected function _createElement(Type $returning) |
|
52
|
|
|
{ |
|
53
|
7 |
|
list($type, $value) = $this->_prepareTypeAndValue($returning); |
|
54
|
|
|
$element = array( |
|
55
|
7 |
|
'name' => $returning->getName(), |
|
56
|
|
|
$type => $value |
|
57
|
7 |
|
); |
|
58
|
7 |
|
return $element; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
30 |
|
protected function _prepareTypeAndValue(Type $parameter) |
|
62
|
|
|
{ |
|
63
|
30 |
|
$type = ''; |
|
64
|
30 |
|
$value = ''; |
|
65
|
30 |
|
if (TypeHelper::isSimple($parameter)) { |
|
66
|
27 |
|
$type = 'type'; |
|
67
|
27 |
|
$value = TypeHelper::getXsdType($parameter->getType()); |
|
68
|
30 |
|
} elseif (TypeHelper::isArray($parameter)) { |
|
69
|
19 |
|
$type = 'type'; |
|
70
|
19 |
|
$value = 'ns:' . 'ArrayOf' . ucfirst($parameter->getName()); |
|
71
|
26 |
|
} elseif (TypeHelper::isObject($parameter)) { |
|
72
|
18 |
|
$type = 'element'; |
|
73
|
18 |
|
$value = 'ns:' . $this->_getObjectName($parameter); |
|
74
|
18 |
|
} |
|
75
|
30 |
|
return array($type, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
32 |
|
protected function _getObjectName(Type $parameter) |
|
79
|
|
|
{ |
|
80
|
32 |
|
return $parameter->getType() == 'object' ? ucfirst($parameter->getName()) : $parameter->getType(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
19 |
|
protected function _generateType($parameter) |
|
84
|
|
|
{ |
|
85
|
19 |
|
if (!TypeHelper::isSimple($parameter)) { |
|
86
|
19 |
|
$generateComplexType = $this->_generateComplexType($parameter); |
|
87
|
19 |
|
if ($generateComplexType) { |
|
88
|
19 |
|
return $generateComplexType; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
7 |
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
32 |
|
protected function _generateComplexType(Type $parameter) |
|
95
|
|
|
{ |
|
96
|
32 |
|
if (TypeHelper::isArray($parameter)) { |
|
97
|
15 |
|
return $this->_generateArray($parameter); |
|
98
|
|
|
} |
|
99
|
24 |
|
if (TypeHelper::isObject($parameter)) { |
|
100
|
24 |
|
return $this->_generateObject($parameter); |
|
101
|
|
|
} |
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
23 |
|
protected function _generateArray(Type $parameter) |
|
106
|
|
|
{ |
|
107
|
23 |
|
$type = $parameter->getComplexType() ? 'ns:' : 'xsd:'; |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
23 |
|
$typesComplex = new TypesComplex(); |
|
110
|
|
|
$typesComplex |
|
111
|
23 |
|
->setName('ArrayOf' . ucfirst($parameter->getName())) |
|
112
|
23 |
|
->setArrayType($type . $this->_getObjectName($parameter) . '[]') |
|
113
|
23 |
|
->setArrayTypeName(Inflector::singularize($parameter->getName())); |
|
114
|
|
|
|
|
115
|
23 |
|
if ($parameter->getComplexType()) { |
|
|
|
|
|
|
116
|
15 |
|
$typesComplex->setComplex($this->_generateObject($parameter->getComplexType())); |
|
|
|
|
|
|
117
|
15 |
|
} |
|
118
|
|
|
|
|
119
|
23 |
|
return $typesComplex; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
28 |
|
protected function _generateObject(Type $parameter) |
|
123
|
|
|
{ |
|
124
|
28 |
|
$typesElement = new TypesElement(); |
|
125
|
28 |
|
$typesElement->setName($this->_getObjectName($parameter)); |
|
126
|
|
|
|
|
127
|
28 |
|
$types = is_array($parameter->getComplexType()) ? $parameter->getComplexType() : $parameter->getComplexType()->getComplexType(); |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
28 |
|
foreach ($types as $complexType) { |
|
130
|
28 |
|
if ($complexType instanceof Type) { |
|
131
|
27 |
|
list($type, $value) = $this->_prepareTypeAndValue($complexType); |
|
132
|
27 |
|
} else { |
|
133
|
16 |
|
$type = 'type'; |
|
134
|
16 |
|
$value = TypeHelper::getXsdType($complexType->getType()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
28 |
|
$typesElement->setElementAttributes($type, $value, $complexType->getName()); |
|
138
|
|
|
|
|
139
|
28 |
|
$this->_setComplexTypeIfNeeded($complexType, $typesElement); |
|
140
|
28 |
|
} |
|
141
|
28 |
|
return $typesElement; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
28 |
|
protected function _setComplexTypeIfNeeded($complexType, TypesElement $typesElement) |
|
145
|
|
|
{ |
|
146
|
28 |
|
if (TypeHelper::isArray($complexType)) { |
|
147
|
8 |
|
$typesElement->setComplex($this->_generateArray($complexType)); |
|
148
|
28 |
|
} elseif ($complexType instanceof Type && !TypeHelper::isSimple($complexType) && $complexType->getComplexType()) { |
|
|
|
|
|
|
149
|
4 |
|
$typesElement->setComplex($this->_generateComplexType($complexType->getComplexType())); |
|
|
|
|
|
|
150
|
4 |
|
} |
|
151
|
28 |
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: