Passed
Push — master ( 19faf5...3be4ae )
by Christopher
01:56
created

xsDuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fixValue() 0 6 1
A isOK() 0 4 1
A format() 0 13 3
1
<?php
2
3
namespace AlgoWeb\xsdTypes;
4
5
use AlgoWeb\xsdTypes\Facets\MinMaxTrait;
6
7
/**
8
 * The type xsd:duration represents a duration of time expressed as a number of years, months, days, hours, minutes,
9
 * and seconds. The format of xsd:duration is PnYnMnDTnHnMnS, where P is a literal value that starts the expression,
10
 * 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
11
 * number of days followed by a literal D, T is a literal value that separates the date and time, nH is the number of
12
 * hours followed by a literal H, nM is the number of minutes followed by a literal M, and nS is the number of seconds
13
 * followed by a literal S. The following rules apply to xsd:duration values:.
14
 *
15
 * - Any of these numbers and corresponding designators may be absent if they are equal to 0, but at least one number
16
 *          and designator must appear.
17
 *  - The numbers may be any unsigned integer, with the exception of the number of seconds, which may be an unsigned
18
 *          decimal number.
19
 *  - If a decimal point appears in the number of seconds, there must be at least one digit after the decimal point.
20
 *  - A minus sign may appear before the P to specify a negative duration.
21
 *  - If no time items (hour, minute, second) are present, the letter T must not appear.
22
 *
23
 * @package AlgoWeb\xsdTypes
24
 */
25
class xsDuration extends xsAnySimpleType
26
{
27
    use MinMaxTrait;
28
29
    /**
30
     * Construct.
31
     *
32
     * @param string $value
33
     */
34
    public function __construct($value)
35
    {
36
        parent::__construct($value);
37
        $this->setWhiteSpaceFacet('collapse');
38
    }
39
40
    public function fixValue()
41
    {
42
        parent::fixValue();
43
        $v = new \DateInterval($this->value);
44
        $this->value = $this->format($v);
45
    }
46
47
    protected function format(\DateInterval $tint, $pattern = 'PnYnMnDTnHnMnS')
48
    {
49
        $sReturn = '';
50
        for ($i = 0; $i < strlen($pattern); $i++) {
51
            if ($pattern[$i] == 'n') {
52
                $v = strtolower($pattern[$i + 1]);
53
                $sReturn .= $tint->$v;
54
                continue;
55
            }
56
            $sReturn .= $pattern[$i];
57
        }
58
        return $sReturn;
59
    }
60
61
    protected function isOK()
62
    {
63
        $this->CheckMinMax(new \DateInterval($this->value));
64
    }
65
}
66