1 | <?php |
||
10 | class Caster |
||
11 | { |
||
12 | /** |
||
13 | * Cast value to a string. |
||
14 | * |
||
15 | * @param mixed $str |
||
16 | * @return string |
||
17 | */ |
||
18 | 2 | public static function string($str): string |
|
22 | |||
23 | /** |
||
24 | * Cast value to a string. |
||
25 | * |
||
26 | * @param mixed $str |
||
27 | * @param mixed $default |
||
28 | * @return \Illuminate\Support\Carbon|null |
||
29 | */ |
||
30 | public static function datetime($str, $default = null): ?Carbon |
||
44 | |||
45 | /** |
||
46 | * Transform data to its JSON representation. |
||
47 | * |
||
48 | * @param mixed $data |
||
49 | * @param mixed $default |
||
50 | * @return string |
||
51 | */ |
||
52 | 2 | public static function toJson($data, $default = null): string |
|
56 | |||
57 | /** |
||
58 | * Transform a JSON string to its associative array representation. |
||
59 | * |
||
60 | * @param mixed $json |
||
61 | * @return array|null |
||
62 | */ |
||
63 | 6 | public static function fromJson($json): ?array |
|
77 | |||
78 | /** |
||
79 | * Transform markdown content to an HTML string. |
||
80 | * |
||
81 | * @param mixed $md |
||
82 | * @param mixed $default |
||
83 | * @return string |
||
84 | */ |
||
85 | 4 | public static function markdown($md, $default = null): ?string |
|
93 | |||
94 | /** |
||
95 | * Cast an integer to an integer value otherwise to null. |
||
96 | * |
||
97 | * @param mixed $int |
||
98 | * @param mixed $default |
||
99 | * @return integer|null |
||
100 | */ |
||
101 | 2 | public static function integer($int, $default = null): ?int |
|
105 | |||
106 | /** |
||
107 | * Cast an boolean to an boolean value otherwise to null. |
||
108 | * |
||
109 | * @param mixed $bool |
||
110 | * @param mixed $default |
||
111 | * @return boolean|null |
||
112 | */ |
||
113 | public static function boolean($bool, $default = null): ?bool |
||
117 | |||
118 | /** |
||
119 | * Cast an float to an float value otherwise to null. |
||
120 | * |
||
121 | * @param mixed $float |
||
122 | * @param mixed $default |
||
123 | * @return float|null |
||
124 | */ |
||
125 | public static function float($float, $default = null): ?float |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Cast an array to an array value otherwise to null. |
||
133 | * |
||
134 | * @param mixed $array |
||
135 | * @param mixed $default |
||
136 | * @return array|null |
||
137 | */ |
||
138 | public static function toArray($array, $default = null): ?array |
||
142 | |||
143 | /** |
||
144 | * Cast an array to an collection value otherwise to null. |
||
145 | * |
||
146 | * @param mixed $array |
||
147 | * @param mixed $default |
||
148 | * @return Collection|null |
||
149 | */ |
||
150 | public static function collect($array, $default = null): ?Collection |
||
154 | |||
155 | /** |
||
156 | * Return entry ID in given "Link" array. |
||
157 | * |
||
158 | * @param array $entry |
||
159 | * @param mixed $default |
||
160 | * @return string|null |
||
161 | */ |
||
162 | 2 | public static function entryId(array $entry, $default = null): ?string |
|
166 | |||
167 | /** |
||
168 | * Return a Location object |
||
169 | * |
||
170 | * @param array $entry |
||
171 | * @param Location $default |
||
172 | * @return Location|null |
||
173 | */ |
||
174 | public static function location(array $entry, ?Location $default = null): ?Location |
||
178 | } |
||
179 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.