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
|
450 |
|
public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods) |
45
|
|
|
{ |
46
|
450 |
|
$this->file = $file; |
47
|
450 |
|
$this->method = $method; |
48
|
450 |
|
$this->attribute = $attribute; |
49
|
450 |
|
$this->methods = $methods; |
50
|
450 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $parameterName |
54
|
|
|
* @param bool $itemType |
55
|
|
|
*/ |
56
|
324 |
|
public function applyRules($parameterName, $itemType = false) |
57
|
|
|
{ |
58
|
324 |
|
if ($this->attribute->isArray() && !$itemType) { |
59
|
150 |
|
$this->getArrayRule()->applyRule($parameterName, null, $itemType); |
60
|
324 |
|
} elseif ($this->attribute->isList() && !$itemType) { |
61
|
18 |
|
$this->getListRule()->applyRule($parameterName, null, $itemType); |
62
|
324 |
|
} elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) { |
63
|
144 |
|
$this->getEnumerationRule()->applyRule($parameterName, null); |
64
|
321 |
|
} elseif ($itemType) { |
65
|
144 |
|
$this->getItemTypeRule()->applyRule($parameterName, null); |
66
|
300 |
|
} elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) { |
67
|
240 |
|
$rule->applyRule($parameterName, null, $itemType); |
68
|
120 |
|
} |
69
|
324 |
|
$this->applyRulesFromAttribute($parameterName, $itemType); |
70
|
324 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generic method to apply rules from current model |
74
|
|
|
* @param string $parameterName |
75
|
|
|
* @param bool $itemType |
76
|
|
|
*/ |
77
|
324 |
|
protected function applyRulesFromAttribute($parameterName, $itemType = false) |
78
|
|
|
{ |
79
|
324 |
|
foreach ($this->attribute->getMeta() as $metaName => $metaValue) { |
80
|
288 |
|
$rule = $this->getRule($metaName); |
81
|
288 |
|
if ($rule instanceof AbstractRule) { |
82
|
272 |
|
$rule->applyRule($parameterName, $metaValue, $itemType); |
83
|
132 |
|
} |
84
|
162 |
|
} |
85
|
324 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $name |
89
|
|
|
* @return AbstractRule|null |
90
|
|
|
*/ |
91
|
324 |
|
public function getRule($name) |
92
|
|
|
{ |
93
|
324 |
|
if (is_string($name)) { |
|
|
|
|
94
|
324 |
|
$className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name)); |
95
|
324 |
|
if (class_exists($className)) { |
96
|
294 |
|
return new $className($this); |
97
|
|
|
} |
98
|
162 |
|
} |
99
|
324 |
|
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
|
414 |
|
public function getAttribute() |
137
|
|
|
{ |
138
|
414 |
|
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
|
324 |
|
public function getFile() |
155
|
|
|
{ |
156
|
324 |
|
return $this->file; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return PhpMethod |
161
|
|
|
*/ |
162
|
414 |
|
public function getMethod() |
163
|
|
|
{ |
164
|
414 |
|
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
|
414 |
|
private static function getAppliedRuleToAttributeKey(AbstractRule $rule, $value, StructAttribute $attribute) |
198
|
|
|
{ |
199
|
414 |
|
return implode('_', [ |
200
|
414 |
|
$rule->validationRuleComment($value), |
201
|
414 |
|
$attribute->getOwner()->getName(), |
202
|
414 |
|
$attribute->getName(), |
203
|
207 |
|
]); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param AbstractRule $rule |
208
|
|
|
* @param string|string[] $value |
209
|
|
|
* @param StructAttribute $attribute |
210
|
|
|
* @retrun void |
211
|
|
|
*/ |
212
|
414 |
|
public static function ruleHasBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute) |
213
|
|
|
{ |
214
|
414 |
|
self::$rulesAppliedToAttribute[self::getAppliedRuleToAttributeKey($rule, $value, $attribute)] = true; |
215
|
414 |
|
} |
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
|
|
|
|