Completed
Push — master ( fe0acb...db8d28 )
by wen
11:03
created

DateRange   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
c 1
b 0
f 0
wmc 8
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStartName() 0 4 1
A getEndName() 0 4 1
A getValue() 0 13 3
A getDefaultValidationRules() 0 6 1
A save() 0 8 1
A __construct() 0 7 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
/**
6
 * Form Element DateRange
7
 *
8
 * @see http://element.eleme.io/#/zh-CN/component/date-picker
9
 */
10
class DateRange extends Date
11
{
12
    protected $type = 'daterange';
13
14
    protected $defaultValue = [];
15
16
    protected $pickerFormat = 'yyyy-MM-dd';
17
18
    protected $startName;
19
20
    protected $endName;
21
22
    public function __construct($startName, $endName, $title)
23
    {
24
        parent::__construct($startName . '_' . $endName, $title);
25
26
        $this->startName = $startName;
27
        $this->endName   = $endName;
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