| 1 | <?php |
||
| 7 | class DatetimeWithTimezoneTransformer implements DataTransformerInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Take a TimeDate object (with a timezone) and put its value on the time |
||
| 11 | * and timezone fields, so the user can see it |
||
| 12 | * |
||
| 13 | * @param \TimeDate $time |
||
| 14 | * @return array |
||
| 15 | */ |
||
| 16 | 1 | public function transform($time) |
|
| 17 | { |
||
| 18 | 1 | if ($time === null) { |
|
| 19 | return null; |
||
| 20 | } |
||
| 21 | |||
| 22 | return array( |
||
| 23 | 1 | 'time' => $this->createTimeDate($time), |
|
| 24 | 1 | 'timezone' => $time->timezone->getName() |
|
| 25 | 1 | ); |
|
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Take the timestamp from the time field and the timezone that the user |
||
| 30 | * provided and combine them into a single timezoned TimeDate |
||
| 31 | * |
||
| 32 | * @param array $data |
||
| 33 | * @return \TimeDate |
||
| 34 | */ |
||
| 35 | 1 | public function reverseTransform($data) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Create a TimeDate object from another one, ignoring its timezone |
||
| 42 | * |
||
| 43 | * @param \TimeDate $from The original timestamp |
||
| 44 | * @param string|null $timezone The timezone to add to the object (defaults |
||
| 45 | * to the PHP's default) |
||
| 46 | * @return \TimeDate |
||
| 47 | */ |
||
| 48 | 1 | private function createTimeDate($from, $timezone = null) |
|
| 67 | } |
||
| 68 |