|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace midcom\datamanager\extension\transformer; |
|
7
|
|
|
|
|
8
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
9
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
10
|
|
|
use midcom\datamanager\extension\type\jsdateType; |
|
11
|
|
|
use DateTime; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Experimental jsdate transformer |
|
15
|
|
|
*/ |
|
16
|
|
|
class jsdateTransformer implements DataTransformerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private array $config; |
|
19
|
|
|
|
|
20
|
43 |
|
public function __construct(array $config) |
|
21
|
|
|
{ |
|
22
|
43 |
|
$this->config = $config; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
43 |
|
public function transform(mixed $input) : mixed |
|
26
|
|
|
{ |
|
27
|
43 |
|
$result = ['date' => null]; |
|
28
|
43 |
|
if ($this->config['widget_config']['show_time']) { |
|
29
|
16 |
|
$result['hours'] = null; |
|
30
|
16 |
|
$result['minutes'] = null; |
|
31
|
16 |
|
if (!$this->config['widget_config']['hide_seconds']) { |
|
32
|
6 |
|
$result['seconds'] = null; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
43 |
|
if ( empty($input) |
|
37
|
43 |
|
|| ( $input instanceof DateTime |
|
38
|
43 |
|
&& $input->format('Y-m-d H:i:s') == '0001-01-01 00:00:00')) { |
|
39
|
43 |
|
return $result; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
20 |
|
$date = new DateTime; |
|
43
|
20 |
|
if ($this->config['type_config']['storage_type'] === jsdateType::UNIXTIME) { |
|
44
|
20 |
|
$date->setTimestamp((int) $input); |
|
45
|
|
|
} elseif ($this->config['type_config']['storage_type'] === jsdateType::ISO) { |
|
46
|
|
|
$date->modify($input); |
|
47
|
|
|
} |
|
48
|
20 |
|
$result['date'] = $date; |
|
49
|
20 |
|
if ($this->config['widget_config']['show_time']) { |
|
50
|
8 |
|
$result['time'] = $date; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
20 |
|
return $result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
7 |
|
public function reverseTransform(mixed $array) : mixed |
|
57
|
|
|
{ |
|
58
|
7 |
|
if (!is_array($array)) { |
|
59
|
|
|
throw new TransformationFailedException('Expected an array.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
7 |
|
if ( empty($array['date']) |
|
63
|
7 |
|
|| ( $array['date'] instanceof DateTime |
|
64
|
7 |
|
&& $array['date']->format('Y-m-d H:i:s') == '0001-01-01 00:00:00')) { |
|
65
|
3 |
|
return null; |
|
66
|
|
|
} |
|
67
|
6 |
|
if (!empty($array['time'])) { |
|
68
|
2 |
|
$array['date']->setTime((int) $array['time']->format('G'), (int) $array['time']->format('i'), (int) $array['time']->format('s')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
if ($this->config['type_config']['storage_type'] === jsdateType::UNIXTIME) { |
|
72
|
6 |
|
return $array['date']->format('U'); |
|
73
|
|
|
} |
|
74
|
|
|
if ($this->config['type_config']['storage_type'] === jsdateType::ISO) { |
|
75
|
|
|
return $array['date']->format('Y-m-d H:i:s'); |
|
76
|
|
|
} |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|