|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer; |
|
6
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
|
7
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
|
8
|
|
|
use WsdlToPhp\PackageGenerator\File\AbstractModelFile; |
|
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
|
|
|
* @var MethodContainer |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $methods; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $rulesAppliedToAttribute = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param AbstractModelFile $file |
|
41
|
|
|
* @param PhpMethod $method |
|
42
|
|
|
* @param StructAttribute $attribute |
|
43
|
|
|
*/ |
|
44
|
444 |
|
public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods) |
|
45
|
|
|
{ |
|
46
|
444 |
|
$this->file = $file; |
|
47
|
444 |
|
$this->method = $method; |
|
48
|
444 |
|
$this->attribute = $attribute; |
|
49
|
444 |
|
$this->methods = $methods; |
|
50
|
444 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $parameterName |
|
54
|
|
|
* @param bool $itemType |
|
55
|
|
|
*/ |
|
56
|
318 |
|
public function applyRules($parameterName, $itemType = false) |
|
57
|
|
|
{ |
|
58
|
318 |
|
if ($this->attribute->isArray() && !$itemType) { |
|
59
|
150 |
|
$this->getArrayRule()->applyRule($parameterName, null, $itemType); |
|
60
|
318 |
|
} elseif ($this->attribute->isList() && !$itemType) { |
|
61
|
18 |
|
$this->getListRule()->applyRule($parameterName, null, $itemType); |
|
62
|
318 |
|
} elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) { |
|
63
|
144 |
|
$this->getEnumerationRule()->applyRule($parameterName, null); |
|
64
|
315 |
|
} elseif ($itemType) { |
|
65
|
144 |
|
$this->getItemTypeRule()->applyRule($parameterName, null); |
|
66
|
294 |
|
} elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) { |
|
67
|
234 |
|
$rule->applyRule($parameterName, null, $itemType); |
|
68
|
117 |
|
} |
|
69
|
318 |
|
$this->applyRulesFromAttribute($parameterName, $itemType); |
|
70
|
318 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Generic method to apply rules from current model |
|
74
|
|
|
* @param string $parameterName |
|
75
|
|
|
* @param bool $itemType |
|
76
|
|
|
*/ |
|
77
|
318 |
|
protected function applyRulesFromAttribute($parameterName, $itemType = false) |
|
78
|
|
|
{ |
|
79
|
318 |
|
foreach ($this->attribute->getMeta() as $metaName => $metaValue) { |
|
80
|
282 |
|
$rule = $this->getRule($metaName); |
|
81
|
282 |
|
if ($rule instanceof AbstractRule) { |
|
82
|
266 |
|
$rule->applyRule($parameterName, $metaValue, $itemType); |
|
83
|
129 |
|
} |
|
84
|
159 |
|
} |
|
85
|
318 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @return AbstractRule|null |
|
90
|
|
|
*/ |
|
91
|
318 |
|
public function getRule($name) |
|
92
|
|
|
{ |
|
93
|
318 |
|
if (is_string($name)) { |
|
|
|
|
|
|
94
|
318 |
|
$className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name)); |
|
95
|
318 |
|
if (class_exists($className)) { |
|
96
|
288 |
|
return new $className($this); |
|
97
|
|
|
} |
|
98
|
159 |
|
} |
|
99
|
318 |
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return ArrayRule |
|
104
|
|
|
*/ |
|
105
|
150 |
|
public function getArrayRule() |
|
106
|
|
|
{ |
|
107
|
150 |
|
return $this->getRule('array'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return EnumerationRule |
|
112
|
|
|
*/ |
|
113
|
156 |
|
public function getEnumerationRule() |
|
114
|
|
|
{ |
|
115
|
156 |
|
return $this->getRule('enumeration'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return ItemTypeRule |
|
120
|
|
|
*/ |
|
121
|
144 |
|
public function getItemTypeRule() |
|
122
|
|
|
{ |
|
123
|
144 |
|
return $this->getRule('itemType'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return ListRule |
|
128
|
|
|
*/ |
|
129
|
18 |
|
public function getListRule() |
|
130
|
|
|
{ |
|
131
|
18 |
|
return $this->getRule('list'); |
|
132
|
|
|
} |
|
133
|
|
|
/** |
|
134
|
|
|
* @return StructAttribute |
|
135
|
|
|
*/ |
|
136
|
408 |
|
public function getAttribute() |
|
137
|
|
|
{ |
|
138
|
408 |
|
return $this->attribute; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param StructAttribute $attribute |
|
143
|
|
|
* @return Rules |
|
144
|
|
|
*/ |
|
145
|
12 |
|
public function setAttribute(StructAttribute $attribute) |
|
146
|
|
|
{ |
|
147
|
12 |
|
$this->attribute = $attribute; |
|
148
|
12 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return AbstractModelFile |
|
153
|
|
|
*/ |
|
154
|
318 |
|
public function getFile() |
|
155
|
|
|
{ |
|
156
|
318 |
|
return $this->file; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return PhpMethod |
|
161
|
|
|
*/ |
|
162
|
408 |
|
public function getMethod() |
|
163
|
|
|
{ |
|
164
|
408 |
|
return $this->method; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param PhpMethod $method |
|
169
|
|
|
* @return Rules |
|
170
|
|
|
*/ |
|
171
|
156 |
|
public function setMethod(PhpMethod $method) |
|
172
|
|
|
{ |
|
173
|
156 |
|
$this->method = $method; |
|
174
|
156 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
/** |
|
177
|
|
|
* @return MethodContainer |
|
178
|
|
|
*/ |
|
179
|
192 |
|
public function getMethods() |
|
180
|
|
|
{ |
|
181
|
192 |
|
return $this->methods; |
|
182
|
|
|
} |
|
183
|
|
|
/** |
|
184
|
|
|
* @return Generator |
|
185
|
|
|
*/ |
|
186
|
12 |
|
public function getGenerator() |
|
187
|
|
|
{ |
|
188
|
12 |
|
return $this->file->getGenerator(); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param AbstractRule $rule |
|
193
|
|
|
* @param string|string[] $value |
|
194
|
|
|
* @param StructAttribute $attribute |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
408 |
|
private static function getAppliedRuleToAttributeKey(AbstractRule $rule, $value, StructAttribute $attribute) |
|
198
|
|
|
{ |
|
199
|
408 |
|
return implode('_', [ |
|
200
|
408 |
|
$rule->validationRuleComment($value), |
|
201
|
408 |
|
$attribute->getOwner()->getName(), |
|
202
|
408 |
|
$attribute->getName(), |
|
203
|
204 |
|
]); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param AbstractRule $rule |
|
208
|
|
|
* @param string|string[] $value |
|
209
|
|
|
* @param StructAttribute $attribute |
|
210
|
|
|
* @retrun void |
|
211
|
|
|
*/ |
|
212
|
408 |
|
public static function ruleHasBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute) |
|
213
|
|
|
{ |
|
214
|
408 |
|
self::$rulesAppliedToAttribute[self::getAppliedRuleToAttributeKey($rule, $value, $attribute)] = true; |
|
215
|
408 |
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param AbstractRule $rule |
|
219
|
|
|
* @param string|string[] $value |
|
220
|
|
|
* @param StructAttribute $attribute |
|
221
|
|
|
* @return bool |
|
222
|
|
|
*/ |
|
223
|
12 |
|
public static function hasRuleBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute) |
|
224
|
|
|
{ |
|
225
|
12 |
|
return array_key_exists(self::getAppliedRuleToAttributeKey($rule, $value, $attribute), self::$rulesAppliedToAttribute); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|