Completed
Push — master ( f5b8f3...bcb3b5 )
by Alexandr
01:26
created

Cast::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bricks\Data;
4
5
6
use Bricks\Exception\ConfigurationException;
7
use Bricks\Helper\ArrHelper;
8
use Carbon\Carbon;
9
10
/**
11
 * Class Cast
12
 * @package Bricks\Data
13
 */
14
class Cast implements CastInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     * @param array $config
23
     */
24 11
    private function __construct(array $config)
25
    {
26 11
        $this->config = ArrHelper::unflatten($config);
27 11
    }
28
29
    /**
30
     * @param array $config
31
     * @return static
32
     */
33 11
    public static function create(array $config)
34
    {
35 11
        return new static($config);
36
    }
37
38
    /**
39
     * @param array $data
40
     * @return array
41
     */
42 10
    public function execute(array $data): array
43
    {
44 10
        return $this->cast($data, $this->config);
45
    }
46
47
    /**
48
     * @param mixed $data
49
     * @param array $config
50
     * @return array
51
     */
52 10
    private function cast($data, array $config)
53
    {
54 10
        $casted = [];
55 10
        foreach ($data as $field => $value) {
56 10
            if (is_array($value) && !empty($value) && ArrHelper::isStrKeysArray($value)) {
57 1
                $casted[$field] = $this->cast($value, $config[$field]);
58
            } else {
59 10
                $method = $config[$field] ?? null;
60 10
                if (empty($method) || $value === null) {
61 2
                    $casted[$field] = $value;
62
                } else {
63 10
                    if (!method_exists($this, $method)) {
64 1
                        throw new ConfigurationException('unknown field type: ' . $method);
65
                    }
66 9
                    $casted[$field] = call_user_func([$this, $method], $value);
67
                }
68
            }
69
        }
70 9
        return $casted;
71
    }
72
73
    /**
74
     * @param string $value
75
     * @return Carbon
76
     */
77 1
    private function dateTime(string $value)
78
    {
79 1
        return Carbon::parse($value);
80
    }
81
82
    /**
83
     * @param array $value
84
     * @return array
85
     */
86
    private function intArray(array $value)
87
    {
88 6
        $array = array_map(function ($val) {
89 5
            return (int)$val;
90 6
        }, array_filter($value));
91
92 6
        return array_values($array);
93
    }
94
95
    /**
96
     * @param array $value
97
     * @return array
98
     */
99
    private function floatArray(array $value)
100
    {
101
        $array = array_map(function ($val) {
102
            return (float)$val;
103
        }, array_filter($value));
104
105
        return array_values($array);
106
    }
107
108
    /**
109
     * @param array $value
110
     * @return array
111
     */
112
    private function strArray(array $value)
113
    {
114
        $array = array_map(function ($val) {
115
            return (string)$val;
116
        }, array_filter($value));
117
118
        return array_values($array);
119
    }
120
121
    /**
122
     * @param string|int $value
123
     * @return int
124
     */
125 6
    private function integer($value)
126
    {
127 6
        return (int)$value;
128
    }
129
130
    /**
131
     * @param string|float $value
132
     * @return int
133
     */
134
    private function float($value)
135
    {
136
        return (float)$value;
137
    }
138
139
    /**
140
     * @param string|int $value
141
     * @return int
142
     */
143 6
    private function string($value)
144
    {
145 6
        return (string)$value;
146
    }
147
148
    /**
149
     * @param string|array $value
150
     * @return array
151
     */
152
    private function array($value)
153
    {
154
        return (array)$value;
155
    }
156
157
    /**
158
     * @param string|bool $value
159
     * @return bool
160
     */
161 1
    private function boolean($value)
162
    {
163 1
        return is_string($value) ? $value === 'true' : (bool)$value;
164
    }
165
}