Completed
Push — master ( f05a87...6b2b65 )
by wen
14:04
created

DateRange::getDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
class DateRange extends Date
6
{
7
    protected $type = 'daterange';
8
9
    protected $defaultValue = [];
10
11
    protected $pickerFormat = 'yyyy-MM-dd';
12
13
    protected $startName;
14
15
    protected $endName;
16
17
    public function __construct($startName, $endName, $title)
18
    {
19
        $this->startName = $startName;
20
        $this->endName   = $endName;
21
22
        parent::__construct('', $title);
23
    }
24
25
    public function getName()
26
    {
27
        return $this->getStartName() . '_' . $this->getEndName();
28
    }
29
30
    public function getStartName()
31
    {
32
        return $this->startName;
33
    }
34
35
    public function getEndName()
36
    {
37
        return $this->endName;
38
    }
39
40
    public function getValue()
41
    {
42
        $model = $this->getModel();
43
        $value = $this->getDefaultValue();
44
        if (is_null($model) || !$model->exists) {
45
            return $value;
46
        }
47
48
        return [
49
            $this->dateToString($model->getAttribute($this->getStartName())),
50
            $this->dateToString($model->getAttribute($this->getEndName())),
51
        ];
52
    }
53
54
    protected function getDefaultValidationRules()
55
    {
56
        return [
57
            'array' => 'array',
58
        ];
59
    }
60
61
    public function save()
62
    {
63
        $model = $this->getModel();
64
65
        list($startValue, $endValue) = $this->getValueFromRequest();
66
        $model->setAttribute($this->getStartName(), $this->prepareValue($startValue));
67
        $model->setAttribute($this->getEndName(), $this->prepareValue($endValue));
68
    }
69
}
70