This repository was archived by the owner on Mar 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,22 @@ trait LoopFunctions
1010{
1111 use HelpsLoopFunctions;
1212
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+
1329 /**
1430 * Maps your model attributes to local class properties.
1531 *
Original file line number Diff line number Diff line change @@ -101,4 +101,19 @@ public function testCollectionAssignmentIsOk()
101101 $ this ->assertArrayHasKey ('test ' , $ this ->supportCollection ->toArray ());
102102 $ this ->assertTrue ($ this ->supportCollection ->toArray ()['test ' ]);
103103 }
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+ }
104119}
Original file line number Diff line number Diff line change @@ -121,4 +121,21 @@ public function testSetsDefaultValueWithWrongConfig()
121121 $ this ->assertFalse ((new \ReflectionProperty ($ this , 'id ' ))->isInitialized ($ this ));
122122 $ this ->assertFalse ((new \ReflectionProperty ($ this , 'password ' ))->isInitialized ($ this ));
123123 }
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+ }
124141}
You can’t perform that action at this time.
0 commit comments