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