Completed
Push — master ( 699dc6...d8f60e )
by Changwan
05:50
created

ArrayCaster::cast()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Caster\Caster;
3
4
use Wandu\Caster\CasterInterface;
5
6
class ArrayCaster implements CasterInterface
7
{
8
    /**
9
     * {@inheritdoc}
10
     */
11 23
    public function cast($value)
12
    {
13 23
        if (is_string($value)) {
14 13
            $result = json_decode($value, true);
15 13
            if (json_last_error() === \JSON_ERROR_NONE) {
16 10
                return (array) $result;
17
            }
18 3
            $result = json_decode("[{$value}]", true);
19 3
            if (json_last_error() === \JSON_ERROR_NONE) {
20 3
                return (array) $result;
21
            }
22
        }
23 10
        return (array) $value;
24
    }
25
}
26