@@ -12,11 +12,11 @@ trait WithModelMapping
1212 * Maps your model attributes to local class properties.
1313 *
1414 * @param Model|null $model
15- * @param mixed $failure
15+ * @param mixed $rescue
1616 *
1717 * @return void
1818 */
19- public function mapModelAttributes (?Model $ model = null , mixed $ failure = null ): void
19+ public function mapModelAttributes (?Model $ model = null , mixed $ rescue = null ): void
2020 {
2121 if (! is_null ($ model )) {
2222 $ toIgnore = config ('model-mapper.ignore_attributes ' );
@@ -27,15 +27,36 @@ public function mapModelAttributes(?Model $model = null, mixed $failure = null):
2727
2828 collect ($ model ->getAttributes ())
2929 ->except ($ ignores )
30- ->each (function ($ value , $ property ) use ($ model , $ failure ) {
30+ ->each (function ($ value , $ property ) use ($ model , $ rescue ) {
3131 if (property_exists ($ this , $ property )) {
3232 rescue (
3333 fn () => $ this ->{$ property } = $ model ->{$ property },
34- $ failure ,
34+ $ rescue ,
3535 config ('model-mapper.log ' ) ?? false
3636 );
3737 }
3838 });
3939 }
4040 }
41+
42+ /**
43+ * Map array data to class properties.
44+ *
45+ * @param array $data
46+ * @param mixed|null $rescue
47+ *
48+ * @return void
49+ */
50+ public function arrayToProperties (array $ data , mixed $ rescue = null ): void
51+ {
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 ('model-mapper.log ' ) ?? false
58+ );
59+ }
60+ });
61+ }
4162}
0 commit comments