Passed
Push — develop ( 9c7e6c...a31d48 )
by Mikaël
09:44
created

AbstractRule::getValidationMethodName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 as StructAttributeModel;
11
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
12
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
13
14
abstract class AbstractRule
15
{
16
    public const VALIDATION_RULE_COMMENT_SENTENCE = 'validation for constraint:';
17
18
    protected Rules $rules;
19
20 154
    public function __construct(Rules $rules)
21
    {
22 154
        $this->rules = $rules;
23
    }
24
25 152
    final public function applyRule(string $parameterName, $value, bool $itemType = false): void
26
    {
27 152
        $test = $this->testConditions($parameterName, $value, $itemType);
28 152
        if (!empty($test)) {
29 152
            $message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType);
30 152
            $this
31 152
                ->getMethod()
32 152
                ->addChild($this->validationRuleComment($value))
33 152
                ->addChild(sprintf('if (%s) {', $test))
34 152
                ->addChild($this->getMethod()->getIndentedString(sprintf('throw new InvalidArgumentException(%s, __LINE__);', $message), 1))
35 152
                ->addChild('}')
36 152
            ;
37 152
            unset($message);
38 152
            Rules::ruleHasBeenAppliedToAttribute($this, $value, $this->getAttribute());
39
        }
40 152
        unset($test);
41
    }
42
43 152
    final public function validationRuleComment($value): string
44
    {
45 152
        return sprintf('// %s %s%s', self::VALIDATION_RULE_COMMENT_SENTENCE, $this->name(), is_array($value) ? sprintf('(%s)', implode(', ', array_unique($value))) : (empty($value) ? '' : sprintf('(%s)', $value)));
46
    }
47
48
    abstract public function name(): string;
49
50
    abstract public function testConditions(string $parameterName, $value, bool $itemType = false): string;
51
52
    abstract public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string;
53
54 68
    public function getRules(): Rules
55
    {
56 68
        return $this->rules;
57
    }
58
59 152
    public function getMethod(): PhpMethod
60
    {
61 152
        return $this->rules->getMethod();
62
    }
63
64 72
    public function getMethods(): MethodContainer
65
    {
66 72
        return $this->rules->getMethods();
67
    }
68
69 86
    public function getFile(): AbstractModelFile
70
    {
71 86
        return $this->rules->getFile();
72
    }
73
74 152
    public function getAttribute(): StructAttributeModel
75
    {
76 152
        return $this->rules->getAttribute();
77
    }
78
79 4
    public function getGenerator(): Generator
80
    {
81 4
        return $this->rules->getGenerator();
82
    }
83
84 14
    final protected function addArrayValidationMethod(string $parameterName, $value): void
85
    {
86 14
        $itemName = sprintf(
87 14
            '%s%sItem',
88 14
            lcfirst($this->getFile()->getModel()->getCleanName(false)),
89 14
            ucfirst($this->getAttribute()->getCleanName())
90 14
        );
91 14
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
92 14
            new PhpFunctionParameter('values', null, '?array'),
93 14
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
94
95 14
        $method
96 14
            ->addChild('$message = \'\';')
97 14
            ->addChild('$invalidValues = [];')
98 14
            ->addChild(sprintf('foreach (($values ?? []) as $%s) {', $itemName))
99 14
            ->addChild($method->getIndentedString($this->validationRuleComment($value), 1))
100 14
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1))
101 14
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2))
102 14
            ->addChild($method->getIndentedString('}', 1))
103 14
            ->addChild('}')
104 14
            ->addChild('if (!empty($invalidValues)) {')
105 14
            ->addChild($method->getIndentedString(
106 14
                sprintf(
107 14
                    '$message = sprintf(\'%s\', implode(\', \', $invalidValues));',
108 14
                    addslashes($this->getArrayExceptionMessageOnTestFailure($value)),
109 14
                ),
110 14
                1
111 14
            ))
112 14
            ->addChild('}')
113 14
            ->addChild('unset($invalidValues);')
114 14
            ->addChild('')
115 14
            ->addChild('return $message;')
116 14
        ;
117 14
        $this->getMethods()->add($method);
118
    }
119
120
    protected function getArrayExceptionMessageOnTestFailure($value): string
121
    {
122
        return '';
123
    }
124
125 72
    final protected function getValidationMethodName(string $parameterName): string
126
    {
127 72
        return sprintf(
128 72
            'validate%sFor%sConstraintFrom%s',
129 72
            ucfirst($parameterName),
130 72
            ucfirst($this->name()),
131 72
            ucfirst($this->getMethod()->getName())
132 72
        );
133
    }
134
135 14
    final protected function getArrayErrorMessageVariableName(string $parameterName): string
136
    {
137 14
        return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name()));
138
    }
139
}
140