HasCast::isCommaCastable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sco\Admin\Form\Elements\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Sco\Admin\Form\Elements\Date;
7
use Sco\Admin\Form\Elements\Timestamp;
8
9
/**
10
 * Trait hasCast
11
 *
12
 * @package Sco\Admin\Form\Elements\Concerns
13
 */
14
trait HasCast
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $cast;
20
21
    abstract public function getName();
22
23
    /**
24
     * @return string
25
     */
26
    public function getCast()
27
    {
28
        return $this->cast;
29
    }
30
31
    /**
32
     * @param string $cast
33
     * @return $this
34
     */
35
    public function setCast(string $cast)
36
    {
37
        $this->cast = $cast;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    protected function isCastable()
46
    {
47
        return ! is_null($this->getCast());
48
    }
49
50
    /**
51
     * Determine whether a value is Date / DateTime castable for inbound manipulation.
52
     *
53
     * @return bool
54
     */
55
    protected function isDateCastable()
56
    {
57
        return in_array($this->getCast(), ['date', 'datetime', 'time', 'timestamp']);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    protected function isJsonCastable()
64
    {
65
        return in_array($this->getCast(), ['array', 'json', 'object', 'collection']);
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    protected function isCommaCastable()
72
    {
73
        return $this->getCast() === 'comma';
74
    }
75
76
    /**
77
     * Cast the given value to JSON.
78
     *
79
     * @param mixed $value
80
     * @return string
81
     */
82
    protected function castValueAsJson($value)
83
    {
84
        $value = $this->asJson($value);
85
86
        if ($value === false) {
87
            throw new \RuntimeException(
88
                sprintf(
89
                    "Unable to encode value [%s] for element [%s] to JSON: %s.",
90
                    $this->getName(),
91
                    get_class($this),
92
                    json_last_error_msg()
93
                )
94
            );
95
        }
96
97
        return $value;
98
    }
99
100
    /**
101
     * Encode the given value as JSON.
102
     *
103
     * @param $value
104
     * @return string
105
     */
106
    protected function asJson($value)
107
    {
108
        return json_encode($value);
109
    }
110
111
    /**
112
     * Decode the given JSON back into an array or object.
113
     *
114
     * @param string $value
115
     * @param bool $asObject
116
     * @return mixed
117
     */
118
    protected function fromJson($value, $asObject = false)
119
    {
120
        return json_decode($value, ! $asObject);
121
    }
122
123
    /**
124
     * Cast the given value to string with comma.
125
     *
126
     * @param mixed $value
127
     * @return string
128
     */
129
    protected function castValueAsCommaSeparated($value)
130
    {
131
        if (! is_array($value)) {
132
            $value = (array) $value;
133
        }
134
135
        return $this->asCommaSeparated($value);
136
    }
137
138
    /**
139
     * Join the given value with a comma
140
     *
141
     * @param array $value
142
     * @return string
143
     */
144
    protected function asCommaSeparated(array $value)
145
    {
146
        return implode(',', $value);
147
    }
148
149
    /**
150
     * Split the given value by comma
151
     *
152
     * @param string $value
153
     * @return array
154
     */
155
    protected function fromCommaSeparated(string $value)
156
    {
157
        return explode(',', $value);
158
    }
159
160
    /**
161
     * @param $value
162
     * @return \Carbon\Carbon
163
     */
164
    protected function castValueAsDateTime($value)
165
    {
166
        return $this->asDateTime($value);
0 ignored issues
show
Bug introduced by
The method asDateTime() does not exist on Sco\Admin\Form\Elements\Concerns\HasCast. Did you maybe mean castValueAsDateTime()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
167
    }
168
169
    protected function isElementOfDate()
170
    {
171
        return $this instanceof Date;
172
    }
173
174
    /**
175
     * Cast a value to a native PHP type.
176
     *
177
     * @param $value
178
     * @return array
179
     */
180
    protected function castValue($value)
181
    {
182
        if (is_null($value)) {
183
            return $value;
184
        }
185
186
        if ($this->isElementOfDate() && $this->isDateCastable()) {
187
            return $this->fromDateTime($value);
0 ignored issues
show
Bug introduced by
It seems like fromDateTime() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
188
        }
189
190
        switch ($this->getCast()) {
191
            case 'int':
192
            case 'integer':
193
                return (int) $value;
194
            case 'real':
195
            case 'float':
196
            case 'double':
197
                return (float) $value;
198
            case 'string':
199
                return (string) $value;
200
            case 'bool':
201
            case 'boolean':
202
                return (bool) $value;
203
            case 'object':
204
                return $this->fromJson($value, true);
205
            case 'array':
206
            case 'json':
207
                return $this->fromJson($value);
208
            case 'collection':
209
                return new Collection($this->fromJson($value));
210
            case 'comma':
211
                return $this->fromCommaSeparated($value);
212
            default:
213
                return $value;
214
        }
215
    }
216
}
217