Completed
Push — master ( 9b8f8b...ad8621 )
by Mikaël
29:42
created

AbstractLengthRule   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 80
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A exceptionMessageOnTestFailure() 0 8 3
A getValidationMethodName() 0 3 1
A addValidationMethod() 0 22 1
A testConditions() 0 9 4
A getErrorMessageVariableName() 0 3 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
8
/**
9
 * Class AbstractLengthRule
10
 * Gathers [min|max|]Length rules
11
 * @package WsdlToPhp\PackageGenerator\File\Validation
12
 */
13
abstract class AbstractLengthRule extends AbstractMinMaxRule
14
{
15
16
    /**
17
     * @param string $parameterName
18
     * @param mixed $value
19
     * @param bool $itemType
20
     * @return string
21
     */
22 54
    final public function testConditions($parameterName, $value, $itemType = false)
23
    {
24 54
        if ($itemType || !$this->getAttribute()->isArray()) {
25 54
            $test = sprintf(($itemType ? '' : '!is_null($%1$s) && ') . 'mb_strlen($%1$s) %3$s %2$d', $parameterName, $value, $this->symbol());
26 27
        } else {
27 12
            $this->addValidationMethod($parameterName, $value);
28 12
            $test = sprintf('\'\' !== (%s = self::%s($%s))', $this->getErrorMessageVariableName($parameterName), $this->getValidationMethodName($parameterName), $parameterName);
29
        }
30 54
        return $test;
31
    }
32
33
    /**
34
     * @param string $parameterName
35
     * @param mixed $value
36
     * @param bool $itemType
37
     * @return string
38
     */
39 54
    final public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
40
    {
41 54
        if ($itemType || !$this->getAttribute()->isArray()) {
42 54
            $message = sprintf('sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen($%s))', $this->comparisonString(), $value, $parameterName);
43 27
        } else {
44 12
            $message = $this->getErrorMessageVariableName($parameterName);
45
        }
46 54
        return $message;
47
    }
48
49
    /**
50
     * @param string $parameterName
51
     * @param mixed $value
52
     */
53 12
    protected function addValidationMethod($parameterName, $value)
54
    {
55 12
        $method = new PhpMethod($this->getValidationMethodName($parameterName), [
56 12
            new PhpFunctionParameter('values', PhpFunctionParameter::NO_VALUE),
57 12
        ], PhpMethod::ACCESS_PUBLIC, false, true);
58 12
        $itemName = sprintf('%s%sItem', lcfirst($this->getFile()->getModel()->getCleanName(false)), ucfirst($this->getAttribute()->getCleanName()));
59
60
        $method
61 12
            ->addChild('$message = \'\';')
62 12
            ->addChild('$invalidValues = [];')
63 12
            ->addChild(sprintf('foreach ($values as $%s) {', $itemName))
64 12
            ->addChild($method->getIndentedString($this->validationRuleComment($value), 1))
65 12
            ->addChild($method->getIndentedString(sprintf('if (%s) {', $this->testConditions($itemName, $value, true)), 1))
66 12
            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s, true);', $itemName), 2))
67 12
            ->addChild($method->getIndentedString('}', 1))
68 12
            ->addChild('}')
69 12
            ->addChild('if (!empty($invalidValues)) {')
70 12
            ->addChild($method->getIndentedString(sprintf('$message = sprintf(\'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s\', implode(\', \', $invalidValues));', $this->comparisonString(), $value), 1))
71 12
            ->addChild('}')
72 12
            ->addChild('unset($invalidValues);')
73 12
            ->addChild('return $message;');
74 12
        $this->getMethods()->add($method);
75 12
    }
76
77
    /**
78
     * @param string $parameterName
79
     * @return string
80
     */
81 12
    protected function getValidationMethodName($parameterName)
82
    {
83 12
        return sprintf('validate%sFor%sConstraintFrom%s', ucfirst($parameterName), ucfirst($this->name()), ucFirst($this->getMethod()->getName()));
84
    }
85
86
    /**
87
     * @param string $parameterName
88
     * @return string
89
     */
90 12
    public function getErrorMessageVariableName($parameterName)
91
    {
92 12
        return sprintf('$%s%sErrorMessage', $parameterName, ucfirst($this->name()));
93
    }
94
}
95