1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Helpers; |
4
|
|
|
|
5
|
|
|
use Parsedown; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
|
8
|
|
|
class Caster |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Cast value to a string. |
12
|
|
|
* |
13
|
|
|
* @param mixed $str |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
2 |
|
public static function string($str): string |
17
|
|
|
{ |
18
|
2 |
|
return (string) $str; |
19
|
|
|
} |
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 |
29
|
|
|
{ |
30
|
|
|
if (empty($str)) { |
31
|
|
|
return $default; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
$carbon = new Carbon($str); |
36
|
|
|
} catch (\Exception $e) { |
37
|
|
|
$carbon = null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $carbon; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Transform data to its JSON representation. |
45
|
|
|
* |
46
|
|
|
* @param mixed $data |
47
|
|
|
* @param mixed $default |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
2 |
|
public static function toJson($data, $default = null): string |
51
|
|
|
{ |
52
|
2 |
|
return ! empty($data) ? json_encode($data): $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Transform a JSON string to its associative array representation. |
57
|
|
|
* |
58
|
|
|
* @param mixed $json |
59
|
|
|
* @return array|null |
60
|
|
|
*/ |
61
|
6 |
|
public static function fromJson($json): ?array |
62
|
|
|
{ |
63
|
6 |
|
if (empty($json)) { |
64
|
2 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$data = json_decode($json, true); |
68
|
|
|
|
69
|
4 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
70
|
2 |
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Transform markdown content to an HTML string. |
78
|
|
|
* |
79
|
|
|
* @param mixed $md |
80
|
|
|
* @param mixed $default |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
4 |
|
public static function markdown($md, $default = null): ?string |
84
|
|
|
{ |
85
|
4 |
|
if (empty($md)) { |
86
|
2 |
|
return $default; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return (new Parsedown)->setBreaksEnabled(true)->text($md); |
90
|
|
|
} |
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
|
2 |
|
public static function integer($int, $default = null): ?int |
100
|
|
|
{ |
101
|
2 |
|
return is_numeric($int) ? (int) $int : $default; |
102
|
|
|
} |
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 |
112
|
|
|
{ |
113
|
|
|
return is_bool($bool) ? (bool) $bool : $default; |
114
|
|
|
} |
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 |
124
|
|
|
{ |
125
|
|
|
return is_float($float) ? (float) $float : $default; |
126
|
|
|
} |
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
|
2 |
|
public static function entryId(array $entry, $default = null): ?string |
136
|
|
|
{ |
137
|
2 |
|
return (isset($entry['sys']) and isset($entry['sys']['id'])) ? $entry['sys']['id'] : $default; |
|
|
|
|
138
|
|
|
} |
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.