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