Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractLengthRule
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 testConditions
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 exceptionMessageOnTestFailure
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 getArrayExceptionMessageOnTestFailure
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7/**
8 * Gathers [min|max|]Length rules.
9 */
10abstract class AbstractLengthRule extends AbstractMinMaxRule
11{
12    final public function testConditions(string $parameterName, $value, bool $itemType = false): string
13    {
14        if ($itemType || !$this->getAttribute()->isArray()) {
15            $test = sprintf(
16                ($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen((string) $%1$s) %3$s %2$d',
17                $parameterName,
18                $value,
19                $this->symbol()
20            );
21        } else {
22            $this->addArrayValidationMethod($parameterName, $value);
23            $test = sprintf(
24                '\'\' !== (%s = self::%s($%s))',
25                $this->getArrayErrorMessageVariableName($parameterName),
26                $this->getValidationMethodName($parameterName),
27                $parameterName
28            );
29        }
30
31        return $test;
32    }
33
34    final public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
35    {
36        if ($itemType || !$this->getAttribute()->isArray()) {
37            $message = sprintf(
38                'sprintf(\'Invalid length of %%s, the number of characters/octets contained by the literal must be %s %s\', mb_strlen((string) $%s))',
39                $this->comparisonString(),
40                is_array($value) ? implode(',', array_unique($value)) : $value,
41                $parameterName
42            );
43        } else {
44            $message = $this->getArrayErrorMessageVariableName($parameterName);
45        }
46
47        return $message;
48    }
49
50    final protected function getArrayExceptionMessageOnTestFailure($value): string
51    {
52        return sprintf(
53            'Invalid length for value(s) %%s, the number of characters/octets contained by the literal must be %s %s',
54            $this->comparisonString(),
55            $value
56        );
57    }
58}