Skip to content

Commit 6ba0618

Browse files
authored
feat: Introduce Event::fromArray to create assignment event from array (#27)
1 parent b39a1a1 commit 6ba0618

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

src/Amplitude/Event.php

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,98 @@
22

33
namespace AmplitudeExperiment\Amplitude;
44

5+
use InvalidArgumentException;
56
use RuntimeException;
67

8+
/**
9+
* @phpstan-type Payload array{
10+
* event_type: string,
11+
* event_properties?: array<mixed>|null,
12+
* user_properties?: array<mixed>|null,
13+
* user_id?: string|null,
14+
* device_id?: string|null,
15+
* insert_id?: string|null,
16+
* time?: int|null
17+
* }
18+
*/
719
class Event
820
{
9-
public ?string $eventType = null;
21+
public string $eventType;
1022
/**
11-
* @var ?array<mixed>
23+
* @var array<mixed>|null
1224
*/
13-
public ?array $eventProperties = null;
25+
public ?array $eventProperties;
1426
/**
15-
* @var ?array<mixed>
27+
* @var array<mixed>|null
1628
*/
17-
public ?array $userProperties = null;
18-
public ?string $userId = null;
19-
public ?string $deviceId = null;
20-
public ?string $insertId = null;
21-
public ?int $time = null;
29+
public ?array $userProperties;
30+
public ?string $userId;
31+
public ?string $deviceId;
32+
public ?string $insertId;
33+
public ?int $time;
2234

23-
public function __construct(string $eventType)
35+
/**
36+
* @param array<mixed>|null $eventProperties
37+
* @param array<mixed>|null $userProperties
38+
*/
39+
public function __construct(
40+
string $eventType,
41+
?array $eventProperties = null,
42+
?array $userProperties = null,
43+
?string $userId = null,
44+
?string $deviceId = null,
45+
?string $insertId = null,
46+
?int $time = null
47+
)
2448
{
49+
if (empty($eventType)) {
50+
throw new InvalidArgumentException('Event type cannot be an empty string.');
51+
}
52+
2553
$this->eventType = $eventType;
54+
$this->eventProperties = $eventProperties;
55+
$this->userProperties = $userProperties;
56+
$this->userId = $userId;
57+
$this->deviceId = $deviceId;
58+
$this->insertId = $insertId;
59+
$this->time = $time;
2660
}
2761

2862
/**
29-
* @return array<string, mixed>
63+
* @return Payload
64+
* @throws InvalidArgumentException
3065
*/
3166
public function toArray(): array
3267
{
68+
if (empty($this->eventType)) {
69+
throw new InvalidArgumentException('Event type cannot be an empty string.');
70+
}
71+
3372
return array_filter([
3473
'event_type' => $this->eventType,
3574
'event_properties' => $this->eventProperties,
3675
'user_properties' => $this->userProperties,
3776
'user_id' => $this->userId,
3877
'device_id' => $this->deviceId,
3978
'insert_id' => $this->insertId,
40-
'time' => $this->time]);
79+
'time' => $this->time
80+
]);
81+
}
82+
83+
/**
84+
* @param Payload $data
85+
*/
86+
public static function fromArray(array $data): self
87+
{
88+
return new self(
89+
$data['event_type'],
90+
$data['event_properties'] ?? null,
91+
$data['user_properties'] ?? null,
92+
$data['user_id'] ?? null,
93+
$data['device_id'] ?? null,
94+
$data['insert_id'] ?? null,
95+
$data['time'] ?? null
96+
);
4197
}
4298

4399
/**

tests/Amplitude/EventTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AmplitudeExperiment\Test\Amplitude;
6+
7+
use AmplitudeExperiment\Amplitude\Event;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class EventTest extends TestCase
11+
{
12+
public function testToAndFromArray(): void
13+
{
14+
$event = new Event(
15+
'eventType',
16+
['eventProperty' => 'eventValue'],
17+
['userProperty' => 'userValue'],
18+
'userId',
19+
'deviceId',
20+
'insertId',
21+
1234567890
22+
);
23+
24+
$this->assertEquals(
25+
[
26+
'event_type' => 'eventType',
27+
'event_properties' => ['eventProperty' => 'eventValue'],
28+
'user_properties' => ['userProperty' => 'userValue'],
29+
'user_id' => 'userId',
30+
'device_id' => 'deviceId',
31+
'insert_id' => 'insertId',
32+
'time' => 1234567890
33+
],
34+
$event->toArray()
35+
);
36+
37+
$this->assertEquals(
38+
$event,
39+
Event::fromArray($event->toArray())
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)