Completed
Push — master ( 5ae406...f4f240 )
by Kamil
62:01 queued 40:55
created

DateTimeFilterTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A transform() 0 4 1
A reverseTransform() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Bundle\GridBundle\Form\DataTransformer;
6
7
use Symfony\Component\Form\DataTransformerInterface;
8
use Webmozart\Assert\Assert;
9
10
final class DateTimeFilterTransformer implements DataTransformerInterface
11
{
12
    private static $defaultTime = [
13
        'from' => ['hour' => '00', 'minute' => '00'],
14
        'to' => ['hour' => '23', 'minute' => '59'],
15
    ];
16
17
    /** @var string */
18
    private $type;
19
20
    public function __construct(string $type)
21
    {
22
        Assert::oneOf($type, array_keys(static::$defaultTime));
23
24
        $this->type = $type;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function transform($value): array
31
    {
32
        return $value;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function reverseTransform($value): array
39
    {
40
        if (!$value['date']['year']) {
41
            return $value;
42
        }
43
44
        $value['time']['hour'] = $value['time']['hour'] === '' ? static::$defaultTime[$this->type]['hour'] : $value['time']['hour'];
45
        $value['time']['minute'] = $value['time']['minute'] === '' ? static::$defaultTime[$this->type]['minute'] : $value['time']['minute'];
46
47
        return $value;
48
    }
49
}
50