Completed
Push — master ( 9b8f8b...ad8621 )
by Mikaël
29:42
created

AbstractRule::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 string
16
     */
17
    const VALIDATION_RULE_COMMENT_SENTENCE = 'validation for constraint:';
18
19
    /**
20
     * @var Rules
21
     */
22
    protected $rules;
23
24
    /**
25
     * @param Rules $rules
26
     */
27 420
    public function __construct(Rules $rules)
28
    {
29 420
        $this->rules = $rules;
30 420
    }
31
32
    /**
33
     * This method has to add the validation rule to the method's body
34
     * @param string $parameterName
35
     * @param string|string[] $value
36
     * @param bool $itemType
37
     * @return AbstractRule
38
     */
39 414
    final public function applyRule($parameterName, $value, $itemType = false)
40 3
    {
41 414
        $test = $this->testConditions($parameterName, $value, $itemType);
42 414
        if (!empty($test)) {
43 414
            $message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType);
44 414
            $this->getMethod()
45 414
                ->addChild($this->validationRuleComment($value))
46 414
                ->addChild(sprintf('if (%s) {', $test))
47 414
                ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(%s, __LINE__);', $message), 1))
48 414
                ->addChild('}');
49 414
            unset($message);
50 414
            Rules::ruleHasBeenAppliedToAttribute($this, $value, $this->getAttribute());
51 207
        }
52 414
        unset($test);
53 414
    }
54
55
    /**
56
     * @param string|string[] $value
57
     * @return string
58
     */
59 414
    final public function validationRuleComment($value)
60
    {
61 414
        return sprintf('// %s %s%s', self::VALIDATION_RULE_COMMENT_SENTENCE, $this->name(), is_array($value) ? sprintf('(%s)', implode(', ', $value)) : (empty($value) ? '' : sprintf('(%s)', $value)));
62
    }
63
64
    /**
65
     * Name of the validation rule
66
     * @return string
67
     */
68
    abstract public function name();
69
70
    /**
71
     * Inline tests of the validation rule
72
     * @param string $parameterName
73
     * @param string|string[] $value
74
     * @param bool $itemType
75
     * @return string
76
     */
77
    abstract public function testConditions($parameterName, $value, $itemType = false);
78
79
    /**
80
     * Message when test fails in order to throw the exception
81
     * @param string $parameterName
82
     * @param string|string[] $value
83
     * @param bool $itemType
84
     * @return string
85
     */
86
    abstract public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false);
87
88
    /**
89
     * @return Rules
90
     */
91 174
    public function getRules()
92
    {
93 174
        return $this->rules;
94
    }
95
96
    /**
97
     * @return PhpMethod
98
     */
99 414
    public function getMethod()
100
    {
101 414
        return $this->rules->getMethod();
102
    }
103
104
    /**
105
     * @return MethodContainer
106
     */
107 192
    public function getMethods()
108
    {
109 192
        return $this->rules->getMethods();
110
    }
111
112
    /**
113
     * @return AbstractModelFile
114
     */
115 222
    public function getFile()
116
    {
117 222
        return $this->rules->getFile();
118
    }
119
120
    /**
121
     * @return StructAttributeModel
122
     */
123 414
    public function getAttribute()
124
    {
125 414
        return $this->rules->getAttribute();
126
    }
127
128
    /**
129
     * @return Generator
130
     */
131 12
    public function getGenerator()
132
    {
133 12
        return $this->rules->getGenerator();
134
    }
135
}
136