Passed
Push — 2.x ( bef6c0...34f135 )
by Mikaël
03:54
created

ChoiceRule   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A getErrorMessageVariableName() 0 3 1
A getValidationMethodName() 0 3 1
A exceptionMessageOnTestFailure() 0 3 1
A testConditions() 0 8 3
A addValidationMethod() 0 40 4
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
6
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
9
class ChoiceRule extends AbstractRule
10
{
11
12
    /**
13
     * @return string
14
     */
15
    public function name()
16
    {
17
        return 'choice';
18
    }
19
20
    /**
21
     * @param string $parameterName
22
     * @param mixed $value
23
     * @param bool $itemType
24
     * @return string
25
     */
26
    public function testConditions($parameterName, $value, $itemType = false)
27
    {
28
        $test = '';
29
        if (is_array($value) && 0 < count($value)) {
30
            $this->addValidationMethod($parameterName, $value);
31
            $test = sprintf('\'\' !== (%s = self::%s($%s))', self::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName);
32
        }
33
        return $test;
34
    }
35
36
    /**
37
     * @param string $parameterName
38
     * @param mixed $value
39
     * @param bool $itemType
40
     * @return string
41
     */
42
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
43
    {
44
        return self::getErrorMessageVariableName($parameterName);
45
    }
46
47
    /**
48
     * @param string $parameterName
49
     * @param string[] $choiceNames
50
     */
51
    protected function addValidationMethod($parameterName, array $choiceNames)
52
    {
53
        $attribute = $this->getAttribute();
54
        $struct = $attribute->getOwner();
55
        $choiceAttributes = [];
56
        foreach ($choiceNames as $choiceName) {
57
            if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) {
58
                $choiceAttributes[] = $choiceAttribute;
59
            }
60
        }
61
62
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
63
            new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE),
64
        ]);
65
66
        $method
67
            ->addChild('$message = \'\';')
68
            ->addChild('if (is_null($value)) {')
69
            ->addChild($method->getIndentedString('return $message;', 1))
70
            ->addChild('}')
71
            ->addChild('$properties = [');
72
73
        array_walk($choiceAttributes, function (StructAttribute $choiceAttribute) use ($method) {
74
            $method->addChild($method->getIndentedString(sprintf('%s,', var_export($choiceAttribute->getCleanName(), true)), 1));
75
        });
76
77
        $method
78
            ->addChild('];')
79
            ->addChild('try {')
80
            ->addChild($method->getIndentedString('foreach ($properties as $property) {', 1))
81
            ->addChild($method->getIndentedString('if (isset($this->{$property})) {', 2))
82
            ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The property %1$s can\\\'t be set as the property %%s is already set. Only one property must be set among these properties: %1$s, %%s.\', $property, implode(\', \', $properties)), __LINE__);', $attribute->getName()), 3))
83
            ->addChild($method->getIndentedString(sprintf('}'), 2))
84
            ->addChild($method->getIndentedString('}', 1))
85
            ->addChild('} catch (\InvalidArgumentException $e) {')
86
            ->addChild($method->getIndentedString('$message = $e->getMessage();', 1))
87
            ->addChild('}')
88
            ->addChild('return $message;');
89
90
        $this->getMethods()->add($method);
91
    }
92
93
    /**
94
     * @param string $parameterName
95
     * @return string
96
     */
97
    protected function getValidationMethodName($parameterName)
98
    {
99
        return sprintf('validate%sForChoiceConstraintsFrom%s', ucfirst($parameterName), ucFirst($this->getMethod()->getName()));
100
    }
101
102
    /**
103
     * @param string $parameterName
104
     * @return string
105
     */
106
    public static function getErrorMessageVariableName($parameterName)
107
    {
108
        return sprintf('$%sChoiceErrorMessage', $parameterName);
109
    }
110
}
111