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

DateRange   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getName() 0 4 1
A getStartName() 0 4 1
A getEndName() 0 4 1
A getDefaultValue() 0 4 1
A getValue() 0 12 3
A getDefaultValidationRules() 0 6 1
A save() 0 8 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