Passed
Pull Request — master (#8)
by Matthew
13:52
created

ElementCountDown::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\CountDown\Elements;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\View\ArrayData;
7
8
/**
9
 * Class ElementCountDown
10
 * @package Dynamic\Elements\Elements
11
 *
12
 * @property string $End
13
 * @property boolean $ShowMonths
14
 * @property boolean $ShowSeconds
15
 * @property boolean $Elapse
16
 */
17
class ElementCountDown extends BaseElement
18
{
19
    /**
20
     * @var string
21
     */
22
    private static $icon = 'sectionnav-icon';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
23
24
    /**
25
     * @var string
26
     */
27
    private static $singular_name = 'Count Down Element';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
28
29
    /**
30
     * @var string
31
     */
32
    private static $plural_name = 'Count Down Elements';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'End' => 'DBDatetime',
39
        'ShowMonths' => 'Boolean',
40
        'ShowSeconds' => 'Boolean',
41
        'Elapse' => 'Boolean',
42
    ];
43
44
    /**
45
     * @var string
46
     */
47
    private static $table_name = 'ElementCountDown';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
48
49
    /**
50
     * @var ArrayData
51
     */
52 1
    private $client_config;
53
54 1
    /**
55
     * @return string
56
     */
57
    public function getType()
58
    {
59
        return _t(__CLASS__ . '.BlockType', 'Count Down');
60 2
    }
61
62
    /**
63 2
     * @return \SilverStripe\ORM\ValidationResult
64 2
     */
65
    public function validate()
66
    {
67 2
        $result = parent::validate();
68
69 2
        if (!$this->End) {
70
            $result->addError('An end date is required');
71
        }
72
73
        return $result;
74
    }
75 2
76
    /**
77 2
     * @return $this
78 2
     */
79
    public function setClientConfig()
80
    {
81 2
        $clientArray = [
82
            'End' => $this->End,
83
            'Elapse' => $this->Elapse,
84
        ];
85
86
        $this->client_config = ArrayData::create($this->encodeArrayValues($clientArray));
87
88 2
        return $this;
89
    }
90 2
91 2
    /**
92
     * @return ArrayData
93
     */
94 2
    public function getClientConfig()
95
    {
96
        if (!$this->client_config) {
97
            $this->setClientConfig();
98
        }
99
100
        return $this->client_config;
101
    }
102
103
    /**
104
     * @param $array
105
     * @return mixed
106
     */
107
    protected function encodeArrayValues($array)
108
    {
109
        foreach ($array as $key => $val) {
110
            $array[$key] = json_encode($val);
111
        }
112
113
        return $array;
114
    }
115
}
116