Completed
Push — master ( 46c7bb...4858ea )
by Arthur
02:48
created

ResolveCasting::processCasting()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.0877

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 8.4444
cc 8
nc 4
nop 2
crap 8.0877
1
<?php
2
3
namespace Larapie\Actions\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Larapie\Actions\Attribute;
7
8
trait ResolveCasting
9
{
10 10
    protected function resolveAttributeCasting(array $data)
11
    {
12 10
        $attributes = (new Collection($this->rules()))
0 ignored issues
show
Bug introduced by
It seems like rules() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

12
        $attributes = (new Collection($this->/** @scrutinizer ignore-call */ rules()))
Loading history...
13
            ->filter(function ($rule) {
14 9
                return $rule instanceof Attribute;
15 10
            });
16
17
        return $attributes->intersectByKeys($data)->map(function (Attribute $attribute, $key) use ($data) {
18 6
            return $this->processCasting($attribute, $data[$key]);
19 10
        })->toArray();
20
    }
21
22 6
    private function processCasting(Attribute $attribute, $value)
23
    {
24 6
        if (($castFunction = $attribute->getCast()) !== null && is_callable($castFunction))
25 2
            return $castFunction($value);
26
27 6
        if ((($type = $attribute->getCast()) !== null && is_string($type)) ||
28 6
            (is_string($type = $attribute->cast(null)) && in_array(strtolower($type), ["bool", "boolean", "string", "double", "float", "int", "integer", "array", "object"]))) {
29 2
            return $this->castFromString($type, $value);
30
        }
31
32 4
        if (!($attribute->cast(null) instanceof Attribute))
0 ignored issues
show
introduced by
$attribute->cast(null) is always a sub-type of Larapie\Actions\Attribute.
Loading history...
33
            return $attribute->cast($value);
34
35 4
        return $value;
36
    }
37
38 2
    private function castFromString(string $type, $value)
39
    {
40 2
        switch (strtolower($type)) {
41 2
            case "boolean":
42 2
            case "bool" :
43 1
                return (bool)$value;
44 1
            case "string":
45 1
                return (string)$value;
46
            case "double":
47
                return (double)$value;
48
            case "integer":
49
            case "int":
50
                return (int)$value;
51
            case "float":
52
                return (float)$value;
53
            case "array":
54
                return (array)$value;
55
            case "object":
56
                return (object)$value;
57
            default:
58
                throw new \RuntimeException("cast type not supported");
59
        }
60
    }
61
}
62