Passed
Pull Request — master (#8)
by
unknown
05:39
created

Caster::collect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Helpers;
4
5
use Exception;
6
use Illuminate\Support\Carbon;
7
use Illuminate\Support\Collection;
8
use Distilleries\Contentful\Models\Location;
9
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
19
    {
20 2
        return (string) $str;
21
    }
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
31
    {
32
        if (empty($str)) {
33
            return $default;
34
        }
35
36
        try {
37
            $carbon = new Carbon($str);
38
        } catch (Exception $e) {
39
            $carbon = null;
40
        }
41
42
        return $carbon;
43
    }
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
53
    {
54 2
        return ! empty($data) ? json_encode($data): $default;
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! empty($data) ? ...ncode($data) : $default could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
55
    }
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
64
    {
65 6
        if (empty($json)) {
66 2
            return null;
67
        }
68
69 4
        if (is_array($json)) {
70
            return $json;
71
        }
72
73 4
        $data = json_decode($json, true);
74 4
        if (json_last_error() !== JSON_ERROR_NONE) {
75 2
            return null;
76
        }
77
78 2
        return $data;
79
    }
80
81
    /**
82
     * Transform markdown content to an HTML string.
83
     *
84
     * @param  mixed  $md
85
     * @param  mixed  $default
86
     * @return string
87
     */
88 4
    public static function markdown($md, $default = null): ?string
89
    {
90 4
        if (empty($md)) {
91 2
            return $default;
92
        }
93
94 2
        return (new Parsedown)->setBreaksEnabled(true)->text($md);
95
    }
96
97
    /**
98
     * Cast an integer to an integer value otherwise to null.
99
     *
100
     * @param  mixed  $int
101
     * @param  mixed  $default
102
     * @return integer|null
103
     */
104 2
    public static function integer($int, $default = null): ?int
105
    {
106 2
        return is_numeric($int) ? (int) $int : $default;
107
    }
108
109
    /**
110
     * Cast an boolean to an boolean value otherwise to null.
111
     *
112
     * @param  mixed  $bool
113
     * @param  mixed  $default
114
     * @return boolean|null
115
     */
116
    public static function boolean($bool, $default = null): ?bool
117
    {
118
        return is_bool($bool) ? (bool) $bool : $default;
119
    }
120
121
    /**
122
     * Cast an float to an float value otherwise to null.
123
     *
124
     * @param  mixed  $float
125
     * @param  mixed  $default
126
     * @return float|null
127
     */
128
    public static function float($float, $default = null): ?float
129
    {
130
        return is_numeric($float) ? (float) $float : $default;
131
    }
132
133
    /**
134
     * Cast an array to an array value otherwise to null.
135
     *
136
     * @param  mixed  $array
137
     * @param  mixed  $default
138
     * @return array|null
139
     */
140
    public static function toArray($array, $default = null): ?array
141
    {
142
        return is_array($array) ? (array) $array : $default;
143
    }
144
145
    /**
146
     * Cast an array to an collection value otherwise to null.
147
     *
148
     * @param  mixed  $array
149
     * @param  mixed  $default
150
     * @return Collection|null
151
     */
152
    public static function collect($array, $default = null): ?Collection
153
    {
154
        return is_array($array) ? collect($array) : $default;
155
    }
156
157
    /**
158
     * Return entry ID in given "Link" array.
159
     *
160
     * @param  array  $entry
161
     * @param  mixed  $default
162
     * @return string|null
163
     */
164 2
    public static function entryId(array $entry, $default = null): ?string
165
    {
166 2
        return (isset($entry['sys']) && isset($entry['sys']['id'])) ? $entry['sys']['id'] : $default;
167
    }
168
169
    /**
170
     * Return a Location object
171
     *
172
     * @param  array  $entry
173
     * @param  Location  $default
174
     * @return Location|null
175
     */
176
    public static function location(array $entry, ?Location $default = null): ?Location
177
    {
178
        return ! empty($entry)? new Location($entry) : $default;
179
    }
180
}
181