1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Form\Elements\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait hasCast |
9
|
|
|
* |
10
|
|
|
* @package Sco\Admin\Form\Elements\Concerns |
11
|
|
|
*/ |
12
|
|
|
trait HasCast |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $cast; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function getCast() |
23
|
|
|
{ |
24
|
|
|
return $this->cast; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $cast |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setCast(string $cast) |
32
|
|
|
{ |
33
|
|
|
$this->cast = $cast; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
protected function isCastable() |
42
|
|
|
{ |
43
|
|
|
return ! is_null($this->getCast()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine whether a value is Date / DateTime castable for inbound manipulation. |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function isDateCastable() |
52
|
|
|
{ |
53
|
|
|
return in_array($this->getCast(), ['date', 'datetime']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
protected function isJsonCastable() |
60
|
|
|
{ |
61
|
|
|
return in_array($this->getCast(), ['array', 'json', 'object', 'collection']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
protected function isCommaCastable() |
68
|
|
|
{ |
69
|
|
|
return $this->getCast() === 'comma'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Cast the given value to JSON. |
74
|
|
|
* |
75
|
|
|
* @param mixed $value |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
protected function castValueAsJson($value) |
79
|
|
|
{ |
80
|
|
|
$value = $this->asJson($value); |
81
|
|
|
|
82
|
|
|
if ($value === false) { |
83
|
|
|
throw new \RuntimeException( |
84
|
|
|
sprintf( |
85
|
|
|
"Unable to encode value [%s] for element [%s] to JSON: %s.", |
86
|
|
|
$this->getName(), |
|
|
|
|
87
|
|
|
get_class($this), |
88
|
|
|
json_last_error_msg() |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Encode the given value as JSON. |
98
|
|
|
* |
99
|
|
|
* @param $value |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function asJson($value) |
103
|
|
|
{ |
104
|
|
|
return json_encode($value); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Decode the given JSON back into an array or object. |
109
|
|
|
* |
110
|
|
|
* @param string $value |
111
|
|
|
* @param bool $asObject |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
protected function fromJson($value, $asObject = false) |
115
|
|
|
{ |
116
|
|
|
return json_decode($value, ! $asObject); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Cast the given value to string with comma. |
121
|
|
|
* |
122
|
|
|
* @param mixed $value |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function castValueAsCommaSeparated($value) |
126
|
|
|
{ |
127
|
|
|
if (! is_array($value)) { |
128
|
|
|
$value = (array) $value; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->asCommaSeparated($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Join the given value with a comma |
136
|
|
|
* |
137
|
|
|
* @param array $value |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
protected function asCommaSeparated(array $value) |
141
|
|
|
{ |
142
|
|
|
return implode(',', $value); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Split the given value by comma |
147
|
|
|
* |
148
|
|
|
* @param string $value |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
protected function fromCommaSeparated(string $value) |
152
|
|
|
{ |
153
|
|
|
return explode(',', $value); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Cast a value to a native PHP type. |
158
|
|
|
* |
159
|
|
|
* @param $value |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
protected function castValue($value) |
163
|
|
|
{ |
164
|
|
|
if (is_null($value)) { |
165
|
|
|
return $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
switch ($this->getCast()) { |
169
|
|
|
case 'int': |
170
|
|
|
case 'integer': |
171
|
|
|
return (int) $value; |
172
|
|
|
case 'real': |
173
|
|
|
case 'float': |
174
|
|
|
case 'double': |
175
|
|
|
return (float) $value; |
176
|
|
|
case 'string': |
177
|
|
|
return (string) $value; |
178
|
|
|
case 'bool': |
179
|
|
|
case 'boolean': |
180
|
|
|
return (bool) $value; |
181
|
|
|
case 'object': |
182
|
|
|
return $this->fromJson($value, true); |
183
|
|
|
case 'array': |
184
|
|
|
case 'json': |
185
|
|
|
return $this->fromJson($value); |
186
|
|
|
case 'collection': |
187
|
|
|
return new Collection($this->fromJson($value)); |
188
|
|
|
case 'comma': |
189
|
|
|
return $this->fromCommaSeparated($value); |
190
|
|
|
case 'date': |
191
|
|
|
return $this->asDate($value); |
|
|
|
|
192
|
|
|
case 'datetime': |
193
|
|
|
return $this->asDateTime($value); |
|
|
|
|
194
|
|
|
case 'timestamp': |
195
|
|
|
return $this->asTimestamp($value); |
|
|
|
|
196
|
|
|
default: |
197
|
|
|
return $value; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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.