Completed
Pull Request — master (#28)
by Christopher
12:01 queued 01:14
created

XMLDateInterval::handleN()   A

Complexity

Conditions 3
Paths 4

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 3
eloc 3
nc 4
nop 2
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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    private function handleSign()
43
    {
44
        if ($this->invert === 1) {
45
            return '-';
46
        }
47
    }
48
49
    /**
50
     * @param int  $i
51
     * @param bool $tSeen
52
     *
53
     * @return string
54
     */
55
    private function handleChar($i, &$tSeen)
56
    {
57
        switch ($this->pattern[$i]) {
58
            case 'n':
59
                return $this->handleN($i, $tSeen);
60
            case 'T':
61
                return $this->handleT($tSeen);
62
            default:
63
                return $this->HandleOther($i);
64
        }
65
    }
66
67
    /**
68
     * @param int  $i
69
     * @param bool $tSeen
70
     *
71
     * @return string
72
     */
73
    private function handleN($i, $tSeen)
74
    {
75
        $v = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]);
76
        return $this->$v;
77
    }
78
79
    /**
80
     * @param bool $tSeen
81
     *
82
     * @return string
83
     */
84
    private function handleT(&$tSeen)
85
    {
86
        $tSeen = true;
87
        return 'T';
88
    }
89
90
    /**
91
     * @param int $i
92
     *
93
     * @return string
94
     */
95
    private function HandleOther($i)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
96
    {
97
        return $this->pattern[$i];
98
    }
99
}
100