TimeOffset::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Popy\Calendar\ValueObject;
4
5
use DateTimeZone;
6
use DateTimeInterface;
7
8
/**
9
 * Represents the time offset induced by timezones and day saving time.
10
 * It is an immutable object.
11
 *
12
 * A TimeOffset MAY NOT be aware of its properties (value, dst status,
13
 * abbreviation, usually when it's been built by a parser).
14
 */
15
class TimeOffset
16
{
17
    /**
18
     * Offset value.
19
     *
20
     * @var integer|null
21
     */
22
    protected $value;
23
24
    /**
25
     * Day saving time status.
26
     *
27
     * @var boolean|null
28
     */
29
    protected $dst;
30
31
    /**
32
     * Offset abbreviation.
33
     *
34
     * @var string|null
35
     */
36
    protected $abbreviation;
37
38
    /**
39
     * Class constructor.
40
     *
41
     * @param integer|null $value        Offset value.
42
     * @param boolean|null $dst          Day saving time status.
43
     * @param string|null  $abbreviation Offset abbreviation.
44
     */
45
    public function __construct($value = null, $dst = null, $abbreviation = null)
46
    {
47
        $this->value        = $value;
48
        $this->dst          = $dst;
49
        $this->abbreviation = $abbreviation;
50
    }
51
52
    /**
53
     * Instanciates a TimeOffset from a DateTimeInterface, using the format
54
     * method to extract properties.
55
     *
56
     * @param DateTimeInterface $date
57
     *
58
     * @return static
59
     */
60
    public static function buildFromDateTimeInterface(DateTimeInterface $date)
61
    {
62
        $parts = explode('|', $date->format('Z|I|T'));
63
64
        return new static((int)$parts[0], (bool)$parts[1], $parts[2]);
65
    }
66
67
    /**
68
     * Gets the offset value.
69
     *
70
     * @return integer|null
71
     */
72
    public function getValue()
73
    {
74
        return $this->value;
75
    }
76
77
    /**
78
     * Checks day saving time status.
79
     *
80
     * @return boolean|null
81
     */
82
    public function isDst()
83
    {
84
        return $this->dst;
85
    }
86
87
    /**
88
     * Gets the Offset abbreviation..
89
     *
90
     * @return string|null
91
     */
92
    public function getAbbreviation()
93
    {
94
        return $this->abbreviation;
95
    }
96
97
    
98
    /**
99
     * Gets a new TimeOffset instance with the input value.
100
     *
101
     * @param integer|null $value
102
     *
103
     * @return static
104
     */
105
    public function withValue($value)
106
    {
107
        $res = clone $this;
108
        $res->value = $value;
109
110
        return $res;
111
    }
112
    
113
    /**
114
     * Gets a new TimeOffset instance with the input dst.
115
     *
116
     * @param boolean|null $dst
117
     *
118
     * @return static
119
     */
120
    public function withDst($dst)
121
    {
122
        $res = clone $this;
123
        $res->dst = $dst;
124
125
        return $res;
126
    }
127
    
128
    /**
129
     * Gets a new TimeOffset instance with the input abbreviation.
130
     *
131
     * @param string|null $abbreviation
132
     *
133
     * @return static
134
     */
135
    public function withAbbreviation($abbreviation)
136
    {
137
        $res = clone $this;
138
        $res->abbreviation = $abbreviation;
139
140
        return $res;
141
    }
142
143
    /**
144
     * Build a DateTimeZone object based on TimeOffset properties, if possible.
145
     *
146
     * @return DateTimeZone|null
147
     */
148
    public function buildTimeZone()
149
    {
150
        if (null !== $this->value) {
151
            $sign = $this->value < 0 ? '-' : '+';
152
            $value = intval(abs($this->value) / 60);
153
154
            return new DateTimeZone(sprintf(
155
                '%s%02d:%02d',
156
                $sign,
157
                intval($value / 60),
158
                $value % 60
159
            ));
160
        }
161
    }
162
}
163