Passed
Push — master ( cd8ed8...ef1fc1 )
by Andreas
10:21
created

jsdateTransformer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 85.29%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 60
ccs 29
cts 34
cp 0.8529
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B reverseTransform() 0 20 8
B transform() 0 29 9
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 $config;
19
20 43
    public function __construct(array $config)
21
    {
22 43
        $this->config = $config;
23
    }
24
25 43
    public function transform($input)
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
            || (   $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($array)
57
    {
58 7
        if (!is_array($array)) {
59
            throw new TransformationFailedException('Expected an array.');
60
        }
61
62 7
        if (   empty($array['date'])
63 6
            || (   $array['date'] instanceof DateTime
64 7
                && $array['date']->format('Y-m-d H:i:s') == '0001-01-01 00:00:00')) {
65 3
            return;
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
    }
78
}
79