Passed
Push — master ( 75c14e...2520d9 )
by Matthew
02:43
created

ElementCountDown::encodeArrayValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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
    private $client_config;
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getType()
58
    {
59 1
        return _t(__CLASS__ . '.BlockType', 'Count Down');
60
    }
61
62
    /**
63
     * @return \SilverStripe\ORM\ValidationResult
64
     */
65 1
    public function validate()
66
    {
67 1
        $result = parent::validate();
68
69 1
        if (!$this->End) {
70 1
            $result->addError('An end date is required');
71
        }
72
73 1
        return $result;
74
    }
75
76
    /**
77
     * @return $this
78
     */
79 2
    public function setClientConfig()
80
    {
81
        $clientArray = [
82 2
            'End' => $this->End,
83 2
            'Elapse' => $this->Elapse,
84
        ];
85
86 2
        $this->client_config = ArrayData::create($this->encodeArrayValues($clientArray));
87
88 2
        return $this;
89
    }
90
91
    /**
92
     * @return ArrayData
93
     */
94 2
    public function getClientConfig()
95
    {
96 2
        if (!$this->client_config) {
97 2
            $this->setClientConfig();
98
        }
99
100 2
        return $this->client_config;
101
    }
102
103
    /**
104
     * @param $array
105
     * @return mixed
106
     */
107 2
    protected function encodeArrayValues($array)
108
    {
109 2
        foreach ($array as $key => $val) {
110 2
            $array[$key] = json_encode($val);
111
        }
112
113 2
        return $array;
114
    }
115
}
116