Passed
Pull Request — master (#22)
by Christopher
02:30
created

xsDuration::fixValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\xsdTypes;
4
5
/**
6
 * The type xsd:duration represents a duration of time expressed as a number of years, months, days, hours, minutes,
7
 * and seconds. The format of xsd:duration is PnYnMnDTnHnMnS, where P is a literal value that starts the expression,
8
 * nY is the number of years followed by a literal Y, nM is the number of months followed by a literal M, nD is the
9
 * number of days followed by a literal D, T is a literal value that separates the date and time, nH is the number of
10
 * hours followed by a literal H, nM is the number of minutes followed by a literal M, and nS is the number of seconds
11
 * followed by a literal S. The following rules apply to xsd:duration values:
12
 *
13
 * - Any of these numbers and corresponding designators may be absent if they are equal to 0, but at least one number and designator must appear.
14
 *  - The numbers may be any unsigned integer, with the exception of the number of seconds, which may be an unsigned decimal number.
15
 *  - If a decimal point appears in the number of seconds, there must be at least one digit after the decimal point.
16
 *  - A minus sign may appear before the P to specify a negative duration.
17
 *  - If no time items (hour, minute, second) are present, the letter T must not appear.
18
 * @package AlgoWeb\xsdTypes
19
 */
20
class xsDuration extends xsAnySimpleType
21
{
22
    use MinMaxTrait;
23
24
    /**
25
     * Construct
26
     *
27
     * @param string $value
28
     */
29
    public function __construct($value)
30
    {
31
        parent::__construct($value);
32
        $this->setWhiteSpaceFacet("collapse");
33
    }
34
35
    public function fixValue()
36
    {
37
        parent::fixValue();
38
        $v = new \DateInterval($this->value);
39
        $this->value = $this->format($v);
40
    }
41
42
    protected function format(\DateInterval $tint)
43
    {
44
        $sReturn = 'P';
45
46
        if ($this->y) {
47
            $sReturn .= $tint->y . 'Y';
48
        }
49
50
        if ($this->m) {
51
            $sReturn .= $tint->m . 'M';
52
        }
53
54
        if ($this->d) {
55
            $sReturn .= $tint->d . 'D';
56
        }
57
58 View Code Duplication
        if ($tint->h || $tint->i || $tint->s) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $sReturn .= 'T';
60
61
            if ($this->h) {
62
                $sReturn .= $tint->h . 'H';
63
            }
64
65
            if ($this->i) {
66
                $sReturn .= $tint->i . 'M';
67
            }
68
69
            if ($this->s) {
70
                $sReturn .= $tint->s . 'S';
71
            }
72
        }
73
74
        return $sReturn;
75
    }
76
77
    protected function isOK()
78
    {
79
        $this->CheckMinMax(new \DateInterval($this->value));
80
    }
81
}
82