Completed
Push — master ( 184377...e7ee0f )
by Dmitry
05:03
created

Converter::toObject()   C

Complexity

Conditions 11
Paths 19

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.0207

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 17
cts 18
cp 0.9444
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 18
nc 19
nop 1
crap 11.0207

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 Carbon\Carbon;
6
use Exception;
7
use stdClass;
8
use Tarantool\Mapper\Entity;
9
10
class Converter
11
{
12 45
    public function isTuple($array)
13
    {
14 45
        if (is_object($array)) {
15
            $array = get_object_vars($array);
16
        }
17 45
        if (!is_array($array)) {
18 10
            return false;
19
        }
20 45
        return !count($array) || array_keys($array) === range(0, count($array) -1);
21
    }
22
23 45
    public function toObject($data)
24
    {
25 45
        if (is_array($data)) {
26 45
            if ($this->isTuple($data)) {
27 45
                return $this->convertArrayToObject($data);
28
            }
29
        }
30
31 45
        if (is_object($data)) {
32 5
            if ($data instanceof Entity) {
33
                // keep instance
34
                return $data;
35
            }
36
37 5
            $tmp = $data;
38 5
            $data = [];
39 5
            foreach ($tmp as $k => $v) {
40 1
                $data[$k] = $v;
41
            }
42
        }
43
44 45
        $data = (object) $data;
45
46 45
        foreach ($data as $k => $v) {
47 45
            if (is_array($v) && $this->isTuple($v)) {
48 45
                $data->$k = $this->convertArrayToObject($v);
49 45
            } elseif(is_array($v) || is_object($v)) {
50 45
                $data->$k = $this->toObject($v);
51
            }
52
        }
53
54 45
        return $data;
55
    }
56
57 45
    public function convertArrayToObject($data)
58
    {
59 45
        $result = [];
60 45
        foreach ($data as $k => $v) {
61 10
            if ($this->isTuple($v)) {
62
                $result[$k] = $this->convertArrayToObject($v);
63 10
            } elseif (is_object($v) || is_array($v)) {
64
                $result[$k] = $this->toObject($v);
65
            } else {
66 10
                $result[$k] = $v;
67
            }
68
        }
69 45
        return $result;
70
    }
71
72 11
    public function toArray($data) : array
73
    {
74 11
        if (!$data) {
75 5
            return [];
76
        }
77
78 11
        if (is_object($data)) {
79 7
            $data = get_object_vars($data);
80
        }
81
82 11
        foreach ($data as $k => $v) {
83 11
            if (is_array($v) || is_object($v)) {
84 11
                $data[$k] = $this->toArray($v);
85
            }
86
        }
87
88 11
        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
129
    private $dates = [];
130
131 1
    public function getDate($string)
132
    {
133 1
        $key = $string;
134 1
        if (func_num_args() == 3) {
135 1
            $key = implode('.', func_get_args());
136
        }
137
138 1
        if (Carbon::hasTestNow() || !array_key_exists($key, $this->dates)) {
139 1
            if (strlen($string) == 8 && is_numeric($string)) {
140 1
                $value = Carbon::createFromFormat('Ymd', $string)->setTime(0, 0, 0);
141 1
            } elseif (func_num_args() == 3) {
142 1
                [$year, $month, $day] = func_get_args();
0 ignored issues
show
Bug introduced by
The variable $year does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $month does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $day does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
143 1
                $value = Carbon::createFromDate($year, $month, $day)->setTime(0, 0, 0);
144
            } else {
145 1
                $value = Carbon::parse($string);
146
            }
147 1
            if (Carbon::hasTestNow()) {
148 1
                return $value;
149
            }
150 1
            $this->dates[$key] = $value;
151
        }
152 1
        return $this->dates[$key]->copy();
153
    }
154
155 1
    public function getTimestamp($string)
156
    {
157 1
        return $this->getDate($string)->timestamp;
158
    }
159
160
}
161