Completed
Push — feature/issue-168 ( f01c3b...77e7a9 )
by Mikaël
23:44
created

TotalDigitsRule::name()   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 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
/**
6
 * Class TotalDigitsRule
7
 * @link https://www.w3.org/TR/xmlschema-2/#rf-totalDigits
8
 * Validation Rule: totalDigits Valid
9
 * A value in a ·value space· is facet-valid with respect to ·totalDigits· if:
10
 *  - 1 that value is expressible as i × 10^-n where i and n are integers such that |i| < 10^{value} and 0 <= n <= {value}.
11
 */
12
class TotalDigitsRule extends AbstractRule
13
{
14
15
    /**
16
     * @return string
17
     */
18 48
    public function name()
19
    {
20 48
        return 'totalDigits';
21
    }
22
23
    /**
24
     * @param string $parameterName
25
     * @param mixed $value
26
     * @param bool $itemType
27
     * @return string
28
     */
29 48
    public function testConditions($parameterName, $value, $itemType = false)
30
    {
31 48
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ') . 'strlen(preg_replace(\'/(\D)/\', \'\', $%1$s)) > %2$d', $parameterName, $value);
32
    }
33
34
    /**
35
     * @param string $parameterName
36
     * @param mixed $value
37
     * @param bool $itemType
38
     * @return string
39
     */
40 48
    public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
41
    {
42 48
        return sprintf('sprintf(\'Invalid value %%s, the value must use at most %1$d digits, "%%d" given\', var_export($%2$s, true), strlen(preg_replace(\'/(\D)/\', \'\', $%2$s)))', $value, $parameterName);
43
    }
44
}
45