Passed
Push — feature/issue-165 ( 2b7e50...101bca )
by Mikaël
14:10
created

AbstractSetOfValuesRule::addValidationMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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