Completed
Push — master ( bb7ad2...0385a1 )
by Dmitry
02:27
created

Converter::toCamelCase()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

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