@@ -120,6 +120,18 @@ protected function getOptions(): array
120
120
return [];
121
121
}
122
122
123
+ /**
124
+ * 批量设置模型参数
125
+ * @param array $options 值
126
+ * @return void
127
+ */
128
+ protected function setOptions (array $ options )
129
+ {
130
+ foreach ($ options as $ name => $ value ) {
131
+ $ this ->setOption ($ name , $ value );
132
+ }
133
+ }
134
+
123
135
/**
124
136
* 设置模型参数
125
137
*
@@ -300,10 +312,10 @@ public function model()
300
312
protected function initializeData (array | object $ data , bool $ fromSave = false )
301
313
{
302
314
// 分析数据
303
- $ data = $ this ->parseData ($ data );
304
- // 获取字段列表
305
- $ schema = $ this -> getFields ( );
306
- $ fields = array_keys ( $ schema );
315
+ $ data = $ this ->parseData ($ data );
316
+ $ schema = $ this -> getFields ();
317
+ $ fields = array_keys ( $ schema );
318
+ $ mapping = $ this -> getOption ( ' mapping ' );
307
319
308
320
// 实体模型赋值
309
321
foreach ($ data as $ name => $ val ) {
@@ -312,9 +324,8 @@ protected function initializeData(array | object $data, bool $fromSave = false)
312
324
continue ;
313
325
}
314
326
315
- if (!empty (self ::$ weakMap [$ this ]['mapping ' ])) {
316
- // 字段映射
317
- $ name = array_search ($ name , self ::$ weakMap [$ this ]['mapping ' ]) ?: $ name ;
327
+ if (!empty ($ mapping )) {
328
+ $ name = array_search ($ name , $ mapping ) ?: $ name ;
318
329
}
319
330
320
331
if (str_contains ($ name , '__ ' )) {
@@ -431,8 +442,9 @@ public function withAttr(string $name, callable $callback)
431
442
{
432
443
$ name = $ this ->getRealFieldName ($ name );
433
444
434
- self ::$ weakMap [$ this ]['with_attr ' ][$ name ] = $ callback ;
435
- self ::$ weakMap [$ this ]['append ' ][] = $ name ;
445
+ $ this ->setWeakData ('with_attr ' , $ name , $ callback );
446
+ // 自动追加输出
447
+ self ::$ weakMap [$ this ]['append ' ][] = $ name ;
436
448
return $ this ;
437
449
}
438
450
@@ -1154,7 +1166,7 @@ public function getData(?string $name = null)
1154
1166
protected function setData (string $ name , $ value )
1155
1167
{
1156
1168
$ this ->setWeakData ('data ' , $ name , $ value );
1157
- if (isset ( self :: $ weakMap [ $ this ][ 'get ' ][ $ name] )) {
1169
+ if ($ this -> getWeakData ( 'get ' , $ name )) {
1158
1170
$ this ->setWeakData ('get ' , $ name , null );
1159
1171
}
1160
1172
}
@@ -1243,9 +1255,8 @@ public function toArray(array $allow = []): array
1243
1255
$ item [$ name ] = $ this ->getWithAttr ($ name , $ val , $ data );
1244
1256
}
1245
1257
1246
- if (isset ($ item [$ name ]) && isset ( self :: $ weakMap [ $ this ][ 'mapping ' ][ $ name] )) {
1258
+ if (isset ($ item [$ name ]) && $ key = $ this -> getWeakData ( 'mapping ' , $ name )) {
1247
1259
// 检查字段映射
1248
- $ key = self ::$ weakMap [$ this ]['mapping ' ][$ name ];
1249
1260
$ item [$ key ] = $ item [$ name ];
1250
1261
unset($ item [$ name ]);
1251
1262
}
@@ -1292,11 +1303,7 @@ public function isForce(): bool
1292
1303
*/
1293
1304
public function set (string $ name , $ value )
1294
1305
{
1295
- if (!empty (self ::$ weakMap [$ this ]['mapping ' ])) {
1296
- $ name = array_search ($ name , self ::$ weakMap [$ this ]['mapping ' ]) ?: $ name ;
1297
- }
1298
-
1299
- $ name = $ this ->getRealFieldName ($ name );
1306
+ $ name = $ this ->getMappingName ($ name );
1300
1307
$ type = $ this ->getFields ($ name );
1301
1308
1302
1309
if (is_null ($ value ) && is_subclass_of ($ type , Entity::class)) {
@@ -1363,14 +1370,10 @@ private function setWithAttr(string $name, $value, array $data = [])
1363
1370
*/
1364
1371
public function get (string $ name , bool $ attr = true )
1365
1372
{
1366
- if (!empty (self ::$ weakMap [$ this ]['mapping ' ])) {
1367
- $ name = array_search ($ name , self ::$ weakMap [$ this ]['mapping ' ]) ?: $ name ;
1368
- }
1369
-
1370
- $ name = $ this ->getRealFieldName ($ name );
1371
- if ($ attr && isset (self ::$ weakMap [$ this ]['get ' ][$ name ])) {
1373
+ $ name = $ this ->getMappingName ($ name );
1374
+ if ($ attr && $ value = $ this ->getWeakData ('get ' , $ name )) {
1372
1375
// 已经输出的数据直接返回
1373
- return self :: $ weakMap [ $ this ][ ' get ' ][ $ name ] ;
1376
+ return $ value ;
1374
1377
}
1375
1378
1376
1379
if (!array_key_exists ($ name , $ this ->getOption ('data ' ))) {
@@ -1389,6 +1392,22 @@ public function get(string $name, bool $attr = true)
1389
1392
return $ value ;
1390
1393
}
1391
1394
1395
+ /**
1396
+ * 获取映射字段
1397
+ *
1398
+ * @param string $name 名称
1399
+ *
1400
+ * @return string
1401
+ */
1402
+ protected function getMappingName (string $ name ): string
1403
+ {
1404
+ $ mapping = $ this ->getOption ('mapping ' );
1405
+ if (!empty ($ mapping )) {
1406
+ $ name = array_search ($ name , $ mapping ) ?: $ name ;
1407
+ }
1408
+ return $ this ->getRealFieldName ($ name );
1409
+ }
1410
+
1392
1411
/**
1393
1412
* 处理数据对象的值(经过获取器和类型转换)
1394
1413
*
@@ -1400,16 +1419,20 @@ public function get(string $name, bool $attr = true)
1400
1419
*/
1401
1420
private function getWithAttr (string $ name , $ value , array $ data = [])
1402
1421
{
1403
- $ attr = Str::studly ($ name );
1404
- $ method = 'get ' . $ attr . 'Attr ' ;
1405
- if (isset (self ::$ weakMap [$ this ]['with_attr ' ][$ name ])) {
1406
- $ callback = self ::$ weakMap [$ this ]['with_attr ' ][$ name ];
1407
- $ value = $ callback ($ value , $ data , $ this );
1422
+ $ attr = Str::studly ($ name );
1423
+ $ method = 'get ' . $ attr . 'Attr ' ;
1424
+ $ withAttr = $ this ->getWeakData ('with_attr ' , $ name );
1425
+ if ($ withAttr ) {
1426
+ // 动态获取器
1427
+ $ value = $ withAttr ($ value , $ data , $ this );
1408
1428
} elseif (method_exists ($ this , $ attr ) && $ get = $ this ->$ attr ()['get ' ] ?? '' ) {
1429
+ // 属性器
1409
1430
$ value = $ get ($ value , $ data );
1410
1431
} elseif (method_exists ($ this , $ method )) {
1432
+ // 获取器
1411
1433
$ value = $ this ->$ method ($ value , $ data );
1412
1434
} elseif ($ value instanceof Typeable || is_subclass_of ($ value , EnumTransform::class)) {
1435
+ // 类型自动转换
1413
1436
$ value = $ value ->value ();
1414
1437
}
1415
1438
return $ value ;
0 commit comments