|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer; |
|
8
|
|
|
use WsdlToPhp\PackageGenerator\File\AbstractModelFile; |
|
9
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
|
10
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute; |
|
11
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
|
12
|
|
|
|
|
13
|
|
|
final class Rules |
|
14
|
|
|
{ |
|
15
|
|
|
private StructAttribute $attribute; |
|
16
|
|
|
|
|
17
|
|
|
private AbstractModelFile $file; |
|
18
|
|
|
|
|
19
|
|
|
private PhpMethod $method; |
|
20
|
|
|
|
|
21
|
|
|
private MethodContainer $methods; |
|
22
|
|
|
|
|
23
|
|
|
private static array $rulesAppliedToAttribute = []; |
|
24
|
|
|
|
|
25
|
158 |
|
public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods) |
|
26
|
|
|
{ |
|
27
|
158 |
|
$this->file = $file; |
|
28
|
158 |
|
$this->method = $method; |
|
29
|
158 |
|
$this->attribute = $attribute; |
|
30
|
158 |
|
$this->methods = $methods; |
|
31
|
158 |
|
} |
|
32
|
|
|
|
|
33
|
112 |
|
public function applyRules(string $parameterName, bool $itemType = false): void |
|
34
|
|
|
{ |
|
35
|
112 |
|
if ($this->attribute->isArray() && !$itemType) { |
|
36
|
52 |
|
$this->getArrayRule()->applyRule($parameterName, null, $itemType); |
|
37
|
102 |
|
} elseif ($this->attribute->isXml() && !$itemType) { |
|
38
|
4 |
|
$this->getXmlRule()->applyRule($parameterName, null, $itemType); |
|
39
|
102 |
|
} elseif ($this->attribute->isList() && !$itemType) { |
|
40
|
6 |
|
$this->getListRule()->applyRule($parameterName, null, $itemType); |
|
41
|
102 |
|
} elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) { |
|
42
|
48 |
|
$this->getEnumerationRule()->applyRule($parameterName, null); |
|
43
|
102 |
|
} elseif ($itemType) { |
|
44
|
36 |
|
$this->getItemTypeRule()->applyRule($parameterName, null); |
|
45
|
96 |
|
} elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) { |
|
46
|
82 |
|
$rule->applyRule($parameterName, null, $itemType); |
|
47
|
|
|
} |
|
48
|
112 |
|
$this->applyRulesFromAttribute($parameterName, $itemType); |
|
49
|
112 |
|
} |
|
50
|
|
|
|
|
51
|
112 |
|
public function getRule(string $name): ?AbstractRule |
|
52
|
|
|
{ |
|
53
|
112 |
|
if (is_string($name)) { |
|
|
|
|
|
|
54
|
112 |
|
$className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name)); |
|
55
|
112 |
|
if (class_exists($className)) { |
|
56
|
102 |
|
return new $className($this); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
86 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
52 |
|
public function getArrayRule(): ArrayRule |
|
64
|
|
|
{ |
|
65
|
52 |
|
return $this->getRule('array'); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
public function getXmlRule(): XmlRule |
|
69
|
|
|
{ |
|
70
|
4 |
|
return $this->getRule('xml'); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
60 |
|
public function getEnumerationRule(): EnumerationRule |
|
74
|
|
|
{ |
|
75
|
60 |
|
return $this->getRule('enumeration'); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
50 |
|
public function getItemTypeRule(): ItemTypeRule |
|
79
|
|
|
{ |
|
80
|
50 |
|
return $this->getRule('itemType'); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
public function getListRule(): ListRule |
|
84
|
|
|
{ |
|
85
|
6 |
|
return $this->getRule('list'); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
146 |
|
public function getAttribute(): StructAttribute |
|
89
|
|
|
{ |
|
90
|
146 |
|
return $this->attribute; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
public function setAttribute(StructAttribute $attribute): self |
|
94
|
|
|
{ |
|
95
|
4 |
|
$this->attribute = $attribute; |
|
96
|
|
|
|
|
97
|
4 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
112 |
|
public function getFile(): AbstractModelFile |
|
101
|
|
|
{ |
|
102
|
112 |
|
return $this->file; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
146 |
|
public function getMethod(): PhpMethod |
|
106
|
|
|
{ |
|
107
|
146 |
|
return $this->method; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
54 |
|
public function setMethod(PhpMethod $method): self |
|
111
|
|
|
{ |
|
112
|
54 |
|
$this->method = $method; |
|
113
|
|
|
|
|
114
|
54 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
66 |
|
public function getMethods(): MethodContainer |
|
118
|
|
|
{ |
|
119
|
66 |
|
return $this->methods; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
4 |
|
public function getGenerator(): Generator |
|
123
|
|
|
{ |
|
124
|
4 |
|
return $this->file->getGenerator(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
146 |
|
public static function ruleHasBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): void |
|
128
|
|
|
{ |
|
129
|
146 |
|
self::$rulesAppliedToAttribute[self::getAppliedRuleToAttributeKey($rule, $value, $attribute)] = true; |
|
130
|
146 |
|
} |
|
131
|
|
|
|
|
132
|
4 |
|
public static function hasRuleBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): bool |
|
133
|
|
|
{ |
|
134
|
4 |
|
return array_key_exists(self::getAppliedRuleToAttributeKey($rule, $value, $attribute), self::$rulesAppliedToAttribute); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
112 |
|
private function applyRulesFromAttribute(string $parameterName, bool $itemType = false): void |
|
138
|
|
|
{ |
|
139
|
112 |
|
foreach ($this->attribute->getMeta() as $metaName => $metaValue) { |
|
140
|
100 |
|
$rule = $this->getRule($metaName); |
|
141
|
100 |
|
if ($rule instanceof AbstractRule) { |
|
142
|
94 |
|
$rule->applyRule($parameterName, $metaValue, $itemType); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
112 |
|
} |
|
146
|
|
|
|
|
147
|
146 |
|
private static function getAppliedRuleToAttributeKey(AbstractRule $rule, $value, StructAttribute $attribute): string |
|
148
|
|
|
{ |
|
149
|
146 |
|
return implode('_', [ |
|
150
|
146 |
|
$rule->validationRuleComment($value), |
|
151
|
146 |
|
$attribute->getOwner()->getName(), |
|
152
|
146 |
|
$attribute->getName(), |
|
153
|
|
|
]); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|