NullableIntArrayCast::castAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Casts;
4
5
use Vkovic\LaravelCustomCasts\CustomCastBase;
6
7
class NullableIntArrayCast extends CustomCastBase
8
{
9
    /**
10
     * Cast the $value as an array of integer values.
11
     *
12
     *  - set to null if an empty array or null value is passed
13
     *
14
     * @param  $value
15
     * @return string
16
     */
17
    public function setAttribute($value): ?string
18
    {
19
        return ! is_null($value) ? json_encode(array_map('intval', $value)) : null;
20
    }
21
22
    /**
23
     * Retrieve an array of values.
24
     *
25
     * @param  $value
26
     * @return array
27
     */
28
    public function castAttribute($value): ?array
29
    {
30
        return json_decode($value, true);
31
    }
32
}
33