@@ -24,11 +24,11 @@ abstract class BaseObject implements JsonSerializable
2424 use QuickInitTrait;
2525
2626 /**
27- * Field alias mapping. [alias => field]
27+ * JSON field mapping. Format: [json-field => field]
2828 *
2929 * @var array
3030 */
31- protected array $ _aliasMap = [];
31+ protected array $ _jsonMap = [];
3232
3333 /**
3434 * Class constructor.
@@ -58,12 +58,12 @@ protected function init(): void
5858 * - Automatically convert fields to camelCase format
5959 *
6060 * @param array $data
61- * @param array $aliasMap
61+ * @param array $jsonMap json field mapping
6262 */
63- public function load (array $ data , array $ aliasMap = []): void
63+ public function load (array $ data , array $ jsonMap = []): void
6464 {
6565 if ($ data ) {
66- Obj::init ($ this , $ data , true , $ aliasMap ?: $ this ->_aliasMap );
66+ Obj::init ($ this , $ data , true , $ jsonMap ?: $ this ->_jsonMap );
6767 }
6868 }
6969
@@ -76,7 +76,7 @@ public function load(array $data, array $aliasMap = []): void
7676 */
7777 public function setValue (string $ field , mixed $ value ): static
7878 {
79- Obj::init ($ this , [$ field => $ value ], true , $ this ->_aliasMap );
79+ Obj::init ($ this , [$ field => $ value ], true , $ this ->_jsonMap );
8080 return $ this ;
8181 }
8282
@@ -85,19 +85,20 @@ public function setValue(string $field, mixed $value): static
8585 */
8686 public function toArray (): array
8787 {
88- return Obj:: toArray ( $ this , false , false );
88+ return $ this -> convToMap ( true , true );
8989 }
9090
9191 /**
9292 * Convert to array map.
9393 *
94- * @param bool $filter
94+ * @param bool $filter exclude empty value
95+ * @param bool $useJsonMap use json field map
9596 *
9697 * @return array
9798 */
98- public function toMap (bool $ filter = false ): array
99+ public function toMap (bool $ filter = false , bool $ useJsonMap = false ): array
99100 {
100- return $ this ->convToMap ($ filter );
101+ return $ this ->convToMap ($ filter, $ useJsonMap );
101102 }
102103
103104 /**
@@ -107,7 +108,7 @@ public function toMap(bool $filter = false): array
107108 */
108109 public function toCleaned (): array
109110 {
110- return $ this ->convToMap (true );
111+ return $ this ->convToMap (true , true );
111112 }
112113
113114 /**
@@ -135,8 +136,8 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
135136 {
136137 $ data = [];
137138 $ 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 );
140141 }
141142
142143 // filter empty value
@@ -211,11 +212,11 @@ protected function convToMap(bool $filter = false, bool $aliased = false): array
211212 * 转成 JSON 字符串
212213 *
213214 * @param bool $filter filter empty value
214- * @param bool $aliased
215+ * @param bool $aliased use json field map
215216 *
216217 * @return string
217218 */
218- public function toJson (bool $ filter = true , bool $ aliased = false ): string
219+ public function toJson (bool $ filter = true , bool $ aliased = true ): string
219220 {
220221 return Json::unescaped ($ this ->convToMap ($ filter , $ aliased ));
221222 }
@@ -233,7 +234,7 @@ public function __toString(): string
233234 */
234235 public function jsonSerialize (): array
235236 {
236- return $ this ->toMap ( true );
237+ return $ this ->convToMap ( true , true );
237238 }
238239
239240}
0 commit comments