Completed
Push — master ( 3ff673...359e01 )
by Dmitry
05:47
created

Converter::toObject()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.5625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
ccs 12
cts 16
cp 0.75
rs 4.8196
cc 10
eloc 16
nc 17
nop 1
crap 11.5625

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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