@@ -120,6 +120,18 @@ protected function getOptions(): array
120120 return [];
121121 }
122122
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+
123135 /**
124136 * 设置模型参数
125137 *
@@ -300,10 +312,10 @@ public function model()
300312 protected function initializeData (array | object $ data , bool $ fromSave = false )
301313 {
302314 // 分析数据
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 ' );
307319
308320 // 实体模型赋值
309321 foreach ($ data as $ name => $ val ) {
@@ -312,9 +324,8 @@ protected function initializeData(array | object $data, bool $fromSave = false)
312324 continue ;
313325 }
314326
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 ;
318329 }
319330
320331 if (str_contains ($ name , '__ ' )) {
@@ -431,8 +442,9 @@ public function withAttr(string $name, callable $callback)
431442 {
432443 $ name = $ this ->getRealFieldName ($ name );
433444
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 ;
436448 return $ this ;
437449 }
438450
@@ -1154,7 +1166,7 @@ public function getData(?string $name = null)
11541166 protected function setData (string $ name , $ value )
11551167 {
11561168 $ this ->setWeakData ('data ' , $ name , $ value );
1157- if (isset ( self :: $ weakMap [ $ this ][ 'get ' ][ $ name] )) {
1169+ if ($ this -> getWeakData ( 'get ' , $ name )) {
11581170 $ this ->setWeakData ('get ' , $ name , null );
11591171 }
11601172 }
@@ -1243,9 +1255,8 @@ public function toArray(array $allow = []): array
12431255 $ item [$ name ] = $ this ->getWithAttr ($ name , $ val , $ data );
12441256 }
12451257
1246- if (isset ($ item [$ name ]) && isset ( self :: $ weakMap [ $ this ][ 'mapping ' ][ $ name] )) {
1258+ if (isset ($ item [$ name ]) && $ key = $ this -> getWeakData ( 'mapping ' , $ name )) {
12471259 // 检查字段映射
1248- $ key = self ::$ weakMap [$ this ]['mapping ' ][$ name ];
12491260 $ item [$ key ] = $ item [$ name ];
12501261 unset($ item [$ name ]);
12511262 }
@@ -1292,11 +1303,7 @@ public function isForce(): bool
12921303 */
12931304 public function set (string $ name , $ value )
12941305 {
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 );
13001307 $ type = $ this ->getFields ($ name );
13011308
13021309 if (is_null ($ value ) && is_subclass_of ($ type , Entity::class)) {
@@ -1363,14 +1370,10 @@ private function setWithAttr(string $name, $value, array $data = [])
13631370 */
13641371 public function get (string $ name , bool $ attr = true )
13651372 {
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 )) {
13721375 // 已经输出的数据直接返回
1373- return self :: $ weakMap [ $ this ][ ' get ' ][ $ name ] ;
1376+ return $ value ;
13741377 }
13751378
13761379 if (!array_key_exists ($ name , $ this ->getOption ('data ' ))) {
@@ -1389,6 +1392,22 @@ public function get(string $name, bool $attr = true)
13891392 return $ value ;
13901393 }
13911394
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+
13921411 /**
13931412 * 处理数据对象的值(经过获取器和类型转换)
13941413 *
@@ -1400,16 +1419,20 @@ public function get(string $name, bool $attr = true)
14001419 */
14011420 private function getWithAttr (string $ name , $ value , array $ data = [])
14021421 {
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 );
14081428 } elseif (method_exists ($ this , $ attr ) && $ get = $ this ->$ attr ()['get ' ] ?? '' ) {
1429+ // 属性器
14091430 $ value = $ get ($ value , $ data );
14101431 } elseif (method_exists ($ this , $ method )) {
1432+ // 获取器
14111433 $ value = $ this ->$ method ($ value , $ data );
14121434 } elseif ($ value instanceof Typeable || is_subclass_of ($ value , EnumTransform::class)) {
1435+ // 类型自动转换
14131436 $ value = $ value ->value ();
14141437 }
14151438 return $ value ;
0 commit comments