Passed
Pull Request — develop (#279)
by Mikaël
09:14 queued 07:02
created

ChoiceRule::getValidationMethodName()   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
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
8
use WsdlToPhp\PackageGenerator\File\Element\PhpFunctionParameter;
9
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
10
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
11
12
final class ChoiceRule extends AbstractRule
13
{
14 8
    public const NAME = 'choice';
15
16 8
    public function name(): string
17
    {
18
        return self::NAME;
19 8
    }
20
21 8
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
22 8
    {
23 8
        $test = '';
24 8
        if (is_array($value) && 0 < count($value)) {
25
            $this->addValidationMethod($parameterName, $value);
26
            $test = sprintf('\'\' !== (%s = self::%s($%s))', self::getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName);
27 8
        }
28
29
        return $test;
30 8
    }
31
32 8
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
33
    {
34
        return self::getErrorMessageVariableName($parameterName);
35 8
    }
36
37 8
    public static function getErrorMessageVariableName(string $parameterName): string
38
    {
39
        return sprintf('$%sChoiceErrorMessage', $parameterName);
40 8
    }
41
42 8
    protected function addValidationMethod(string $parameterName, array $choiceNames)
43 8
    {
44 8
        $attribute = $this->getAttribute();
45 8
        $struct = $attribute->getOwner();
46 8
        $choiceAttributes = [];
47 8
        foreach ($choiceNames as $choiceName) {
48
            if ($choiceName !== $attribute->getName() && $choiceAttribute = $struct->getAttribute($choiceName)) {
49
                $choiceAttributes[] = $choiceAttribute;
50
            }
51 8
        }
52 8
53 8
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
54
            new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE),
55 8
        ], AbstractModelFile::TYPE_STRING);
56 8
57 8
        $method
58 8
            ->addChild('$message = \'\';')
59 8
            ->addChild('if (is_null($value)) {')
60 8
            ->addChild($method->getIndentedString('return $message;', 1))
61 8
            ->addChild('}')
62
            ->addChild('$properties = [')
63 8
        ;
64 8
65 8
        array_walk($choiceAttributes, function (StructAttribute $choiceAttribute) use ($method) {
66
            $method->addChild($method->getIndentedString(sprintf('%s,', var_export($choiceAttribute->getCleanName(), true)), 1));
67 8
        });
68 8
69 8
        $method
70 8
            ->addChild('];')
71 8
            ->addChild('try {')
72 8
            ->addChild($method->getIndentedString('foreach ($properties as $property) {', 1))
73 8
            ->addChild($method->getIndentedString('if (isset($this->{$property})) {', 2))
74 8
            ->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))
75 8
            ->addChild($method->getIndentedString(sprintf('}'), 2))
76 8
            ->addChild($method->getIndentedString('}', 1))
77 8
            ->addChild('} catch (InvalidArgumentException $e) {')
78 8
            ->addChild($method->getIndentedString('$message = $e->getMessage();', 1))
79 8
            ->addChild('}')
80 8
            ->addChild('')
81
            ->addChild('return $message;')
82 8
        ;
83
84
        $this->getMethods()->add($method);
85 8
    }
86
}
87