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