1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Helpers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Carbon; |
7
|
|
|
use Contentful\RichText\Renderer; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Distilleries\Contentful\Models\Location; |
10
|
|
|
|
11
|
|
|
class Caster |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Cast value to a string. |
15
|
|
|
* |
16
|
|
|
* @param mixed $str |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
2 |
|
public static function string($str): string |
20
|
|
|
{ |
21
|
2 |
|
return (string) $str; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Cast value to a string. |
26
|
|
|
* |
27
|
|
|
* @param mixed $str |
28
|
|
|
* @param mixed $default |
29
|
|
|
* @return \Illuminate\Support\Carbon|null |
30
|
|
|
*/ |
31
|
|
|
public static function datetime($str, $default = null): ?Carbon |
32
|
|
|
{ |
33
|
|
|
if (empty($str)) { |
34
|
|
|
return $default; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
$carbon = new Carbon($str); |
39
|
|
|
} catch (Exception $e) { |
40
|
|
|
$carbon = null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $carbon; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Transform data to its JSON representation. |
48
|
|
|
* |
49
|
|
|
* @param mixed $data |
50
|
|
|
* @param mixed $default |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
2 |
|
public static function toJson($data, $default = null): string |
54
|
|
|
{ |
55
|
2 |
|
return ! empty($data) ? json_encode($data): $default; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Transform a JSON string to its associative array representation. |
60
|
|
|
* |
61
|
|
|
* @param mixed $json |
62
|
|
|
* @return array|null |
63
|
|
|
*/ |
64
|
6 |
|
public static function fromJson($json): ?array |
65
|
|
|
{ |
66
|
6 |
|
if (empty($json)) { |
67
|
2 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
4 |
|
if (is_array($json)) { |
71
|
|
|
return $json; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
$data = json_decode($json, true); |
75
|
4 |
|
if (json_last_error() !== JSON_ERROR_NONE) { |
76
|
2 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return $data; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Return rendered HTML rich-text data. |
84
|
|
|
* |
85
|
|
|
* @param mixed $object |
86
|
|
|
* @return string|null |
87
|
|
|
*/ |
88
|
|
|
public static function richText($object): ?string |
89
|
|
|
{ |
90
|
|
|
$html = ''; |
91
|
|
|
|
92
|
|
|
if (! empty($object)) { |
93
|
|
|
try { |
94
|
|
|
$node = app('contentful.rich-text.parser')->parse($object); |
95
|
|
|
$html = (new Renderer)->render($node); |
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
dd($e->getMessage()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $html; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Transform markdown content to an HTML string. |
106
|
|
|
* |
107
|
|
|
* @param mixed $md |
108
|
|
|
* @param mixed $default |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
4 |
|
public static function markdown($md, $default = null): ?string |
112
|
|
|
{ |
113
|
4 |
|
if (empty($md)) { |
114
|
2 |
|
return $default; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return (new Parsedown)->setBreaksEnabled(true)->text($md); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Cast an integer to an integer value otherwise to null. |
122
|
|
|
* |
123
|
|
|
* @param mixed $int |
124
|
|
|
* @param mixed $default |
125
|
|
|
* @return integer|null |
126
|
|
|
*/ |
127
|
2 |
|
public static function integer($int, $default = null): ?int |
128
|
|
|
{ |
129
|
2 |
|
return is_numeric($int) ? (int) $int : $default; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Cast an boolean to an boolean value otherwise to null. |
134
|
|
|
* |
135
|
|
|
* @param mixed $bool |
136
|
|
|
* @param mixed $default |
137
|
|
|
* @return boolean|null |
138
|
|
|
*/ |
139
|
|
|
public static function boolean($bool, $default = null): ?bool |
140
|
|
|
{ |
141
|
|
|
return is_bool($bool) ? (bool) $bool : $default; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Cast an float to an float value otherwise to null. |
146
|
|
|
* |
147
|
|
|
* @param mixed $float |
148
|
|
|
* @param mixed $default |
149
|
|
|
* @return float|null |
150
|
|
|
*/ |
151
|
|
|
public static function float($float, $default = null): ?float |
152
|
|
|
{ |
153
|
|
|
return is_numeric($float) ? (float) $float : $default; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Cast an array to an array value otherwise to null. |
158
|
|
|
* |
159
|
|
|
* @param mixed $array |
160
|
|
|
* @param mixed $default |
161
|
|
|
* @return array|null |
162
|
|
|
*/ |
163
|
|
|
public static function toArray($array, $default = null): ?array |
164
|
|
|
{ |
165
|
|
|
return is_array($array) ? (array) $array : $default; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Cast an array to an collection value otherwise to null. |
170
|
|
|
* |
171
|
|
|
* @param mixed $array |
172
|
|
|
* @param mixed $default |
173
|
|
|
* @return Collection|null |
174
|
|
|
*/ |
175
|
|
|
public static function collect($array, $default = null): ?Collection |
176
|
|
|
{ |
177
|
|
|
return is_array($array) ? collect($array) : $default; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return entry ID in given "Link" array. |
182
|
|
|
* |
183
|
|
|
* @param array $entry |
184
|
|
|
* @param mixed $default |
185
|
|
|
* @return string|null |
186
|
|
|
*/ |
187
|
2 |
|
public static function entryId(array $entry, $default = null): ?string |
188
|
|
|
{ |
189
|
2 |
|
return (isset($entry['sys']) && isset($entry['sys']['id'])) ? $entry['sys']['id'] : $default; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return a Location object |
194
|
|
|
* |
195
|
|
|
* @param array $entry |
196
|
|
|
* @param Location $default |
197
|
|
|
* @return Location|null |
198
|
|
|
*/ |
199
|
|
|
public static function location(array $entry, ?Location $default = null): ?Location |
200
|
|
|
{ |
201
|
|
|
return ! empty($entry)? new Location($entry) : $default; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|