1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Field; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Terranet\Administrator\Exception; |
8
|
|
|
|
9
|
|
|
class Enum extends Field |
10
|
|
|
{ |
11
|
|
|
/** @var array */ |
12
|
|
|
protected $options = []; |
13
|
|
|
|
14
|
|
|
/** @var array */ |
15
|
|
|
protected $colors = ['#777777', '#2574ab', '#259dab', '#5bc0de', '#e6ad5c', '#d9534f']; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
protected $palette = []; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $useColors = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function disableColors() |
27
|
|
|
{ |
28
|
|
|
$this->useColors = false; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param iterable $options |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
|
|
public function setOptions(iterable $options): self |
38
|
|
|
{ |
39
|
|
|
if ($options instanceof Arrayable) { |
40
|
|
|
$options = $options->toArray(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$i = 0; |
44
|
|
|
foreach ($options as $key => $value) { |
45
|
|
|
if (!Arr::has($this->palette, $key)) { |
46
|
|
|
$this->palette[$key] = $this->colors[$i % \count($this->colors)]; |
47
|
|
|
++$i; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->options = $options; |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set colors palette. |
57
|
|
|
* |
58
|
|
|
* @param mixed $color |
59
|
|
|
* @param null|string $value |
60
|
|
|
* @return Enum |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
|
|
public function palette($color, string $value = null) |
64
|
|
|
{ |
65
|
|
|
if (\is_array($color)) { |
66
|
|
|
foreach ($color as $name => $code) { |
67
|
|
|
$this->palette($name, $code); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!array_key_exists($color, $this->options)) { |
74
|
|
|
throw new Exception("Unknown option {$color}"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->palette[$color] = $value; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function onEdit() |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
|
|
'options' => $this->options ?: [], |
89
|
|
|
'color' => $this->useColors && $this->value() ? \Illuminate\Support\Arr::get($this->palette, $this->value()) : null, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function onIndex() |
97
|
|
|
{ |
98
|
|
|
return $this->onEdit(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function onView() |
105
|
|
|
{ |
106
|
|
|
return $this->onEdit(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.