FractionDigitsRule::name()   A
last analyzed

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
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7
/**
8
 * @see https://www.w3.org/TR/xmlschema-2/#rf-fractionDigits
9
 * Validation Rule: fractionDigits Valid
10
 * A value in a ·value space· is facet-valid with respect to ·fractionDigits· if:
11
 * - 1 that value is expressible as i × 10^-n where i and n are integers and 0 <= n <= {value}.
12
 */
13
final class FractionDigitsRule extends AbstractRule
14
{
15 14
    public function name(): string
16
    {
17 14
        return 'fractionDigits';
18
    }
19
20 14
    public function testConditions(string $parameterName, $value, bool $itemType = false): string
21
    {
22 14
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ').'mb_strlen(mb_substr((string) $%1$s, false !== mb_strpos((string) $%1$s, \'.\') ? mb_strpos((string) $%1$s, \'.\') + 1 : mb_strlen((string) $%1$s))) > %2$d', $parameterName, $value);
23
    }
24
25 14
    public function exceptionMessageOnTestFailure(string $parameterName, $value, bool $itemType = false): string
26
    {
27 14
        return sprintf('sprintf(\'Invalid value %%s, the value must at most contain %1$d fraction digits, %%d given\', var_export($%2$s, true), mb_strlen(mb_substr((string) $%2$s, mb_strpos((string) $%2$s, \'.\') + 1)))', $value, $parameterName);
28
    }
29
}
30