1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Feeder\Modifier\Data\Transformer; |
4
|
|
|
|
5
|
|
|
use TreeHouse\Feeder\Exception\UnexpectedTypeException; |
6
|
|
|
|
7
|
|
|
abstract class AbstractDateTimeTransformer implements TransformerInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name of the input timezone. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $inputTimezone; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name of the output timezone. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $outputTimezone; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $inputTimezone The name of the input timezone |
25
|
|
|
* @param string $outputTimezone The name of the output timezone |
26
|
|
|
* |
27
|
|
|
* @throws UnexpectedTypeException If a timezone is not a string |
28
|
|
|
* @throws \InvalidArgumentException If a timezone is invalid |
29
|
|
|
*/ |
30
|
66 |
|
public function __construct($inputTimezone = null, $outputTimezone = null) |
31
|
|
|
{ |
32
|
|
|
$timeZones = [ |
33
|
66 |
|
'inputTimezone' => $inputTimezone, |
34
|
66 |
|
'outputTimezone' => $outputTimezone, |
35
|
66 |
|
]; |
36
|
|
|
|
37
|
66 |
|
foreach ($timeZones as $field => $timezone) { |
38
|
66 |
|
if (!is_string($timezone) && null !== $timezone) { |
39
|
2 |
|
throw new UnexpectedTypeException($timezone, 'string'); |
40
|
|
|
} |
41
|
|
|
|
42
|
64 |
|
$timezone = $timezone ?: date_default_timezone_get(); |
43
|
|
|
|
44
|
|
|
// Check if input and output timezones are valid |
45
|
|
|
try { |
46
|
64 |
|
new \DateTimeZone($timezone); |
47
|
64 |
|
} catch (\Exception $e) { |
48
|
4 |
|
throw new \InvalidArgumentException(sprintf('%s is invalid: %s.', $field, $timezone), $e->getCode(), $e); |
49
|
|
|
} |
50
|
|
|
|
51
|
62 |
|
$this->$field = $timezone; |
52
|
62 |
|
} |
53
|
60 |
|
} |
54
|
|
|
} |
55
|
|
|
|