Completed
Push — master ( 64ceeb...01d429 )
by Terzi
04:48
created

Enum::onEdit()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Terranet\Administrator\Exception;
6
7
class Enum extends Generic
8
{
9
    protected $options = [];
10
11
    protected $colors = ['#777777', '#2574ab', '#259dab', '#5bc0de', '#e6ad5c', '#d9534f'];
12
13
    protected $palette = [];
14
15
    /**
16
     * @param array $options
17
     * @return self
18
     */
19
    public function setOptions(array $options): self
20
    {
21
        $i = 0;
22
        foreach ($options as $key => $value) {
23
            if (!array_has($this->palette, $key)) {
24
                $this->palette[$key] = $this->colors[$i % count($this->colors)];
25
                $i++;
26
            }
27
        }
28
        $this->options = $options;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Set colors palette.
35
     *
36
     * @param string|array $colors
37
     * @param string|null $value
38
     */
39
    public function palette($color, string $value = null)
40
    {
41
        if (is_array($color)) {
42
            foreach ($color as $name => $code) {
43
                $this->palette($name, $code);
44
            }
45
46
            return $this;
47
        }
48
49
        if (!array_key_exists($color, $this->options)) {
50
            throw new Exception("Unknown option {$color}");
51
        }
52
53
        $this->palette[$color] = $value;
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    protected function onEdit()
62
    {
63
        return [
64
            'options' => $this->options ?: [],
65
            'palette' => $this->palette,
66
        ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    protected function onIndex()
73
    {
74
        return $this->onEdit();
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    protected function onView()
81
    {
82
        return $this->onEdit();
83
    }
84
}