Completed
Push — master ( d3c009...e173f1 )
by Dmitry
03:16
created

Converter::isTuple()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4222
c 0
b 0
f 0
cc 5
nc 8
nop 1
crap 5.025
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 1
            $array = get_object_vars($array);
16
        }
17 45
        if (!is_array($array)) {
18 10
            return false;
19
        }
20 45
        if (!count($array)) {
21 45
            return true;
22
        }
23 45
        if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
24
            return array_key_last($array) === count($array) - 1;
25
        }
26 45
        return array_values($array) == $array;
27
    }
28
29 45
    public function toObject($data)
30
    {
31 45
        if (is_array($data)) {
32 45
            if ($this->isTuple($data)) {
33 45
                return $this->convertArrayToObject($data);
34
            }
35
        }
36
37 45
        if (is_object($data)) {
38 5
            if ($data instanceof Entity) {
39
                // keep instance
40
                return $data;
41
            }
42
43 5
            $tmp = $data;
44 5
            $data = [];
45 5
            foreach ($tmp as $k => $v) {
46 2
                $data[$k] = $v;
47
            }
48
        }
49
50 45
        $data = (object) $data;
51
52 45
        foreach ($data as $k => $v) {
53 45
            if (is_array($v) && $this->isTuple($v)) {
54 45
                $data->$k = $this->convertArrayToObject($v);
55 45
            } elseif (is_array($v) || is_object($v)) {
56 45
                $data->$k = $this->toObject($v);
57
            }
58
        }
59
60 45
        return $data;
61
    }
62
63 45
    public function convertArrayToObject($data)
64
    {
65 45
        $result = [];
66 45
        foreach ($data as $k => $v) {
67 10
            if ($this->isTuple($v)) {
68
                $result[$k] = $this->convertArrayToObject($v);
69 10
            } elseif (is_object($v) || is_array($v)) {
70
                $result[$k] = $this->toObject($v);
71
            } else {
72 10
                $result[$k] = $v;
73
            }
74
        }
75 45
        return $result;
76
    }
77
78 11
    public function toArray($data) : array
79
    {
80 11
        if (!$data) {
81 5
            return [];
82
        }
83
84 11
        if (is_object($data)) {
85 7
            $data = get_object_vars($data);
86
        }
87
88 11
        foreach ($data as $k => $v) {
89 11
            if (is_array($v) || is_object($v)) {
90 11
                $data[$k] = $this->toArray($v);
91
            }
92
        }
93
94 11
        return $data;
95
    }
96
97
    private $underscores = [];
98
99 1
    public function toUnderscore(string $input) : string
100
    {
101 1
        if (!array_key_exists($input, $this->underscores)) {
102 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
103 1
            $ret = $matches[0];
104 1
            foreach ($ret as &$match) {
105 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
106
            }
107 1
            $this->underscores[$input] = implode('_', $ret);
108
        }
109 1
        return $this->underscores[$input];
110
    }
111
112
    private $camelcased = [];
113
114 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
115
    {
116 1
        $capitalize = $capitalize ? 1 : 0;
117
118 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
119 1
            $this->camelcased[$capitalize] = [];
120
        }
121
122 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
123 1
            $chain = explode('_', $string);
124 1
            foreach ($chain as $index => $word) {
125 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
126
            }
127
128 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
129
        }
130
131 1
        return $this->camelcased[$capitalize][$string];
132
    }
133
134
135
    private $dates = [];
136
137 1
    public function getDate($string)
138
    {
139 1
        $key = $string;
140 1
        if (func_num_args() == 3) {
141 1
            $key = implode('.', func_get_args());
142
        }
143
144 1
        if (Carbon::hasTestNow() || !array_key_exists($key, $this->dates)) {
145 1
            if (strlen($string) == 8 && is_numeric($string)) {
146 1
                $value = Carbon::createFromFormat('Ymd', $string)->setTime(0, 0, 0);
147 1
            } elseif (func_num_args() == 3) {
148 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...
149 1
                $value = Carbon::createFromDate($year, $month, $day)->setTime(0, 0, 0);
150
            } else {
151 1
                $value = Carbon::parse($string);
152
            }
153 1
            if (Carbon::hasTestNow()) {
154 1
                return $value;
155
            }
156 1
            $this->dates[$key] = $value;
157
        }
158 1
        return $this->dates[$key]->copy();
159
    }
160
161 1
    public function getTimestamp($string)
162
    {
163 1
        return $this->getDate($string)->timestamp;
164
    }
165
}
166