Completed
Push — master ( 534ed1...5a2681 )
by wen
15:08
created

DateRange::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Http\Request;
6
7
class DateRange extends Date
8
{
9
    protected $type = 'daterange';
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 getDefaultValue()
41
    {
42
        return [];
43
    }
44
45
    public function getValue()
46
    {
47
        $model = $this->getModel();
48
        $value = $this->getDefaultValue();
49
        if (is_null($model) || !$model->exists) {
50
            return $value;
51
        }
52
        return [
53
            $model->getAttribute($this->getStartName()),
54
            $model->getAttribute($this->getEndName()),
55
        ];
56
    }
57
58
    protected function getDefaultValidationRules()
59
    {
60
        return [
61
            'array' => 'array',
62
        ];
63
    }
64
65
    public function save(Request $request)
66
    {
67
        $model = $this->getModel();
68
69
        list($startValue, $endValue) = $this->getValueFromRequest($request);
70
        $model->setAttribute($this->getStartName(), $this->prepareValue($startValue));
71
        $model->setAttribute($this->getEndName(), $this->prepareValue($endValue));
72
    }
73
}
74