1 | <?php |
||
8 | class Caster |
||
9 | { |
||
10 | /** |
||
11 | * Cast value to a string. |
||
12 | * |
||
13 | * @param mixed $str |
||
14 | * @return string |
||
15 | */ |
||
16 | public static function string($str): string |
||
20 | |||
21 | /** |
||
22 | * Cast value to a string. |
||
23 | * |
||
24 | * @param mixed $str |
||
25 | * @param mixed $default |
||
26 | * @return \Illuminate\Support\Carbon|null |
||
27 | */ |
||
28 | public static function datetime($str, $default = null): ?Carbon |
||
42 | |||
43 | /** |
||
44 | * Transform data to its JSON representation. |
||
45 | * |
||
46 | * @param mixed $data |
||
47 | * @param mixed $default |
||
48 | * @return string |
||
49 | */ |
||
50 | public static function toJson($data, $default = null): string |
||
54 | |||
55 | /** |
||
56 | * Transform a JSON string to its associative array representation. |
||
57 | * |
||
58 | * @param mixed $json |
||
59 | * @return array|null |
||
60 | */ |
||
61 | public static function fromJson($json): ?array |
||
75 | |||
76 | /** |
||
77 | * Transform markdown content to an HTML string. |
||
78 | * |
||
79 | * @param mixed $md |
||
80 | * @param mixed $default |
||
81 | * @return string |
||
82 | */ |
||
83 | public static function markdown($md, $default = null): ?string |
||
91 | |||
92 | /** |
||
93 | * Cast an integer to an integer value otherwise to null. |
||
94 | * |
||
95 | * @param mixed $int |
||
96 | * @param mixed $default |
||
97 | * @return integer|null |
||
98 | */ |
||
99 | public static function integer($int, $default = null): ?int |
||
103 | |||
104 | /** |
||
105 | * Cast an boolean to an boolean value otherwise to null. |
||
106 | * |
||
107 | * @param mixed $bool |
||
108 | * @param mixed $default |
||
109 | * @return boolean|null |
||
110 | */ |
||
111 | public static function boolean($bool, $default = null): ?bool |
||
115 | |||
116 | /** |
||
117 | * Cast an float to an float value otherwise to null. |
||
118 | * |
||
119 | * @param mixed $float |
||
120 | * @param mixed $default |
||
121 | * @return float|null |
||
122 | */ |
||
123 | public static function float($float, $default = null): ?float |
||
127 | |||
128 | /** |
||
129 | * Return entry ID in given "Link" array. |
||
130 | * |
||
131 | * @param array $entry |
||
132 | * @param mixed $default |
||
133 | * @return string|null |
||
134 | */ |
||
135 | public static function entryId(array $entry, $default = null): ?string |
||
139 | } |
||
140 |
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.