1 | <?php |
||
12 | trait HasCast |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $cast; |
||
18 | |||
19 | /** |
||
20 | * @return string |
||
21 | */ |
||
22 | public function getCast() |
||
26 | |||
27 | /** |
||
28 | * @param string $cast |
||
29 | * @return $this |
||
30 | */ |
||
31 | public function setCast(string $cast) |
||
37 | |||
38 | /** |
||
39 | * @return bool |
||
40 | */ |
||
41 | protected function isCastable() |
||
45 | |||
46 | /** |
||
47 | * Determine whether a value is Date / DateTime castable for inbound manipulation. |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | protected function isDateCastable() |
||
55 | |||
56 | /** |
||
57 | * @return bool |
||
58 | */ |
||
59 | protected function isJsonCastable() |
||
63 | |||
64 | /** |
||
65 | * @return bool |
||
66 | */ |
||
67 | protected function isCommaCastable() |
||
71 | |||
72 | /** |
||
73 | * Cast the given value to JSON. |
||
74 | * |
||
75 | * @param mixed $value |
||
76 | * @return string |
||
77 | */ |
||
78 | protected function castValueAsJson($value) |
||
95 | |||
96 | /** |
||
97 | * Encode the given value as JSON. |
||
98 | * |
||
99 | * @param $value |
||
100 | * @return string |
||
101 | */ |
||
102 | protected function asJson($value) |
||
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) |
||
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) |
||
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) |
||
144 | |||
145 | /** |
||
146 | * Split the given value by comma |
||
147 | * |
||
148 | * @param string $value |
||
149 | * @return array |
||
150 | */ |
||
151 | protected function fromCommaSeparated(string $value) |
||
155 | |||
156 | /** |
||
157 | * Cast a value to a native PHP type. |
||
158 | * |
||
159 | * @param $value |
||
160 | * @return array |
||
161 | */ |
||
162 | protected function castValue($value) |
||
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.