|
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\Parser; |
|
25
|
|
|
|
|
26
|
|
|
use Ouzo\Utilities\Arrays as OuzoArrays; |
|
27
|
|
|
use WSDL\Types\Arrays; |
|
28
|
|
|
use WSDL\Types\Object; |
|
29
|
|
|
use WSDL\Types\Simple; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ParameterParser |
|
33
|
|
|
* |
|
34
|
|
|
* @author Piotr Olaszewski <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class ParameterParser |
|
37
|
|
|
{ |
|
38
|
|
|
private $_strategy; |
|
39
|
|
|
private $_parameter; |
|
40
|
|
|
private $_type; |
|
41
|
|
|
private $_name; |
|
42
|
|
|
private $_methodName; |
|
43
|
|
|
private $_optional; |
|
44
|
|
|
|
|
45
|
47 |
|
public function __construct($parameter, $methodName = '') |
|
46
|
|
|
{ |
|
47
|
47 |
|
$this->_parameter = trim($parameter); |
|
48
|
47 |
|
$this->_methodName = $methodName; |
|
49
|
47 |
|
} |
|
50
|
|
|
|
|
51
|
46 |
|
public function parse() |
|
52
|
|
|
{ |
|
53
|
46 |
|
return $this->_detectType(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
46 |
|
private function _detectType() |
|
57
|
|
|
{ |
|
58
|
46 |
|
$this->_parseAndSetType(); |
|
59
|
46 |
|
$this->_parseAndSetName(); |
|
60
|
46 |
|
$this->_parseAndSetOptional(); |
|
61
|
|
|
|
|
62
|
46 |
|
switch ($this->_strategy) { |
|
63
|
46 |
|
case 'object': |
|
64
|
28 |
|
return new Object($this->getType(), $this->getName(), $this->complexTypes(), $this->getOptional()); |
|
65
|
46 |
|
case 'wrapper': |
|
66
|
14 |
|
return $this->_createWrapperObject(); |
|
67
|
44 |
|
case 'array': |
|
68
|
28 |
|
return $this->_createArrayObject(); |
|
69
|
38 |
|
default: |
|
70
|
38 |
|
return new Simple($this->getType(), $this->getName(), $this->getOptional()); |
|
71
|
38 |
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
46 |
|
private function _parseAndSetType() |
|
75
|
|
|
{ |
|
76
|
46 |
|
if (preg_match('#^(\w*)\[\]#', $this->_parameter, $type)) { |
|
77
|
28 |
|
$this->_type = $type[1]; |
|
78
|
28 |
|
$this->_strategy = 'array'; |
|
79
|
28 |
|
} else { |
|
80
|
40 |
|
preg_match('#(\w+)#', $this->_parameter, $type); |
|
81
|
40 |
|
if (isset($type[1])) { |
|
82
|
40 |
|
$this->_type = $type[1]; |
|
83
|
40 |
|
$this->_strategy = $type[1]; |
|
84
|
40 |
|
} else { |
|
85
|
8 |
|
$this->_type = 'void'; |
|
86
|
8 |
|
$this->_strategy = 'void'; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
46 |
|
} |
|
90
|
|
|
|
|
91
|
46 |
|
private function _parseAndSetName() |
|
92
|
|
|
{ |
|
93
|
46 |
|
preg_match('#\\$(\w+)#', $this->_parameter, $name); |
|
94
|
46 |
|
$this->_name = OuzoArrays::getValue($name, 1, ''); |
|
95
|
46 |
|
} |
|
96
|
|
|
|
|
97
|
46 |
|
private function _parseAndSetOptional() |
|
98
|
|
|
{ |
|
99
|
46 |
|
preg_match('#\\$(\w+)#', $this->_parameter, $name); |
|
100
|
46 |
|
$this->_optional = !!preg_match('#@optional#', $this->_parameter, $matches); |
|
101
|
46 |
|
} |
|
102
|
|
|
|
|
103
|
14 |
|
private function _createWrapperObject() |
|
104
|
|
|
{ |
|
105
|
14 |
|
$wrapper = $this->wrapper(); |
|
106
|
14 |
|
$object = null; |
|
107
|
14 |
|
if ($wrapper->getComplexTypes()) { |
|
108
|
14 |
|
$object = new Object($this->getType(), $this->getName(), $wrapper->getComplexTypes(), $this->getOptional()); |
|
109
|
14 |
|
} |
|
110
|
14 |
|
return new Object($this->getType(), $this->getName(), $object, $this->getOptional()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
28 |
|
private function _createArrayObject() |
|
114
|
|
|
{ |
|
115
|
28 |
|
$object = null; |
|
116
|
28 |
|
if ($this->_type == 'wrapper') { |
|
117
|
16 |
|
$complex = $this->wrapper()->getComplexTypes(); |
|
118
|
16 |
|
$object = new Object($this->getType(), $this->getName(), $complex, $this->getOptional()); |
|
119
|
28 |
|
} elseif ($this->isComplex()) { |
|
120
|
5 |
|
$complex = $this->complexTypes(); |
|
121
|
5 |
|
$object = new Object($this->getType(), $this->getName(), $complex, $this->getOptional()); |
|
122
|
5 |
|
} |
|
123
|
28 |
|
return new Arrays($this->getType(), $this->getName(), $object, $this->getOptional()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
47 |
|
public function getType() |
|
127
|
|
|
{ |
|
128
|
47 |
|
return $this->_type; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
46 |
|
public function getName() |
|
132
|
|
|
{ |
|
133
|
46 |
|
return $this->_name; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
46 |
|
public function getOptional() |
|
137
|
|
|
{ |
|
138
|
46 |
|
return $this->_optional; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
33 |
|
public function complexTypes() |
|
142
|
|
|
{ |
|
143
|
33 |
|
if (!$this->isComplex()) { |
|
144
|
1 |
|
throw new ParameterParserException("This parameter is not complex type."); |
|
145
|
|
|
} |
|
146
|
32 |
|
preg_match("#(@.+)#", $this->_parameter, $complexTypes); |
|
147
|
32 |
|
return ComplexTypeParser::create($complexTypes[1]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
22 |
|
public function wrapper() |
|
151
|
|
|
{ |
|
152
|
22 |
|
if (!$this->isComplex()) { |
|
153
|
|
|
throw new ParameterParserException("This parameter is not complex type."); |
|
154
|
|
|
} |
|
155
|
22 |
|
preg_match('#@className=(.*?)(?:\s|$)#', $this->_parameter, $matches); |
|
156
|
22 |
|
$className = $matches[1]; |
|
157
|
22 |
|
$this->_type = str_replace('\\', '', $className); |
|
158
|
22 |
|
$wrapperParser = new WrapperParser($className); |
|
159
|
22 |
|
$wrapperParser->parse(); |
|
160
|
22 |
|
return $wrapperParser; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
42 |
|
public function isComplex() |
|
164
|
|
|
{ |
|
165
|
42 |
|
return in_array($this->getType(), array('object', 'wrapper')); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param array $parameters |
|
170
|
|
|
* @param $methodName |
|
171
|
|
|
* @return ParameterParser[] |
|
172
|
|
|
*/ |
|
173
|
32 |
|
public static function create($parameters, $methodName) |
|
174
|
|
|
{ |
|
175
|
32 |
|
$obj = array(); |
|
176
|
32 |
|
foreach ($parameters as $parameter) { |
|
177
|
31 |
|
$parser = new self($parameter, $methodName); |
|
178
|
31 |
|
$obj[] = $parser->parse(); |
|
179
|
32 |
|
} |
|
180
|
32 |
|
return $obj; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|