Completed
Push — master ( c8bd96...bb7ad2 )
by Dmitry
02:21
created

Converter::convertArrayToObject()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 5.2742
1
<?php
2
3
namespace Basis;
4
5
use Exception;
6
use stdClass;
7
use Tarantool\Mapper\Entity;
8
9
class Converter
10
{
11 23
    protected function isTuple($array)
12
    {
13 23
        if (is_object($array)) {
14
            $array = get_object_vars($array);
15
        }
16 23
        if (!is_array($array)) {
17 8
            return false;
18
        }
19 23
        return !count($array) || array_keys($array) === range(0, count($array) -1);
20
    }
21
22 23
    public function toObject($data)
23
    {
24 23
        if (is_array($data)) {
25 23
            if ($this->isTuple($data)) {
26 1
                return $this->convertArrayToObject($data);
27
            }
28
        }
29
30 23
        if (is_object($data)) {
31
            if ($data instanceof Entity) {
32
                // keep instance
33
                return $data;
34
            }
35
36
            $tmp = $data;
37
            $data = [];
38
            foreach ($tmp as $k => $v) {
39
                $data[$k] = $v;
40
            }
41
        } else {
42 23
            $data = (object) $data;
43
        }
44
45
46 23
        foreach ($data as $k => $v) {
47 23
            if (is_array($v) && $this->isTuple($v)) {
48 23
                $data->$k = $this->convertArrayToObject($v);
49 23
            } elseif(is_array($v) || is_object($v)) {
50 23
                $data->$k = $this->toObject($v);
51
            }
52
        }
53
54 23
        return $data;
55
    }
56
57 23
    public function convertArrayToObject($data)
58
    {
59 23
        $result = [];
60 23
        foreach ($data as $k => $v) {
61 8
            if ($this->isTuple($v)) {
62
                $result[$k] = $this->convertArrayToObject($v);
63 8
            } elseif (is_object($v) || is_array($v)) {
64
                $result[$k] = $this->toObject($v);
65
            } else {
66 8
                $result[$k] = $v;
67
            }
68
        }
69 23
        return $result;
70
    }
71
72 1
    public function toArray($data) : array
73
    {
74 1
        if (!$data) {
75 1
            return [];
76
        }
77
78 1
        if (is_object($data)) {
79 1
            $data = get_object_vars($data);
80
        }
81
82 1
        foreach ($data as $k => $v) {
83 1
            if (is_array($v) || is_object($v)) {
84 1
                $data[$k] = $this->toArray($v);
85
            }
86
        }
87
88 1
        return $data;
89
    }
90
91
    private $underscores = [];
92
93 1
    public function toUnderscore(string $input) : string
94
    {
95 1
        if (!array_key_exists($input, $this->underscores)) {
96 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
97 1
            $ret = $matches[0];
98 1
            foreach ($ret as &$match) {
99 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
100
            }
101 1
            $this->underscores[$input] = implode('_', $ret);
102
        }
103 1
        return $this->underscores[$input];
104
    }
105
106
    private $camelcased = [];
107
108 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
109
    {
110 1
        $capitalize = $capitalize ? 1 : 0;
111
112 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
113 1
            $this->camelcased[$capitalize] = [];
114
        }
115
116 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
117 1
            $chain = explode('_', $string);
118 1
            foreach ($chain as $index => $word) {
119 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
120
            }
121
122 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
123
        }
124
125 1
        return $this->camelcased[$capitalize][$string];
126
    }
127
}
128