Passed
Push — feature/issue-168 ( a16e5d...f01c3b )
by Mikaël
06:47
created

AbstractBoundRule::testConditions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace WsdlToPhp\PackageGenerator\File\Validation;
5
6
/**
7
 * Class AbstractBoundRule
8
 * Gathers [min|max][In|Ex]Clusive rules
9
 * @package WsdlToPhp\PackageGenerator\File\Validation
10
 */
11
abstract class AbstractBoundRule extends AbstractMinMaxRule
12
{
13
14
    /**
15
     * @param string $parameterName
16
     * @param mixed $value
17
     * @param bool $itemType
18
     * @return string
19
     */
20
    final public function testConditions($parameterName, $value, $itemType = false)
21
    {
22
        if (is_numeric($value)) {
23
            $test = '$%1$s %3$s %2$d';
24
        } else {
25
            $test = '($time = (string) time()) && \DateTime::createFromFormat(\'U\', $time)->add(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', $%1$s))) %3$s \DateTime::createFromFormat(\'U\', $time)->add(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', \'%2$s\')))';
26
        }
27
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ') . $test, $parameterName, $value, static::symbol());
28
    }
29
30
    /**
31
     * @param string $parameterName
32
     * @param mixed $value
33
     * @param bool $itemType
34
     * @return string
35
     */
36
    final public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
37
    {
38
        return sprintf('sprintf(\'Invalid value %%s, the value must be %s %s %s\', var_export($%4$s, true))', is_numeric($value) ? 'numerically' : 'chronologically', static::comparisonString(), $value, $parameterName);
39
    }
40
}
41