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
|
47 |
|
public function isTuple($array) |
13
|
|
|
{ |
14
|
47 |
|
if (is_object($array)) { |
15
|
1 |
|
$array = get_object_vars($array); |
16
|
|
|
} |
17
|
47 |
|
if (!is_array($array)) { |
18
|
10 |
|
return false; |
19
|
|
|
} |
20
|
47 |
|
if (!count($array)) { |
21
|
47 |
|
return true; |
22
|
|
|
} |
23
|
47 |
|
if (version_compare(PHP_VERSION, '7.3.0') >= 0) { |
24
|
47 |
|
return array_key_last($array) === count($array) - 1; |
25
|
|
|
} |
26
|
|
|
return array_values($array) == $array; |
27
|
|
|
} |
28
|
|
|
|
29
|
47 |
|
public function toObject($data) |
30
|
|
|
{ |
31
|
47 |
|
if (is_array($data)) { |
32
|
47 |
|
if ($this->isTuple($data)) { |
33
|
47 |
|
return $this->convertArrayToObject($data); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
47 |
|
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
|
47 |
|
$data = (object) $data; |
51
|
|
|
|
52
|
47 |
|
foreach ($data as $k => $v) { |
53
|
47 |
|
if (is_array($v) && $this->isTuple($v)) { |
54
|
47 |
|
$data->$k = $this->convertArrayToObject($v); |
55
|
47 |
|
} elseif (is_array($v) || is_object($v)) { |
56
|
10 |
|
$data->$k = $this->toObject($v); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
47 |
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
47 |
|
public function convertArrayToObject($data) |
64
|
|
|
{ |
65
|
47 |
|
$result = []; |
66
|
47 |
|
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
|
47 |
|
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
|
5 |
|
$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(); |
|
|
|
|
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
|
1 |
|
public function getPluralForm($n, $forms) |
167
|
|
|
{ |
168
|
1 |
|
return is_float($n)?$forms[1]:($n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2])); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: