1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WsdlToPhp\PackageGenerator\File\Validation; |
4
|
|
|
|
5
|
|
|
use WsdlToPhp\PackageGenerator\Generator\Generator; |
6
|
|
|
use WsdlToPhp\PhpGenerator\Element\PhpMethod; |
7
|
|
|
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel; |
8
|
|
|
use WsdlToPhp\PackageGenerator\File\AbstractModelFile; |
9
|
|
|
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer; |
10
|
|
|
|
11
|
|
|
abstract class AbstractRule |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Rules |
16
|
|
|
*/ |
17
|
|
|
protected $rules; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Rules $rules |
21
|
|
|
*/ |
22
|
846 |
|
public function __construct(Rules $rules) |
23
|
|
|
{ |
24
|
846 |
|
$this->rules = $rules; |
25
|
846 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This method has to add the validation rule to the method's body |
29
|
|
|
* @param string $parameterName |
30
|
|
|
* @param mixed $value |
31
|
|
|
* @param bool $itemType |
32
|
|
|
* @return AbstractRule |
33
|
|
|
*/ |
34
|
846 |
|
final public function applyRule($parameterName, $value, $itemType = false) |
35
|
|
|
{ |
36
|
846 |
|
$test = $this->testConditions($parameterName, $value, $itemType); |
37
|
846 |
|
if (!empty($test)) { |
38
|
846 |
|
$message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType); |
39
|
846 |
|
$this->getMethod() |
40
|
846 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('// validation for constraint: %s', $this->name()), $itemType ? 1 : 0)) |
41
|
846 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('if (%s) {', $test), $itemType ? 1 : 0)) |
42
|
846 |
|
->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(%s, __LINE__);', $message), $itemType ? 2 : 1)) |
43
|
846 |
|
->addChild($this->getMethod()->getIndentedString('}', $itemType ? 1 : 0)); |
44
|
846 |
|
unset($message); |
45
|
423 |
|
} |
46
|
846 |
|
unset($test); |
47
|
846 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Name of the validation rule |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
abstract public function name(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Inline tests of the validation rule |
57
|
|
|
* @param string $parameterName |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @param bool $itemType |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
abstract public function testConditions($parameterName, $value, $itemType = false); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Message when test fails in order to throw the exception |
66
|
|
|
* @param string $parameterName |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @param bool $itemType |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
abstract public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Rules |
75
|
|
|
*/ |
76
|
174 |
|
public function getRules() |
77
|
|
|
{ |
78
|
174 |
|
return $this->rules; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return PhpMethod |
83
|
|
|
*/ |
84
|
846 |
|
public function getMethod() |
85
|
|
|
{ |
86
|
846 |
|
return $this->rules->getMethod(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return MethodContainer |
91
|
|
|
*/ |
92
|
150 |
|
public function getMethods() |
93
|
|
|
{ |
94
|
150 |
|
return $this->rules->getMethods(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return AbstractModelFile |
99
|
|
|
*/ |
100
|
207 |
|
public function getFile() |
101
|
|
|
{ |
102
|
207 |
|
return $this->rules->getFile(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return StructAttributeModel |
107
|
|
|
*/ |
108
|
231 |
|
public function getAttribute() |
109
|
|
|
{ |
110
|
231 |
|
return $this->rules->getAttribute(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return Generator |
115
|
|
|
*/ |
116
|
6 |
|
public function getGenerator() |
117
|
|
|
{ |
118
|
6 |
|
return $this->rules->getGenerator(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|