TimeStamp::getOffset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2016 [email protected]
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace PHPHealth\CDA\DataType\Quantity\DateAndTime;
28
29
use PHPHealth\CDA\DataType\Quantity\AbstractQuantity;
30
use PHPHealth\CDA\ClinicalDocument as CD;
31
32
/**
33
 * A quantity specifying a point on the axis of natural time. A point in time is
34
 *  most often represented as a calendar expression.
35
 *
36
 * Semantically, however, time is independent from calendars and best described
37
 * by its relationship to elapsed time (measured as a physical quantity in the
38
 * dimension of time). A TS plus an elapsed time yields another TS. Inversely,
39
 * a TS minus another TS yields an elapsed time.
40
 *
41
 * As nobody knows when time began, a TS is conceptualized as the amount of time
42
 * that has elapsed from some arbitrary zero-point, called an epoch. Because
43
 * there is no absolute zero-point on the time axis; natural time is a
44
 * difference-scale quantity, where only differences are defined but no ratios.
45
 * (For example, no TS is — absolutely speaking — "twice as late" as another TS.)
46
 *
47
 * Given some arbitrary zero-point, one can express any point in time as an
48
 * elapsed time measured from that offset. Such an arbitrary zero-point is
49
 * called an epoch. This epoch-offset form is used as a semantic representation
50
 * here, without implying that any system would have to implement TS in that
51
 * way. Systems that do not need to compute distances between TSs will not need
52
 * any other representation than a calendar expression literal.
53
 *
54
 *
55
 * **note about implementation**
56
 *
57
 * **Offset** : the offset will be extracted from the given \DateTime. Set offset
58
 * to true if offset is required. The offset will be inserted only if the precision
59
 * is set to seconds (14)
60
 *
61
 *
62
 * @author [email protected]
63
 */
64
class TimeStamp extends AbstractQuantity
65
{
66
    const DATE_FORMAT = "YmdHis";
67
    
68
    const PRECISION_DAY = 8;
69
    const PRECISION_SECONDS = 14;
70
    
71
    /**
72
     *
73
     * @var \DateTime
74
     */
75
    private $date;
76
    
77
    private $precision = 14;
78
    
79
    /**
80
     *
81
     * @var boolean
82
     */
83
    private $offset = false;
84
    
85
    public function __construct(\DateTime $datetime = null)
86
    {
87
        $this->date = $datetime === null ? new \DateTime() : $datetime;
88
    }
89
    
90
    public function getDate()
91
    {
92
        return $this->date;
93
    }
94
95
    public function setDate(\DateTime $date)
96
    {
97
        $this->date = $date;
98
        
99
        return $this;
100
    }
101
    
102
    public function getPrecision()
103
    {
104
        return $this->precision;
105
    }
106
107
    public function setPrecision($precision)
108
    {
109
        $this->precision = $precision;
110
        return $this;
111
    }
112
    
113
    public function getOffset()
114
    {
115
        return $this->offset;
116
    }
117
118
    public function setOffset($offset)
119
    {
120
        $this->offset = $offset;
121
        
122
        return $this;
123
    }
124
125
    
126
    public function setValueToElement(\DOMElement &$el, \DOMDocument $doc = null)
127
    {
128
        $value = \mb_substr(
129
            $this->getDate()->format(self::DATE_FORMAT),
130
            0,
131
            $this->getPrecision()
132
        );
133
        
134
        if ($this->getPrecision() >= self::PRECISION_SECONDS
135
            && $this->getOffset() !== false) {
136
            $value .= $this->getDate()->format("O");
137
        }
138
        
139
        $el->setAttributeNS(CD::NS_CDA, 'value', $value);
140
    }
141
}
142