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

Cast::string()   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 7
    private function __construct(array $config)
25
    {
26 7
        foreach ($config as $key => $type) {
27 7
            $parts = explode('.', $key, 2);
28 7
            if (isset($parts[1])) {
29 1
                if (isset($this->config[$parts[0]]) && is_string($this->config[$parts[0]])) {
30 1
                    $this->config[$parts[0]] = [];
31
                }
32 1
                $this->config[$parts[0]][$parts[1]] = $type;
33
            } else {
34 7
                $this->config[$parts[0]] = $type;
35
            }
36
        }
37 7
    }
38
39
    /**
40
     * @param array $config
41
     * @return static
42
     */
43 7
    public static function create(array $config)
44
    {
45 7
        return new static($config);
46
    }
47
48
    /**
49
     * @param array $data
50
     * @return array
51
     */
52 6
    public function execute(array $data): array
53
    {
54 6
        $casted = [];
55 6
        foreach ($data as $field => $value) {
56 6
            if (is_array($value) && ArrHelper::isStrKeysArray($value)) {
57 1
                foreach ($value as $key => $val) {
58
                    $method = $this->config[$field][$key] ?? null;
59 View Code Duplication
                    if (empty($method) || $val === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                        $casted[$field][$key] = $val;
61
                    } else {
62
                        if (!method_exists($this, $method)) {
63
                            throw new ConfigurationException('unknown field type: ' . $method);
64
                        }
65 1
                        $casted[$field][$key] = call_user_func([$this, $method], $value);
66
                    }
67
                }
68 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69 6
                $method = $this->config[$field] ?? null;
70 6
                if (empty($method) || $value === null) {
71 2
                    $casted[$field] = $value;
72
                } else {
73 5
                    if (!method_exists($this, $method)) {
74
                        throw new ConfigurationException('unknown field type: ' . $method);
75
                    }
76 6
                    $casted[$field] = call_user_func([$this, $method], $value);
77
                }
78
            }
79
        }
80 6
        return $casted;
81
    }
82
83
    /**
84
     * @param string $value
85
     * @return Carbon
86
     */
87
    private function dateTime(string $value)
88
    {
89
        return Carbon::parse($value);
90
    }
91
92
    /**
93
     * @param array $value
94
     * @return array
95
     */
96
    private function intArray(array $value)
97
    {
98 4
        $array = array_map(function ($val) {
99 4
            return (int)$val;
100 4
        }, $value);
101
102 4
        return array_values(array_filter($array));
103
    }
104
105
    /**
106
     * @param string $value
107
     * @return string
108
     */
109
    private function email(string $value)
110
    {
111
        return mb_strtolower($value, 'utf-8');
112
    }
113
114
    /**
115
     * @param string|int $value
116
     * @return int
117
     */
118 5
    private function integer($value)
119
    {
120 5
        return (int)$value;
121
    }
122
123
    /**
124
     * @param string|float $value
125
     * @return int
126
     */
127
    private function float($value)
128
    {
129
        return (float)$value;
130
    }
131
132
    /**
133
     * @param string|int $value
134
     * @return int
135
     */
136 5
    private function string($value)
137
    {
138 5
        return (string)$value;
139
    }
140
141
    /**
142
     * @param string|array $value
143
     * @return array
144
     */
145
    private function array($value)
146
    {
147
        return (array)$value;
148
    }
149
150
    /**
151
     * @param string|bool $value
152
     * @return bool
153
     */
154
    private function boolean($value)
155
    {
156
        return is_string($value) ? $value === 'true' : (bool)$value;
157
    }
158
}