Completed
Push — master ( b05eaa...0a1951 )
by Paweł
16:26 queued 07:46
created

DateFilter::getDateTime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Grid\Filter;
13
14
use Sylius\Component\Grid\Data\DataSourceInterface;
15
use Sylius\Component\Grid\Filtering\FilterInterface;
16
17
/**
18
 * @author Grzegorz Sadowski <[email protected]>
19
 */
20
final class DateFilter implements FilterInterface
21
{
22
    const NAME = 'date';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function apply(DataSourceInterface $dataSource, $name, $data, array $options)
28
    {
29
        $expressionBuilder = $dataSource->getExpressionBuilder();
30
31
        $field = isset($options['field']) ? $options['field'] : $name;
32
33
        $from = $this->getDateTime($data['from']);
34
        if (null !== $from) {
35
            $expressionBuilder->greaterThanOrEqual($field, $from);
36
        }
37
38
        $to = $this->getDateTime($data['to']);
39
        if (null !== $to) {
40
            $expressionBuilder->lessThan($field, $to);
41
        }
42
    }
43
44
    /**
45
     * @param string[] $data
46
     *
47
     * @return null|string
48
     */
49
    private function getDateTime(array $data)
50
    {
51
        if (empty($data['date'])) {
52
            return null;
53
        }
54
55
        if (empty($data['time'])) {
56
            return $data['date'];
57
        }
58
59
        return $data['date'].' '.$data['time'];
60
    }
61
}
62