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