TimeStamp   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 33
dl 0
loc 117
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fromString() 0 7 1
A setPrecision() 0 4 1
A setDate() 0 5 1
A setOffset() 0 4 1
A getOffset() 0 3 1
A getPrecision() 0 3 1
A setValueToElement() 0 17 4
A getDate() 0 3 1
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 NON INFRINGEMENT. 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 i3Soft\CDA\DataType\Quantity\DateAndTime;
28
29
use i3Soft\CDA\ClinicalDocument as CD;
30
use i3Soft\CDA\DataType\Quantity\AbstractQuantity;
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
  const PRECISION_DAY     = 8;
68
  const PRECISION_HOURS   = 10;
69
  const PRECISION_MINUTES = 12;
70
  const PRECISION_SECONDS = 14;
71
72
  /** @var \DateTime */
73
  private $date;
74
75
  /** @var int */
76
  private $precision;
77
78
  /** @var bool */
79
  private $offset = FALSE;
80
81
  /**
82
   * TimeStamp constructor.
83
   *
84
   * @param \DateTime|NULL $datetime
85
   */
86
  public function __construct (\DateTime $datetime = NULL)
87
  {
88
    $this->date      = $datetime ?? new \DateTime();
89
    $this->precision = self::PRECISION_SECONDS;
90
  }
91
92
  public static function fromString (string $in, $tz = 'UTC', int $precision = self::PRECISION_MINUTES): TimeStamp
93
  {
94
    $dt         = new \DateTime($in, new \DateTimeZone($tz));
95
    $time_stamp = new self($dt);
96
    $time_stamp->setOffset($tz[0] === '+')
97
      ->setPrecision($precision);
98
    return $time_stamp;
99
  }
100
101
  /**
102
   * @param \DOMElement       $el
103
   * @param \DOMDocument|NULL $doc
104
   */
105
  public function setValueToElement (\DOMElement $el, \DOMDocument $doc)
106
  {
107
    if ($this->getDate())
108
    {
109
      $value = \mb_substr(
110
        $this->getDate()->format(self::DATE_FORMAT),
111
        0,
112
        $this->getPrecision()
113
      );
114
115
      if ($this->getPrecision() >= self::PRECISION_MINUTES
116
          && $this->getOffset() !== FALSE)
117
      {
118
        $value .= $this->getDate()->format('O');
119
      }
120
121
      $el->setAttributeNS(CD::getNS(), 'value', $value);
122
    }
123
  }
124
125
  /**
126
   * @return \DateTime|NULL
127
   */
128
  public function getDate ()
129
  {
130
    return $this->date;
131
  }
132
133
  /**
134
   * @param \DateTime $date
135
   *
136
   * @return TimeStamp
137
   */
138
  public function setDate (\DateTime $date): self
139
  {
140
    $this->date = $date;
141
142
    return $this;
143
  }
144
145
  /**
146
   * @return int
147
   */
148
  public function getPrecision (): int
149
  {
150
    return $this->precision;
151
  }
152
153
  /**
154
   * @param int $precision
155
   *
156
   * @return TimeStamp
157
   */
158
  public function setPrecision (int $precision): self
159
  {
160
    $this->precision = $precision;
161
    return $this;
162
  }
163
164
  /**
165
   * @return bool
166
   */
167
  public function getOffset (): bool
168
  {
169
    return $this->offset;
170
  }
171
172
  /**
173
   * @param bool $offset
174
   *
175
   * @return TimeStamp
176
   */
177
  public function setOffset (bool $offset): self
178
  {
179
    $this->offset = $offset;
180
    return $this;
181
  }
182
}
183