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