@@ -24,11 +24,11 @@ abstract class BaseObject implements JsonSerializable
24
24
use QuickInitTrait;
25
25
26
26
/**
27
- * Field alias mapping. [alias => field]
27
+ * JSON field mapping. Format: [json-field => field]
28
28
*
29
29
* @var array
30
30
*/
31
- protected array $ _aliasMap = [];
31
+ protected array $ _jsonMap = [];
32
32
33
33
/**
34
34
* Class constructor.
@@ -58,12 +58,12 @@ protected function init(): void
58
58
* - Automatically convert fields to camelCase format
59
59
*
60
60
* @param array $data
61
- * @param array $aliasMap
61
+ * @param array $jsonMap json field mapping
62
62
*/
63
- public function load (array $ data , array $ aliasMap = []): void
63
+ public function load (array $ data , array $ jsonMap = []): void
64
64
{
65
65
if ($ data ) {
66
- Obj::init ($ this , $ data , true , $ aliasMap ?: $ this ->_aliasMap );
66
+ Obj::init ($ this , $ data , true , $ jsonMap ?: $ this ->_jsonMap );
67
67
}
68
68
}
69
69
@@ -76,7 +76,7 @@ public function load(array $data, array $aliasMap = []): void
76
76
*/
77
77
public function setValue (string $ field , mixed $ value ): static
78
78
{
79
- Obj::init ($ this , [$ field => $ value ], true , $ this ->_aliasMap );
79
+ Obj::init ($ this , [$ field => $ value ], true , $ this ->_jsonMap );
80
80
return $ this ;
81
81
}
82
82
@@ -85,19 +85,20 @@ public function setValue(string $field, mixed $value): static
85
85
*/
86
86
public function toArray (): array
87
87
{
88
- return Obj:: toArray ( $ this , false , false );
88
+ return $ this -> convToMap ( true , true );
89
89
}
90
90
91
91
/**
92
92
* Convert to array map.
93
93
*
94
- * @param bool $filter
94
+ * @param bool $filter exclude empty value
95
+ * @param bool $useJsonMap use json field map
95
96
*
96
97
* @return array
97
98
*/
98
- public function toMap (bool $ filter = false ): array
99
+ public function toMap (bool $ filter = false , bool $ useJsonMap = false ): array
99
100
{
100
- return $ this ->convToMap ($ filter );
101
+ return $ this ->convToMap ($ filter, $ useJsonMap );
101
102
}
102
103
103
104
/**
@@ -107,7 +108,7 @@ public function toMap(bool $filter = false): array
107
108
*/
108
109
public function toCleaned (): array
109
110
{
110
- return $ this ->convToMap (true );
111
+ return $ this ->convToMap (true , true );
111
112
}
112
113
113
114
/**
@@ -135,8 +136,8 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
135
136
{
136
137
$ data = [];
137
138
$ full = get_object_vars ($ this );
138
- if ($ aliased && $ this ->_aliasMap ) {
139
- $ this ->_name2alias = array_flip ($ this ->_aliasMap );
139
+ if ($ aliased && $ this ->_jsonMap ) {
140
+ $ this ->_name2alias = array_flip ($ this ->_jsonMap );
140
141
}
141
142
142
143
// filter empty value
@@ -211,11 +212,11 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
211
212
* 转成 JSON 字符串
212
213
*
213
214
* @param bool $filter filter empty value
214
- * @param bool $aliased
215
+ * @param bool $aliased use json field map
215
216
*
216
217
* @return string
217
218
*/
218
- public function toJson (bool $ filter = true , bool $ aliased = false ): string
219
+ public function toJson (bool $ filter = true , bool $ aliased = true ): string
219
220
{
220
221
return Json::unescaped ($ this ->convToMap ($ filter , $ aliased ));
221
222
}
@@ -233,7 +234,7 @@ public function __toString(): string
233
234
*/
234
235
public function jsonSerialize (): array
235
236
{
236
- return $ this ->toMap ( true );
237
+ return $ this ->convToMap ( true , true );
237
238
}
238
239
239
240
}
0 commit comments