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