Completed
Push — master ( 9b8f8b...ad8621 )
by Mikaël
29:42
created

AbstractBoundRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 45
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A exceptionMessageOnTestFailure() 0 3 2
B testConditions() 0 25 6
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
/**
6
 * Class AbstractBoundRule
7
 * Gathers [min|max][In|Ex]clusive rules
8
 * @package WsdlToPhp\PackageGenerator\File\Validation
9
 */
10
abstract class AbstractBoundRule extends AbstractMinMaxRule
11
{
12
13
    /**
14
     * @param string $parameterName
15
     * @param mixed $value
16
     * @param bool $itemType
17
     * @return string
18
     */
19 132
    final public function testConditions($parameterName, $value, $itemType = false)
20
    {
21 132
        $method = '';
22 132
        $checkValueDomain = '';
23 132
        if (is_numeric($value)) {
24 60
            $test = '$%1$s %3$s %2$s';
25 30
        } else {
26 72
            if (false === mb_strpos($value, '-')) {
27 36
                $method = 'add';
28 18
            } else {
29 36
                $method = 'sub';
30 36
                $value = mb_substr($value, 1);
31
            }
32 72
            switch ($this->symbol()) {
33 72
                case self::SYMBOL_MAX_EXCLUSIVE:
34 63
                case self::SYMBOL_MAX_INCLUSIVE:
35 36
                    $checkValueDomain = '===';
36 36
                    break;
37 18
                default:
38 36
                    $checkValueDomain = '!==';
39 36
                    break;
40 36
            }
41 72
            $test = 'false %5$s mb_strpos($%1$s, \'-\') && ($time = (string) time()) && \DateTime::createFromFormat(\'U\', $time)->%4$s(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', str_replace(\'-\', \'\', $%1$s)))) %3$s \DateTime::createFromFormat(\'U\', $time)->%4$s(new \DateInterval(preg_replace(\'/(.*)(\.[0-9]*S)/\', \'$1S\', \'%2$s\')))';
42
        }
43 132
        return sprintf(($itemType ? '' : '!is_null($%1$s) && ') . $test, $parameterName, $value, $this->symbol(), $method, $checkValueDomain);
44
    }
45
46
    /**
47
     * @param string $parameterName
48
     * @param mixed $value
49
     * @param bool $itemType
50
     * @return string
51
     */
52 132
    final public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false)
53
    {
54 132
        return sprintf('sprintf(\'Invalid value %%s, the value must be %s %s %s\', var_export($%4$s, true))', is_numeric($value) ? 'numerically' : 'chronologically', $this->comparisonString(), $value, $parameterName);
55
    }
56
}
57