Passed
Push — master ( f0393b...d8ac0a )
by Mikaël
12:30 queued 13s
created

getArrayExceptionMessageOnTestFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
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
/**
8
 * @see https://www.w3.org/TR/xmlschema-2/#rf-pattern
9
 * Validation Rule: pattern valid
10
 * A literal in a ·lexical space· is facet-valid with respect to ·pattern· if:
11
 *  - 1 the literal is among the set of character sequences denoted by the ·regular expression· specified in {value}.
12
 */
13
final class PatternRule extends AbstractRule
14
{
15 26
    public const NAME = 'pattern';
16
17 26
    public function name(): string
18
    {
19
        return self::NAME;
20 26
    }
21
22 26
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
23
    {
24
        if ($itemType || !$this->getAttribute()->isArray()) {
25 26
            $valueToMatch = self::valueToRegularExpression($value);
26
            if (empty($valueToMatch)) {
27 26
                return '';
28
            }
29
30 26
            $test = sprintf(
31
                ($itemType ? '' : '!is_null($%1$s) && ').'!preg_match(\'/%2$s/\', (string) $%1$s)',
32 26
                $parameterName,
33 26
                self::valueToRegularExpression($value)
34 26
            );
35 26
        } else {
36 26
            $this->addArrayValidationMethod($parameterName, $value);
37
            $test = sprintf(
38
                '\'\' !== (%s = self::%s($%s))',
39
                $this->getArrayErrorMessageVariableName($parameterName),
40
                $this->getValidationMethodName($parameterName),
41
                $parameterName
42
            );
43
        }
44
45
        return $test;
46
    }
47
48
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
49
    {
50
        if ($itemType || !$this->getAttribute()->isArray()) {
51
            $message = sprintf(
52
                'sprintf(\'Invalid value %%s, please provide a literal that is among the set of character sequences denoted by the regular expression /%s/\', var_export($%s, true))',
53
                self::valueToRegularExpression($value),
54
                $parameterName
55
            );
56
        } else {
57
            $message = $this->getArrayErrorMessageVariableName($parameterName);
58
        }
59
60
        return $message;
61
    }
62
63
    public static function valueToRegularExpression($value): string
64
    {
65
        return implode(
66
            '|',
67
            array_map(
68
                static fn ($value) => addcslashes($value, '\'\\/'),
69
                array_map(
70
                    static fn ($value) => empty($value) ? '^$' : $value,
71
                    array_map(
72
                        'trim',
73
                        array_filter(
74
                            is_array($value) ? $value : [$value],
75
                            static fn ($value) => !in_array($value, ['true', 'false', true, false], true)
76
                        )
77
                    )
78
                )
79
            )
80
        );
81
    }
82
83
    protected function getArrayExceptionMessageOnTestFailure($value): string
84
    {
85
        return sprintf(
86
            'Invalid value(s) %%s, please provide literals that are among the set of character sequences denoted by the regular expression /%s/\'',
87
            stripcslashes(self::valueToRegularExpression($value)),
88
        );
89
    }
90
}
91