Date   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 82
rs 10
c 0
b 0
f 0
ccs 17
cts 18
cp 0.9444

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setDateFormatInput() 0 5 1
A getDateFormatInput() 0 4 1
A setDateFormatOutput() 0 5 1
A getDateFormatOutput() 0 4 1
A getFormControl() 0 8 1
A __getCondition() 0 12 4
1
<?php
2
3
/**
4
 * This file is part of the Grido (https://github.com/o5/grido)
5
 *
6
 * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE.md that was distributed with this source code.
10
 */
11
12
namespace Grido\Components\Filters;
13
14
/**
15
 * Date input filter.
16
 *
17
 * @package     Grido
18
 * @subpackage  Components\Filters
19
 * @author      Petr Bugyík
20
 *
21
 * @property string $dateFormatInput
22
 * @property string $dateFormatOutput
23
 */
24
class Date extends Text
25 1
{
26
    /** @var string */
27
    protected $formatValue;
28
29
    /** @var string */
30
    protected $dateFormatInput = 'd.m.Y';
31
32
    /** @var string */
33
    protected $dateFormatOutput = 'Y-m-d%';
34
35
    /**
36
     * Sets date-input format.
37
     * @param string $format
38
     * @return Date
39
     */
40
    public function setDateFormatInput($format)
41
    {
42 1
        $this->dateFormatInput = $format;
43 1
        return $this;
44
    }
45
46
    /**
47
     * Returns date-input format.
48
     * @return string
49
     */
50
    public function getDateFormatInput()
51
    {
52 1
        return $this->dateFormatInput;
53
    }
54
55
    /**
56
     * Sets date-output format.
57
     * @param string $format
58
     * @return Date
59
     */
60
    public function setDateFormatOutput($format)
61
    {
62 1
        $this->dateFormatOutput = $format;
63 1
        return $this;
64
    }
65
66
    /**
67
     * Returns date-output format.
68
     * @return string
69
     */
70
    public function getDateFormatOutput()
71
    {
72 1
        return $this->dateFormatOutput;
73
    }
74
75
    /**
76
     * @return \Nette\Forms\Controls\TextInput
77
     */
78
    protected function getFormControl()
79
    {
80 1
        $control = parent::getFormControl();
81 1
        $control->getControlPrototype()->class[] = 'date';
82 1
        $control->getControlPrototype()->attrs['autocomplete'] = 'off';
83
84 1
        return $control;
85
    }
86
87
    /**
88
     * @param string $value
89
     * @return Condition|bool
90
     * @throws \Exception
91
     * @internal
92
     */
93
    public function __getCondition($value)
94
    {
95 1
        $condition = $this->condition;
96 1
        if ($this->where === NULL && is_string($condition)) {
97 1
            $column = $this->getColumn();
98 1
            return ($date = \DateTime::createFromFormat($this->dateFormatInput, $value))
99 1
                ? Condition::setupFromArray([$column, $condition, $date->format($this->dateFormatOutput)])
100 1
                : Condition::setupEmpty();
101
        }
102
103
        return parent::__getCondition($value);
104
    }
105
}
106