1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
4
|
|
|
|
5
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
6
|
|
|
use WsdlToPhp\PackageGenerator\File\AbstractModelFile; |
7
|
|
|
use WsdlToPhp\PackageGenerator\Model\AbstractModel; |
8
|
|
|
use WsdlToPhp\PackageGenerator\Model\Struct; |
9
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute; |
10
|
|
|
|
11
|
|
|
class Rules |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var StructAttribute |
16
|
|
|
*/ |
17
|
|
|
protected $attribute; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var AbstractModelFile |
21
|
|
|
*/ |
22
|
|
|
protected $file; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var PhpMethod |
26
|
|
|
*/ |
27
|
|
|
protected $method; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param AbstractModelFile $file |
31
|
|
|
* @param PhpMethod $method |
32
|
|
|
* @param StructAttribute $attribute |
33
|
|
|
*/ |
34
|
786 |
|
public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute) |
35
|
|
|
{ |
36
|
393 |
|
$this |
37
|
786 |
|
->setFile($file) |
38
|
786 |
|
->setMethod($method) |
39
|
786 |
|
->setAttribute($attribute); |
40
|
786 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $parameterName |
44
|
|
|
* @param bool $itemType |
45
|
|
|
*/ |
46
|
234 |
|
public function applyRules($parameterName, $itemType = false) |
47
|
|
|
{ |
48
|
234 |
|
$this->applyRulesFromModel($this->attribute, $parameterName, $itemType); |
49
|
234 |
|
if ($this->getAttribute()->isArray() && !$itemType) { |
50
|
114 |
|
$this->getArrayRule()->applyRule($parameterName, null, $itemType); |
51
|
234 |
|
} elseif ($this->getFile()->getRestrictionFromStructAttribute($this->getAttribute())) { |
52
|
102 |
|
$this->getEnumerationRule()->applyRule($parameterName, null, $itemType); |
53
|
231 |
|
} elseif ($itemType) { |
54
|
108 |
|
$this->getItemTypeRule()->applyRule($parameterName, null, $itemType); |
55
|
210 |
|
} elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute()))) instanceof AbstractRule) { |
56
|
150 |
|
$rule->applyRule($parameterName, null, $itemType); |
57
|
75 |
|
} |
58
|
234 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* This method is called when an attribute has a union meta which means the attribute is of several types. |
62
|
|
|
* In this case, the types are currently only of type string (normally) so we add the rules according to each type |
63
|
|
|
* @param string $parameterName |
64
|
|
|
* @param bool $itemType |
65
|
|
|
* @param string[] $unionTypes |
66
|
|
|
*/ |
67
|
6 |
|
protected function applyUnionRules($parameterName, $itemType, array $unionTypes) |
68
|
|
|
{ |
69
|
6 |
|
foreach ($unionTypes as $type) { |
70
|
6 |
|
$struct = $this->getAttribute()->getGenerator()->getStructByName($type); |
71
|
6 |
|
if ($struct instanceof Struct) { |
72
|
6 |
|
$this->applyRulesFromModel($struct, $parameterName, $itemType); |
73
|
3 |
|
} |
74
|
3 |
|
} |
75
|
6 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generic method to apply rules from current model |
79
|
|
|
* @param AbstractModel $model |
80
|
|
|
* @param string $parameterName |
81
|
|
|
* @param bool $itemType |
82
|
|
|
*/ |
83
|
234 |
|
protected function applyRulesFromModel(AbstractModel $model, $parameterName, $itemType = false) |
84
|
|
|
{ |
85
|
234 |
|
foreach ($model->getMeta() as $metaName => $metaValue) { |
86
|
198 |
|
$rule = $this->getRule($metaName); |
87
|
198 |
|
if ($rule instanceof AbstractRule) { |
88
|
48 |
|
$rule->applyRule($parameterName, $metaValue, $itemType); |
89
|
198 |
|
} elseif ($metaName === 'union' && is_array($metaValue) && count($metaValue) > 0) { |
90
|
6 |
|
$this->applyUnionRules($parameterName, $itemType, $metaValue); |
91
|
198 |
|
} elseif ($metaName === 'choiceNames' && is_array($metaValue) && count($metaValue) > 0) { |
92
|
74 |
|
$this->getChoiceRule()->applyRule($parameterName, $metaValue, $itemType); |
93
|
6 |
|
} |
94
|
117 |
|
} |
95
|
234 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $metaName |
99
|
|
|
* @return AbstractRule|null |
100
|
|
|
*/ |
101
|
234 |
|
protected function getRule($metaName) |
102
|
|
|
{ |
103
|
234 |
|
if (is_string($metaName)) { |
|
|
|
|
104
|
234 |
|
$className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($metaName)); |
105
|
234 |
|
if (class_exists($className)) { |
106
|
204 |
|
return new $className($this); |
107
|
|
|
} |
108
|
117 |
|
} |
109
|
234 |
|
return null; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return ArrayRule |
114
|
|
|
*/ |
115
|
114 |
|
public function getArrayRule() |
116
|
|
|
{ |
117
|
114 |
|
return $this->getRule('array'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return EnumerationRule |
122
|
|
|
*/ |
123
|
102 |
|
public function getEnumerationRule() |
124
|
|
|
{ |
125
|
102 |
|
return $this->getRule('enumeration'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ItemTypeRule |
130
|
|
|
*/ |
131
|
108 |
|
public function getItemTypeRule() |
132
|
|
|
{ |
133
|
108 |
|
return $this->getRule('itemType'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return ChoiceRule |
138
|
|
|
*/ |
139
|
12 |
|
public function getChoiceRule() |
140
|
|
|
{ |
141
|
12 |
|
return $this->getRule('choice'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return StructAttribute |
146
|
|
|
*/ |
147
|
282 |
|
public function getAttribute() |
148
|
|
|
{ |
149
|
282 |
|
return $this->attribute; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param StructAttribute $attribute |
154
|
|
|
* @return Rules |
155
|
|
|
*/ |
156
|
786 |
|
public function setAttribute(StructAttribute $attribute) |
157
|
|
|
{ |
158
|
786 |
|
$this->attribute = $attribute; |
159
|
786 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return AbstractModelFile |
164
|
|
|
*/ |
165
|
282 |
|
public function getFile() |
166
|
|
|
{ |
167
|
282 |
|
return $this->file; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param AbstractModelFile $file |
172
|
|
|
* @return Rules |
173
|
|
|
*/ |
174
|
786 |
|
public function setFile(AbstractModelFile $file) |
175
|
|
|
{ |
176
|
786 |
|
$this->file = $file; |
177
|
786 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return PhpMethod |
182
|
|
|
*/ |
183
|
756 |
|
public function getMethod() |
184
|
|
|
{ |
185
|
756 |
|
return $this->method; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param PhpMethod $method |
190
|
|
|
* @return Rules |
191
|
|
|
*/ |
192
|
786 |
|
public function setMethod(PhpMethod $method) |
193
|
|
|
{ |
194
|
786 |
|
$this->method = $method; |
195
|
786 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|