1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akibatech\Crud\Traits; |
4
|
|
|
use Akibatech\Crud\Exceptions\InvalidModelException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FieldWithOptions |
8
|
|
|
* |
9
|
|
|
* @package Akibatech\Crud\Traits |
10
|
|
|
*/ |
11
|
|
|
trait FieldWithOptions |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $options; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var mixed |
20
|
|
|
*/ |
21
|
|
|
protected $default_option = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $options |
25
|
|
|
* @param mixed $default The default value. It's a key from given options. |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
|
|
public function withOptions($options = [], $default = null) |
29
|
|
|
{ |
30
|
|
|
$this->options = $options; |
31
|
|
|
|
32
|
|
|
$this->addRule('in:' . implode(',', $this->getOptionsKeys())); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if (!is_null($default)) |
35
|
|
|
{ |
36
|
|
|
if (!array_key_exists($default, $options)) |
37
|
|
|
{ |
38
|
|
|
throw new InvalidModelException("$default option does not exist in given options."); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->default_option = $default; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $identifier |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function hasOption($identifier) |
52
|
|
|
{ |
53
|
|
|
return array_key_exists($identifier, $this->getOptions()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param void |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getOptions() |
61
|
|
|
{ |
62
|
|
|
return $this->options; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param void |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
public function getOptionsKeys() |
70
|
|
|
{ |
71
|
|
|
return array_keys($this->options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param void |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function getOption($identifier) |
79
|
|
|
{ |
80
|
|
|
return $this->hasOption($identifier) ? $this->options[$identifier] : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param void |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function getDefaultOption() |
88
|
|
|
{ |
89
|
|
|
return $this->default_option; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param void |
94
|
|
|
* @return string|null |
95
|
|
|
*/ |
96
|
|
|
public function getTableValue() |
97
|
|
|
{ |
98
|
|
|
return $this->getOption($this->getValue()); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param void |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
|
|
public function beforeSave() |
106
|
|
|
{ |
107
|
|
|
if (is_null($this->getValue()) && !is_null($this->default_option)) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$this->newValue($this->default_option); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.