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