Skip to content

Commit a97aa9c

Browse files
authored
Add static fromArray() method to Events and Items (#22)
2 parents 43a205d + b978150 commit a97aa9c

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

src/Item.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace AlexWestergaard\PhpGa4;
44

5-
use AlexWestergaard\PhpGa4\Facade;
65
use AlexWestergaard\PhpGa4\Model;
6+
use AlexWestergaard\PhpGa4\Facade;
77

88
/**
99
* @requires One of item_id or item_name must be present and valid
@@ -168,6 +168,29 @@ public function getRequiredParams(): array
168168
return $return;
169169
}
170170

171+
public static function fromArray(array $params = [])
172+
{
173+
$item = static::new();
174+
175+
$insertables = array_unique(array_merge($item->getParams(), $item->getRequiredParams()));
176+
177+
foreach ($insertables as $insertable) {
178+
if (!in_array($insertable, array_keys($params)) || is_null($param = $params[$insertable])) {
179+
continue;
180+
}
181+
182+
$callableName = implode('', array_map('ucfirst', explode('_', $insertable)));
183+
184+
if (method_exists($item, ($method = 'add' . $callableName))) {
185+
$item->$method($param);
186+
} elseif (method_exists($item, ($method = 'set' . $callableName))) {
187+
$item->$method($param);
188+
}
189+
}
190+
191+
return $item;
192+
}
193+
171194
/**
172195
* @param GA4Exception $childErrors
173196
*/

src/Model/Event.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace AlexWestergaard\PhpGa4\Model;
44

5-
use AlexWestergaard\PhpGa4\GA4Exception;
65
use AlexWestergaard\PhpGa4\Facade;
6+
use AlexWestergaard\PhpGa4\GA4Exception;
77

88
abstract class Event extends ToArray implements Facade\Export
99
{
@@ -14,6 +14,40 @@ abstract class Event extends ToArray implements Facade\Export
1414
*/
1515
abstract public function getName(): string;
1616

17+
public static function fromArray(array $params = [])
18+
{
19+
$event = static::new();
20+
21+
$insertables = array_unique(array_merge($event->getParams(), $event->getRequiredParams()));
22+
23+
foreach ($insertables as $insertable) {
24+
if (!in_array($insertable, array_keys($params)) || is_null($param = $params[$insertable])) {
25+
continue;
26+
}
27+
28+
$callableName = implode('', array_map('ucfirst', explode('_', $insertable)));
29+
30+
if (is_array($param)) {
31+
$callableName = substr($callableName, -1) === 's' ? substr($callableName, 0, -1) : $callableName;
32+
var_dump($callableName);
33+
foreach ($param as $paramRow) {
34+
if (method_exists($event, ($method = 'add' . $callableName))) {
35+
$event->$method($paramRow);
36+
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
37+
$event->$method($paramRow);
38+
}
39+
}
40+
} else {
41+
if (method_exists($event, ($method = 'add' . $callableName))) {
42+
$event->$method($param);
43+
} elseif (method_exists($event, ($method = 'set' . $callableName))) {
44+
$event->$method($param);
45+
}
46+
}
47+
}
48+
return $event;
49+
}
50+
1751
/**
1852
* @param GA4Exception $childErrors
1953
*/

test/AnalyticsTest.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
use AlexWestergaard\PhpGa4\Analytics;
4-
use AlexWestergaard\PhpGa4\Event\Refund;
53
use AlexWestergaard\PhpGa4\Item;
6-
use AlexWestergaard\PhpGa4\UserProperty;
4+
use AlexWestergaard\PhpGa4\Event;
5+
use AlexWestergaard\PhpGa4\Analytics;
76
use AlexWestergaard\PhpGa4\GA4Exception;
7+
use AlexWestergaard\PhpGa4\UserProperty;
88

99
class AnalyticsTest extends \PHPUnit\Framework\TestCase
1010
{
@@ -83,6 +83,29 @@ public function testItem()
8383
$this->assertArrayHasKey('quantity', $arr);
8484
}
8585

86+
public function testItemFromArray()
87+
{
88+
$this->prepareSituation();
89+
90+
$item = Item::fromArray([
91+
'item_id' => '2',
92+
'item_name' => 'Second Product',
93+
'currency' => $this->prefill['currency'],
94+
'price' => 9.99,
95+
'quantity' => 4,
96+
]);
97+
98+
$this->assertInstanceOf(Item::class, $item);
99+
100+
$arr = $item->toArray();
101+
$this->assertTrue(is_array($arr));
102+
$this->assertArrayHasKey('item_id', $arr);
103+
$this->assertArrayHasKey('item_name', $arr);
104+
$this->assertArrayHasKey('currency', $arr);
105+
$this->assertArrayHasKey('price', $arr);
106+
$this->assertArrayHasKey('quantity', $arr);
107+
}
108+
86109
public function testUserProperty()
87110
{
88111
$this->prepareSituation();
@@ -109,7 +132,7 @@ public function testFullRefundNoItems()
109132
{
110133
$this->prepareSituation();
111134

112-
$refund = Refund::new()->setTransactionId(1)->isFullRefund(true);
135+
$refund = Event\Refund::new()->setTransactionId(1)->isFullRefund(true);
113136

114137
$this->analytics->addEvent($refund);
115138

@@ -120,7 +143,7 @@ public function testPartialRefundWithItems()
120143
{
121144
$this->prepareSituation();
122145

123-
$refund = Refund::new()->setTransactionId(1)->addItem($this->item);
146+
$refund = Event\Refund::new()->setTransactionId(1)->addItem($this->item);
124147

125148
$this->analytics->addEvent($refund);
126149

@@ -137,7 +160,7 @@ public function testPartialRefundNoItemsThrows()
137160
{
138161
$this->prepareSituation();
139162

140-
$refund = Refund::new()->setTransactionId(1);
163+
$refund = Event\Refund::new()->setTransactionId(1);
141164

142165
$this->expectException(GA4Exception::class);
143166

@@ -243,4 +266,20 @@ public function testPrebuildEvents()
243266

244267
$this->assertTrue($this->analytics->post());
245268
}
269+
270+
public function testBuildEventFromArray()
271+
{
272+
$this->prepareSituation();
273+
274+
$event = Event\AddToCart::fromArray([
275+
'currency' => $this->prefill['currency'],
276+
'value' => rand(1000, 10000) / 100,
277+
'items' => [$this->item],
278+
]);
279+
280+
$this->assertTrue(is_array($event->toArray()), get_class($event));
281+
282+
$this->analytics->addEvent($event);
283+
$this->assertTrue($this->analytics->post());
284+
}
246285
}

0 commit comments

Comments
 (0)