Skip to content

Commit 3fab3ba

Browse files
authored
Parse item-arrays as item-models when converting event-array to event-models (#48)
2 parents 986965f + f44967f commit 3fab3ba

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Helper/IOHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace AlexWestergaard\PhpGa4\Helper;
44

5+
use AlexWestergaard\PhpGa4\Item;
6+
use AlexWestergaard\PhpGa4\Facade\Type\ItemType;
57
use AlexWestergaard\PhpGa4\Facade\Type\IOType;
68
use AlexWestergaard\PhpGa4\Exception\Ga4IOException;
79

@@ -303,6 +305,10 @@ public static function fromArray(IOType|array $importable): static
303305
foreach ($importable as $key => $val) {
304306
if (is_array($val)) {
305307
foreach ($val as $single) {
308+
if (in_array($key, ['item', 'items']) && !($importable instanceof ItemType)) {
309+
$single = Item::fromArray($single);
310+
}
311+
306312
$static[$key] = $single;
307313
}
308314
} else {

test/Unit/HelperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ public function test_convert_helper_transforms_array_into_events()
2626
$this->assertCount(2, $this->analytics['events']);
2727
}
2828

29+
public function test_convert_helper_transforms_events_with_items()
30+
{
31+
$request = [
32+
[
33+
'AddToCart' => [
34+
'currency' => 'AUD',
35+
'value' => 38,
36+
'items' => [
37+
[
38+
'item_id' => 29,
39+
'item_name' => '500g Musk Scrolls',
40+
'price' => 38,
41+
'quantity' => 1,
42+
],
43+
],
44+
],
45+
],
46+
];
47+
48+
$list = Helper\ConvertHelper::parseEvents($request);
49+
50+
var_dump($request, $list);
51+
exit;
52+
}
53+
2954
public function test_snakecase_helper_transforms_camelcase_names()
3055
{
3156
$output = Helper\ConvertHelper::snake('snakeCase');

0 commit comments

Comments
 (0)