Passed
Push — develop ( 393819...8447c9 )
by Mikaël
74:52 queued 24:50
created

AbstractSetOfValuesRule::getParameterPassedValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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\File\AbstractModelFile;
8
use WsdlToPhp\PackageGenerator\Model\Struct;
9
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
10
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
11
12
abstract class AbstractSetOfValuesRule extends AbstractRule
13
{
14
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
15
    {
16
        $test = '';
17
        if ($this->mustApplyRuleOnAttribute()) {
18
            $this->addValidationMethod($parameterName, $value);
19
            $test = sprintf('\'\' !== (%s = self::%s(%s))', static::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), static::getParameterPassedValue($parameterName));
20
        }
21
22
        return $test;
23
    }
24 168
25
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
26 168
    {
27 168
        return static::getErrorMessageVariableName($parameterName);
28 168
    }
29 168
30 84
    public static function getErrorMessageVariableName(string $parameterName): string
31 168
    {
32
        return sprintf('$%sArrayErrorMessage', $parameterName);
33
    }
34
35
    public static function getParameterPassedValue(string $parameterName): string
36
    {
37
        return sprintf('$%s', $parameterName);
38
    }
39
40 168
    /**
41
     * Must check the attribute validity according to the current rule.
42 168
     */
43
    abstract protected function mustApplyRuleOnAttribute(): bool;
44
45
    protected function addValidationMethod(string $parameterName, $value)
46
    {
47
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
48
            new PhpFunctionParameter('values', [], 'array'),
49 168
        ], AbstractModelFile::TYPE_STRING, PhpMethod::ACCESS_PUBLIC, false, true);
50
        $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
51 168
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
52 168
        $rules = clone $this->getRules();
53 168
54 168
        if ($model instanceof Struct) {
55 168
            $rule = $rules->getEnumerationRule();
56 168
        } else {
57
            $rule = $rules->setMethod($method)->getItemTypeRule();
58 168
        }
59 42
60 21
        $method
61 150
            ->addChild('$message = \'\';')
62
            ->addChild('$invalidValues = [];')
63
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
64
            ->addChild($method->getIndentedString($rule->validationRuleComment($value), 1))
65 168
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1))
66 168
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = %s;', sprintf('is_object($%1$s) ? get_class($%1$s) : sprintf(\'%%s(%%s)\', gettype($%1$s), var_export($%1$s, true))', $itemName)), 2))
67 168
            ->addChild($method->getIndentedString('}', 1))
68 168
            ->addChild('}')
69 168
            ->addChild('if (!empty($invalidValues)) {')
70 168
            ->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1))
71 168
            ->addChild('}')
72 168
            ->addChild('unset($invalidValues);')
73 168
            ->addChild('')
74 168
            ->addChild('return $message;')
75 168
        ;
76 168
        $this->getMethods()->add($method);
77 168
    }
78 168
79 168
    protected function getValidationMethodName(string $parameterName): string
80
    {
81
        return sprintf('validate%sForArrayConstraintsFrom%s', ucfirst($parameterName), ucfirst($this->getMethod()->getName()));
82
    }
83
}
84