|
2 | 2 |
|
3 | 3 | namespace AmplitudeExperiment\Amplitude; |
4 | 4 |
|
| 5 | +use InvalidArgumentException; |
5 | 6 | use RuntimeException; |
6 | 7 |
|
| 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 | + */ |
7 | 19 | class Event |
8 | 20 | { |
9 | | - public ?string $eventType = null; |
| 21 | + public string $eventType; |
10 | 22 | /** |
11 | | - * @var ?array<mixed> |
| 23 | + * @var array<mixed>|null |
12 | 24 | */ |
13 | | - public ?array $eventProperties = null; |
| 25 | + public ?array $eventProperties; |
14 | 26 | /** |
15 | | - * @var ?array<mixed> |
| 27 | + * @var array<mixed>|null |
16 | 28 | */ |
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; |
22 | 34 |
|
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 | + ) |
24 | 48 | { |
| 49 | + if (empty($eventType)) { |
| 50 | + throw new InvalidArgumentException('Event type cannot be an empty string.'); |
| 51 | + } |
| 52 | + |
25 | 53 | $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; |
26 | 60 | } |
27 | 61 |
|
28 | 62 | /** |
29 | | - * @return array<string, mixed> |
| 63 | + * @return Payload |
| 64 | + * @throws InvalidArgumentException |
30 | 65 | */ |
31 | 66 | public function toArray(): array |
32 | 67 | { |
| 68 | + if (empty($this->eventType)) { |
| 69 | + throw new InvalidArgumentException('Event type cannot be an empty string.'); |
| 70 | + } |
| 71 | + |
33 | 72 | return array_filter([ |
34 | 73 | 'event_type' => $this->eventType, |
35 | 74 | 'event_properties' => $this->eventProperties, |
36 | 75 | 'user_properties' => $this->userProperties, |
37 | 76 | 'user_id' => $this->userId, |
38 | 77 | 'device_id' => $this->deviceId, |
39 | 78 | '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 | + ); |
41 | 97 | } |
42 | 98 |
|
43 | 99 | /** |
|
0 commit comments