File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -154,6 +154,28 @@ public function getRequiredParams(): array
154154 return $ return ;
155155 }
156156
157+ public function toArray (): array
158+ {
159+ $ res = parent ::toArray ();
160+
161+ if (!isset ($ res ['item_category ' ])) {
162+ return $ res ;
163+ }
164+
165+ $ categories = $ res ['item_category ' ];
166+ unset($ res ['item_category ' ]);
167+
168+ if (is_array ($ categories )) {
169+ foreach ($ categories as $ k => $ val ) {
170+ $ tag = 'item_category ' . ($ k > 0 ? $ k + 1 : '' );
171+
172+ $ res [$ tag ] = $ val ;
173+ }
174+ }
175+
176+ return $ res ;
177+ }
178+
157179 public static function new (): static
158180 {
159181 return new static ();
Original file line number Diff line number Diff line change @@ -118,4 +118,21 @@ public function test_can_import_from_array()
118118 $ this ->assertArrayHasKey ('price ' , $ arr );
119119 $ this ->assertArrayHasKey ('quantity ' , $ arr );
120120 }
121+
122+ public function test_can_convert_item_category_to_index_names ()
123+ {
124+ $ arr = Item::new ()
125+ ->setItemId ("123 " )
126+ ->addItemCategory ("a " )
127+ ->addItemCategory ("b " )
128+ ->addItemCategory ("c " )
129+ ->toArray ();
130+
131+ $ this ->assertArrayHasKey ('item_category ' , $ arr );
132+ $ this ->assertArrayHasKey ('item_category2 ' , $ arr );
133+ $ this ->assertArrayHasKey ('item_category3 ' , $ arr );
134+
135+ $ this ->assertArrayNotHasKey ('item_category0 ' , $ arr );
136+ $ this ->assertArrayNotHasKey ('item_category1 ' , $ arr );
137+ }
121138}
You can’t perform that action at this time.
0 commit comments