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

xsDuration::isOK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
14
 *  - The numbers may be any unsigned integer, with the exception of the number of seconds, which may be an unsigned decimal number.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
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);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
39
        $this->value = $this->format($v);
40
    }
41
42
    protected function format(\DateInterval $tint, $pattern = 'PnYnMnDTnHnMnS')
43
    {
44
        $sReturn = '';
45
        for ($i = 0; $i < strlen($pattern); $i++) {
46
            if ($pattern[$i] == 'n') {
47
                $sReturn .= $tint->strtolower($pattern[$i + 1]);
48
                continue;
49
            }
50
            $sReturn .= $pattern[$i];
51
        }
52
        return $sReturn;
53
    }
54
55
    protected function isOK()
56
    {
57
        $this->CheckMinMax(new \DateInterval($this->value));
58
    }
59
}
60