@@ -19,14 +19,8 @@ trait WithLoopFunctions
1919 public function attributesToProperties (?Model $ model = null , mixed $ rescue = null ): void
2020 {
2121 if (! is_null ($ model )) {
22- $ toIgnore = config ('loop-functions.ignore_attributes ' );
23-
24- $ ignores = is_array ($ toIgnore )
25- ? $ toIgnore
26- : ['id ' , 'password ' ];
27-
2822 collect ($ model ->getAttributes ())
29- ->except ($ ignores )
23+ ->except ($ this -> ignoreKeys () )
3024 ->each (function ($ value , $ property ) use ($ model , $ rescue ) {
3125 if (property_exists ($ this , $ property )) {
3226 rescue (
@@ -49,14 +43,48 @@ public function attributesToProperties(?Model $model = null, mixed $rescue = nul
4943 */
5044 public function arrayToProperties (?array $ data , mixed $ rescue = null ): void
5145 {
52- collect ($ data ?? [])->each (function ($ value , $ key ) use ($ rescue ) {
53- if (property_exists ($ this , $ key )) {
54- rescue (
55- fn () => $ this ->{$ key } = $ value ,
56- $ rescue ,
57- config ('loop-functions.log ' ) ?? false
58- );
59- }
60- });
46+ collect ($ data ?? [])
47+ ->except ($ this ->ignoreKeys ())
48+ ->each (function ($ value , $ key ) use ($ rescue ) {
49+ if (is_array ($ key )) {
50+ $ this ->arrayToProperties ($ key , $ rescue );
51+ } else {
52+ $ this ->assignValue ($ key , $ value , $ rescue );
53+ }
54+ });
55+ }
56+
57+ /**
58+ * Assign the value to the property or rescue.
59+ *
60+ * @param string $key
61+ * @param mixed $value
62+ * @param mixed|null $rescue
63+ *
64+ * @return void
65+ */
66+ private function assignValue (string $ key , mixed $ value , mixed $ rescue = null ): void
67+ {
68+ if (property_exists ($ this , $ key )) {
69+ rescue (
70+ fn () => $ this ->{$ key } = $ value ,
71+ $ rescue ,
72+ config ('loop-functions.log ' ) ?? false
73+ );
74+ }
75+ }
76+
77+ /**
78+ * @return array
79+ */
80+ private function ignoreKeys (): array
81+ {
82+ $ defaults = ['id ' , 'password ' ];
83+
84+ $ ignores = config ('loop-functions.ignore_keys ' , $ defaults );
85+
86+ return is_array ($ ignores )
87+ ? $ ignores
88+ : $ defaults ;
6189 }
6290}
0 commit comments