This repository was archived by the owner on Mar 12, 2024. It is now read-only.
File tree 3 files changed +48
-0
lines changed
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,22 @@ trait LoopFunctions
10
10
{
11
11
use HelpsLoopFunctions;
12
12
13
+ /**
14
+ * Choose proper strategy to loop over the data.
15
+ *
16
+ * @param Model|array|null $data
17
+ * @param mixed|null $rescue
18
+ *
19
+ * @return void
20
+ */
21
+ public function propertiesFrom (Model |array |null $ data = null , mixed $ rescue = null ): void
22
+ {
23
+ match (true ) {
24
+ is_array ($ data ) => $ this ->arrayToProperties ($ data , $ rescue ),
25
+ $ data instanceof Model => $ this ->attributesToProperties ($ data , $ rescue ),
26
+ };
27
+ }
28
+
13
29
/**
14
30
* Maps your model attributes to local class properties.
15
31
*
Original file line number Diff line number Diff line change @@ -101,4 +101,19 @@ public function testCollectionAssignmentIsOk()
101
101
$ this ->assertArrayHasKey ('test ' , $ this ->supportCollection ->toArray ());
102
102
$ this ->assertTrue ($ this ->supportCollection ->toArray ()['test ' ]);
103
103
}
104
+
105
+ /** @test */
106
+ public function testCanMapUsingPropertiesFrom ()
107
+ {
108
+ $ array = [
109
+ 'test ' => true ,
110
+ 'additional_data ' => [
111
+ 'next ' => 'test ' ,
112
+ ],
113
+ ];
114
+
115
+ $ this ->propertiesFrom ($ array );
116
+
117
+ $ this ->assertArrayHasKey ('next ' , $ this ->additional_data );
118
+ }
104
119
}
Original file line number Diff line number Diff line change @@ -121,4 +121,21 @@ public function testSetsDefaultValueWithWrongConfig()
121
121
$ this ->assertFalse ((new \ReflectionProperty ($ this , 'id ' ))->isInitialized ($ this ));
122
122
$ this ->assertFalse ((new \ReflectionProperty ($ this , 'password ' ))->isInitialized ($ this ));
123
123
}
124
+
125
+ /** @test */
126
+ public function testCanMapUsingPropertiesFrom ()
127
+ {
128
+ $ model = new TestModel ([
129
+ 'collection ' => [
130
+ 0 => true ,
131
+ 1 => false ,
132
+ ],
133
+ 'intAsString ' => 100 ,
134
+ ]);
135
+
136
+ $ this ->propertiesFrom ($ model );
137
+
138
+ $ this ->assertInstanceOf (Collection::class, $ this ->collection );
139
+ $ this ->assertIsString ($ this ->intAsString );
140
+ }
124
141
}
You can’t perform that action at this time.
0 commit comments