Test Failed
Pull Request — master (#28)
by Christopher
04:21
created

XMLDateInterval::handleT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace AlgoWeb\xsdTypes\AxillaryClasses;
4
5
class XMLDateInterval extends \DateInterval
6
{
7
    /**
8
     * formating string like ISO 8601 (PnYnMnDTnHnMnS).
9
     */
10
    const INTERVAL_ISO8601 = 'P%yY%mM%dDT%hH%iM%sS';
11
    private $pattern;
12
    private $patternLen;
13
14
    public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS')
15
    {
16
        parent::__construct(trim($intervalSpec, '-'));
17
        if ($intervalSpec[0] == '-') {
18
            $this->invert = 1;
19
        }
20
        $this->pattern = trim($pattern);
21
        $this->patternLen = strlen($this->pattern);
22
    }
23
24
    /**
25
     * formating the interval like ISO 8601 (PnYnMnDTnHnMnS).
26
     *
27
     * @return string|null
28
     */
29
    public function __toString()
30
    {
31
        $sReturn = $this->handleSign();
32
        $tSeen = false;
33
        for ($i = 0; $i < $this->patternLen; $i++) {
34
            $sReturn .= $this->handleChar($i, $tSeen);
35
        }
36
        return $sReturn;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    private function handleSign()
43
    {
44
        if ($this->invert === 1) {
45
            return '-';
46
        }
47
        return "";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
48
    }
49
50
    /**
51
     * @param int  $i
52
     * @param bool $tSeen
53
     *
54
     * @return string
55
     */
56
    private function handleChar($i, &$tSeen)
57
    {
58
        switch ($this->pattern[$i]) {
59
            case 'n':
60
                return $this->handleN($i, $tSeen);
61
            case 'T':
62
                return $this->handleT($tSeen);
63
            default:
64
                return $this->handleOther($i);
65
        }
66
    }
67
68
    /**
69
     * @param int  $i
70
     * @param bool $tSeen
71
     *
72
     * @return string
73
     */
74
    private function handleN($i, $tSeen)
75
    {
76
        $v = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]);
77
        return $this->$v;
78
    }
79
80
    /**
81
     * @param bool $tSeen
82
     *
83
     * @return string
84
     */
85
    private function handleT(&$tSeen)
86
    {
87
        $tSeen = true;
88
        return 'T';
89
    }
90
91
    /**
92
     * @param int $i
93
     *
94
     * @return string
95
     */
96
    private function handleOther($i)
97
    {
98
        return $this->pattern[$i];
99
    }
100
}
101