1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class Converter |
9
|
|
|
{ |
10
|
22 |
|
protected function isTuple($array) |
11
|
|
|
{ |
12
|
22 |
|
return !count($array) || array_keys($array) === range(0, count($array) -1); |
13
|
|
|
} |
14
|
|
|
|
15
|
22 |
|
public function toObject($data) : stdClass |
16
|
|
|
{ |
17
|
22 |
|
if (is_array($data)) { |
18
|
22 |
|
if ($this->isTuple($data)) { |
19
|
|
|
throw new Exception('Tuple should not be converted to object'); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
22 |
|
$data = (object) $data; |
24
|
|
|
|
25
|
22 |
|
foreach ($data as $k => $v) { |
26
|
22 |
|
if (is_array($v) || is_object($v)) { |
27
|
22 |
|
if (is_array($v) && $this->isTuple($v)) { |
28
|
22 |
|
$array = []; |
29
|
22 |
|
foreach ($v as $vk => $vv) { |
30
|
4 |
|
if (is_object($vv) || is_array($vv)) { |
31
|
|
|
$array[$vk] = $this->toObject($vv); |
32
|
|
|
} else { |
33
|
4 |
|
$array[$vk] = $vv; |
34
|
|
|
} |
35
|
|
|
} |
36
|
22 |
|
$data->$k = $array; |
37
|
|
|
} else { |
38
|
22 |
|
$data->$k = $this->toObject($v); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
22 |
|
return $data; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function toArray($data) : array |
47
|
|
|
{ |
48
|
|
|
if (!$data) { |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (is_object($data)) { |
53
|
|
|
$data = get_object_vars($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($data as $k => $v) { |
57
|
|
|
if (is_array($v) || is_object($v)) { |
58
|
|
|
$data[$k] = $this->toArray($v); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private $underscores = []; |
66
|
|
|
|
67
|
1 |
|
public function toUnderscore(string $input) : string |
68
|
|
|
{ |
69
|
1 |
|
if (!array_key_exists($input, $this->underscores)) { |
70
|
1 |
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
71
|
1 |
|
$ret = $matches[0]; |
72
|
1 |
|
foreach ($ret as &$match) { |
73
|
1 |
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); |
74
|
|
|
} |
75
|
1 |
|
$this->underscores[$input] = implode('_', $ret); |
76
|
|
|
} |
77
|
1 |
|
return $this->underscores[$input]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private $camelcased = []; |
81
|
|
|
|
82
|
1 |
|
public function toCamelCase(string $string, bool $capitalize = false) : string |
83
|
|
|
{ |
84
|
1 |
|
$capitalize = $capitalize ? 1 : 0; |
85
|
|
|
|
86
|
1 |
|
if (!array_key_exists($capitalize, $this->camelcased)) { |
87
|
1 |
|
$this->camelcased[$capitalize] = []; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if (!array_key_exists($string, $this->camelcased[$capitalize])) { |
91
|
1 |
|
$chain = explode('_', $string); |
92
|
1 |
|
foreach ($chain as $index => $word) { |
93
|
1 |
|
$chain[$index] = $index || $capitalize ? ucfirst($word) : $word; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$this->camelcased[$capitalize][$string] = implode('', $chain); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $this->camelcased[$capitalize][$string]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|