Completed
Push — master ( c737a9...f725fb )
by Maxime
03:12
created

Caster::entryId()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Helpers;
4
5
use Distilleries\Contentful\Models\Location;
6
use Illuminate\Support\Collection;
7
use Parsedown;
8
use Illuminate\Support\Carbon;
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;
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
        $data = json_decode($json, true);
70
71 4
        if (json_last_error() !== JSON_ERROR_NONE) {
72 2
            return null;
73
        }
74
75 2
        return $data;
76
    }
77
78
    /**
79
     * Transform markdown content to an HTML string.
80
     *
81
     * @param  mixed  $md
82
     * @param  mixed  $default
83
     * @return string
84
     */
85 4
    public static function markdown($md, $default = null): ?string
86
    {
87 4
        if (empty($md)) {
88 2
            return $default;
89
        }
90
91 2
        return (new Parsedown)->setBreaksEnabled(true)->text($md);
92
    }
93
94
    /**
95
     * Cast an integer to an integer value otherwise to null.
96
     *
97
     * @param  mixed  $int
98
     * @param  mixed  $default
99
     * @return integer|null
100
     */
101 2
    public static function integer($int, $default = null): ?int
102
    {
103 2
        return is_numeric($int) ? (int) $int : $default;
104
    }
105
106
    /**
107
     * Cast an boolean to an boolean value otherwise to null.
108
     *
109
     * @param  mixed  $bool
110
     * @param  mixed  $default
111
     * @return boolean|null
112
     */
113
    public static function boolean($bool, $default = null): ?bool
114
    {
115
        return is_bool($bool) ? (bool) $bool : $default;
116
    }
117
118
    /**
119
     * Cast an float to an float value otherwise to null.
120
     *
121
     * @param  mixed  $float
122
     * @param  mixed  $default
123
     * @return float|null
124
     */
125
    public static function float($float, $default = null): ?float
126
    {
127
        return is_float($float) ? (float) $float : $default;
128
    }
129
130
131
    /**
132
     * Cast an array to an array value otherwise to null.
133
     *
134
     * @param  mixed  $array
135
     * @param  mixed  $default
136
     * @return array|null
137
     */
138
    public static function toArray($array, $default = null): ?array
139
    {
140
        return is_array($array) ? (array) $array : $default;
141
    }
142
143
    /**
144
     * Cast an array to an collection value otherwise to null.
145
     *
146
     * @param  mixed  $array
147
     * @param  mixed  $default
148
     * @return Collection|null
149
     */
150
    public static function collect($array, $default = null): ?Collection
151
    {
152
        return is_array($array) ? collect($array) : $default;
153
    }
154
155
    /**
156
     * Return entry ID in given "Link" array.
157
     *
158
     * @param  array  $entry
159
     * @param  mixed  $default
160
     * @return string|null
161
     */
162 2
    public static function entryId(array $entry, $default = null): ?string
163
    {
164 2
        return (isset($entry['sys']) and isset($entry['sys']['id'])) ? $entry['sys']['id'] : $default;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
165
    }
166
167
    /**
168
     * Return a Location object
169
     *
170
     * @param  array  $entry
171
     * @param  Location  $default
172
     * @return Location|null
173
     */
174
    public static function location(array $entry, ?Location $default = null): ?Location
175
    {
176
        return isset($entry['default'])? new Location($entry['default']):$default;
177
    }
178
}
179