Completed
Push — master ( ab2db9...9b8f8b )
by Mikaël
23:04
created

getErrorMessageVariableName()   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 1
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\Model\Struct;
6
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
9
abstract class AbstractSetOfValuesRule extends AbstractRule
10
{
11
    /**
12
     * Must check the attribute validity according to the current rule
13
     * @return bool
14
     */
15
    abstract protected function mustApplyRuleOnAttribute();
16
17
    /**
18
     * @param string $parameterName
19
     * @param mixed $value
20
     * @param bool $itemType
21
     * @return string
22
     */
23 120
    public function testConditions($parameterName, $value, $itemType = false)
24
    {
25 120
        $test = '';
26 120
        if ($this->mustApplyRuleOnAttribute()) {
27 120
            $this->addValidationMethod($parameterName);
28 120
            $test = sprintf('\'\' !== (%s = self::%s($%s))', self::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName);
29 60
        }
30 120
        return $test;
31
    }
32
33
    /**
34
     * @param string $parameterName
35
     * @param mixed $value
36
     * @param bool $itemType
37
     * @return string
38
     */
39 120
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
40
    {
41 120
        return self::getErrorMessageVariableName($parameterName);
42
    }
43
44
    /**
45
     * @param string $parameterName
46
     */
47 120
    protected function addValidationMethod($parameterName)
48
    {
49 120
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
50 120
            new PhpFunctionParameter('values', [], 'array'),
51 120
        ], PhpMethod::ACCESS_PUBLIC, false, true);
52 120
        $model = $this->getFile()->getRestrictionFromStructAttribute($this->getAttribute());
53 120
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
54 120
        $rules = clone $this->getRules();
55
56 120
        if ($model instanceof Struct) {
57 36
            $rule = $rules->getEnumerationRule();
58 18
        } else {
59 108
            $rule = $rules->setMethod($method)->getItemTypeRule();
60
        }
61
62
        $method
63 120
            ->addChild('$message = \'\';')
64 120
            ->addChild('$invalidValues = [];')
65 120
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
66 120
            ->addChild($method->getIndentedString(sprintf('// validation for constraint: %s', $rule->name()), 1))
67 120
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $rule->testConditions($itemName, null)), 1))
68 120
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = %s;', sprintf('is_object($%1$s) ? get_class($%1$s) : var_export($%1$s, true)', $itemName)), 2))
69 120
            ->addChild($method->getIndentedString('}', 1))
70 120
            ->addChild('}')
71 120
            ->addChild('if (!empty($invalidValues)) {')
72 120
            ->addChild($method->getIndentedString(sprintf('$message = %s;', $rule->exceptionMessageOnTestFailure('invalidValues', null)), 1))
73 120
            ->addChild('}')
74 120
            ->addChild('unset($invalidValues);')
75 120
            ->addChild('return $message;');
76 120
        $this->getMethods()->add($method);
77 120
    }
78
79
    /**
80
     * @param string $parameterName
81
     * @return string
82
     */
83 120
    protected function getValidationMethodName($parameterName)
84
    {
85 120
        return sprintf('validate%sForArrayContraintsFrom%s', ucfirst($parameterName), ucFirst($this->getMethod()->getName()));
86
    }
87
88
    /**
89
     * @param string $parameterName
90
     * @return string
91
     */
92 120
    public static function getErrorMessageVariableName($parameterName)
93
    {
94 120
        return sprintf('$%sArrayErrorMessage', $parameterName);
95
    }
96
}
97