-
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathBaseResponse.php
More file actions
122 lines (95 loc) · 3.45 KB
/
BaseResponse.php
File metadata and controls
122 lines (95 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Crescat\SaloonSdkGenerator;
use ReflectionClass;
use ReflectionProperty;
use DateTime;
abstract class BaseResponse implements \JsonSerializable
{
protected static array $complexArrayTypes = [];
protected static array $attributeMap = [];
// No constructor - let child classes handle their own typed constructors
/**
* Deserialize JSON data into a response object
*/
public static function deserialize(array $data, string $class): self
{
$reflection = new ReflectionClass($class);
$constructor = $reflection->getConstructor();
if (!$constructor) {
return new $class();
}
$params = [];
foreach ($constructor->getParameters() as $param) {
$paramName = $param->getName();
// Try different key formats (camelCase, snake_case, PascalCase)
$value = $data[$paramName]
?? $data[self::toSnakeCase($paramName)]
?? $data[ucfirst($paramName)]
?? null;
if ($value === null && $param->isDefaultValueAvailable()) {
$params[] = $param->getDefaultValue();
} else {
$params[] = $value;
}
}
return $reflection->newInstanceArgs($params);
}
/**
* Convert camelCase to snake_case
*/
protected static function toSnakeCase(string $string): string
{
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string));
}
public function toArray(): array
{
$result = [];
$reflection = new ReflectionClass($this);
foreach ($reflection->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
if ($property->isStatic()) {
continue;
}
$key = $property->getName();
if (!$property->isInitialized($this)) {
continue;
}
$value = $property->getValue($this);
if ($value === null) {
continue;
}
// Convert property name to camelCase for JSON
$jsonKey = $this->toCamelCase($key);
if (is_object($value) && method_exists($value, 'toArray')) {
$result[$jsonKey] = $value->toArray();
} elseif ($value instanceof DateTime) {
$result[$jsonKey] = $value->format('Y-m-d\TH:i:s\Z');
} elseif (is_array($value)) {
$result[$jsonKey] = array_map(function ($item) {
if (is_object($item) && method_exists($item, 'toArray')) {
return $item->toArray();
} elseif ($item instanceof DateTime) {
return $item->format('Y-m-d\TH:i:s\Z');
}
return $item;
}, $value);
} else {
$result[$jsonKey] = $value;
}
}
return $result;
}
public function jsonSerialize(): array
{
return $this->toArray();
}
public function toJson(): string
{
return json_encode($this->toArray());
}
protected function toCamelCase(string $string): string
{
// Convert snake_case or PascalCase to camelCase
$string = str_replace('_', '', ucwords($string, '_'));
return lcfirst($string);
}
}