1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ag84ark\ObjectConvertor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
class ObjectConvertor |
8
|
|
|
{ |
9
|
24 |
|
public static function toObject(array $array, $object) |
10
|
|
|
{ |
11
|
24 |
|
$class = get_class($object); |
12
|
|
|
|
13
|
24 |
|
$methods = get_class_methods($class); |
14
|
|
|
|
15
|
24 |
|
foreach ($methods as $method) { |
16
|
24 |
|
preg_match('/^(set)(.*?)$/i', $method, $results); |
17
|
|
|
|
18
|
24 |
|
$pre = $results[1] ?? ''; |
19
|
|
|
|
20
|
24 |
|
$k = $results[2] ?? ''; |
21
|
|
|
|
22
|
24 |
|
$k = strtolower(substr($k, 0, 1)).substr($k, 1); |
23
|
24 |
|
$k2 = Str::snake($k); |
24
|
|
|
|
25
|
24 |
|
if ('set' === $pre && ! empty($array[$k])) { |
26
|
18 |
|
$object->$method($array[$k]); |
27
|
24 |
|
} elseif ('set' === $pre && ! empty($array[$k2])) { |
28
|
4 |
|
$object->$method($array[$k2]); |
29
|
24 |
|
} elseif ('set' === $pre && ! empty($array[Str::ucfirst($k)])) { |
30
|
13 |
|
$object->$method($array[Str::ucfirst($k)]); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
24 |
|
return $object; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $object |
39
|
|
|
*/ |
40
|
12 |
|
public static function toObjectBaseModelApi(array $array, $object): BaseModelApi |
41
|
|
|
{ |
42
|
12 |
|
$class = get_class($object); |
43
|
|
|
|
44
|
12 |
|
$methods = get_class_methods($class); |
45
|
|
|
|
46
|
12 |
|
foreach ($methods as $method) { |
47
|
12 |
|
preg_match('/^(set)(.*?)$/i', $method, $results); |
48
|
|
|
|
49
|
12 |
|
$pre = $results[1] ?? ''; |
50
|
|
|
|
51
|
12 |
|
$k = $results[2] ?? ''; |
52
|
|
|
|
53
|
12 |
|
$k = strtolower(substr($k, 0, 1)).substr($k, 1); |
54
|
|
|
|
55
|
12 |
|
$k2 = Str::snake($k); |
56
|
|
|
|
57
|
12 |
|
if ('set' === $pre && ! empty($array[$k])) { |
58
|
12 |
|
$object->$method($array[$k]); |
59
|
12 |
|
} elseif ('set' === $pre && ! empty($array[$k2])) { |
60
|
|
|
$object->$method($array[$k2]); |
61
|
12 |
|
} elseif ('set' === $pre && ! empty($array[Str::ucfirst($k)])) { |
62
|
6 |
|
$object->$method($array[Str::ucfirst($k)]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
12 |
|
return $object; |
67
|
|
|
} |
68
|
|
|
|
69
|
14 |
|
public static function toArray($object, bool $withNulls = true): array |
70
|
|
|
{ |
71
|
14 |
|
$array = []; |
72
|
|
|
|
73
|
14 |
|
$class = get_class($object); |
74
|
|
|
|
75
|
14 |
|
$methods = get_class_methods($class); |
76
|
|
|
|
77
|
14 |
|
foreach ($methods as $method) { |
78
|
14 |
|
preg_match(' /^(get)(.*?)$/i', $method, $results); |
79
|
|
|
|
80
|
14 |
|
$pre = $results[1] ?? ''; |
81
|
|
|
|
82
|
14 |
|
$k = $results[2] ?? ''; |
83
|
|
|
|
84
|
14 |
|
$k = strtolower(substr($k, 0, 1)).substr($k, 1); |
85
|
|
|
|
86
|
14 |
|
if ('get' == $pre) { |
87
|
14 |
|
$array[$k] = $object->$method(); |
88
|
14 |
|
if (is_object($array[$k])) { |
89
|
4 |
|
if (method_exists($array[$k], 'toArray')) { |
90
|
4 |
|
$array[$k] = $array[$k]->toArray($withNulls, $withNulls); |
91
|
|
|
} else { |
92
|
4 |
|
$array[$k] = self::toArray($array[$k], $withNulls); |
93
|
|
|
} |
94
|
|
|
} |
95
|
14 |
|
if (is_array($array[$k])) { |
96
|
4 |
|
foreach ($array[$k] as $key => $value) { |
97
|
4 |
|
if (is_object($array[$k][$key])) { |
98
|
|
|
if (method_exists($array[$k][$key], 'toArray')) { |
99
|
|
|
$array[$k][$key] = $array[$k][$key]->toArray($withNulls); |
100
|
|
|
} else { |
101
|
|
|
$array[$k][$key] = self::toArray($array[$k][$key], $withNulls); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (false === $withNulls && null === $array[$k][$key]) { |
105
|
2 |
|
unset($array[$k][$key]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
// Remove null values if option is set |
111
|
14 |
|
if (false === $withNulls && null === $array[$k]) { |
112
|
8 |
|
unset($array[$k]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
14 |
|
return $array; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|