-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeZoneStringStrategy.php
57 lines (52 loc) · 1.62 KB
/
TimeZoneStringStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace HtTimeZone\Stdlib\Hydrator\Strategy;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
use DateTime;
use DateTimeZone;
/**
* Converts string to instance of DateTimeZone for injecting to object And Converts instance of DateTimeZone to string
*/
class TimeZoneStringStrategy implements StrategyInterface
{
/**
*
* @param string|DateTimeZone $value
* @return DateTimeZone
*/
public function hydrate($value)
{
if (is_string($value)) {
$value = new DateTimeZone($value);
} elseif (!$value instanceof DateTimeZone) {
throw new Exception\InvalidArgumentException(
sprintf(
'%s expects parameter 1 to be string or instance of DateTimeZone, %s provided instead',
__METHOD__,
is_object($value) ? get_class($value) : gettype($value)
)
);
}
return $value;
}
/**
* Converts instance of DateTimeZone to string
*
* @param string|DateTimeZone $value
* @return string
*/
public function extract($value)
{
if ($value instanceof DateTimeZone) {
$value = $value->getName();
} elseif (!is_string($value)) {
throw new Exception\InvalidArgumentException(
sprintf(
'%s expects parameter 1 to be string or instance of DateTimeZone, %s provided instead',
__METHOD__,
is_object($value) ? get_class($value) : gettype($value)
)
);
}
return $value;
}
}