From 6ba0618f60a44c324cbec9a47a416d80f7cebb8e Mon Sep 17 00:00:00 2001 From: Tim Yiu <137842098+tyiuhc@users.noreply.github.com> Date: Tue, 10 Sep 2024 00:32:27 -0700 Subject: [PATCH] feat: Introduce Event::fromArray to create assignment event from array (#27) --- src/Amplitude/Event.php | 80 +++++++++++++++++++++++++++++------ tests/Amplitude/EventTest.php | 42 ++++++++++++++++++ 2 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 tests/Amplitude/EventTest.php diff --git a/src/Amplitude/Event.php b/src/Amplitude/Event.php index 7571969..e33972a 100644 --- a/src/Amplitude/Event.php +++ b/src/Amplitude/Event.php @@ -2,34 +2,73 @@ namespace AmplitudeExperiment\Amplitude; +use InvalidArgumentException; use RuntimeException; +/** + * @phpstan-type Payload array{ + * event_type: string, + * event_properties?: array|null, + * user_properties?: array|null, + * user_id?: string|null, + * device_id?: string|null, + * insert_id?: string|null, + * time?: int|null + * } + */ class Event { - public ?string $eventType = null; + public string $eventType; /** - * @var ?array + * @var array|null */ - public ?array $eventProperties = null; + public ?array $eventProperties; /** - * @var ?array + * @var array|null */ - public ?array $userProperties = null; - public ?string $userId = null; - public ?string $deviceId = null; - public ?string $insertId = null; - public ?int $time = null; + public ?array $userProperties; + public ?string $userId; + public ?string $deviceId; + public ?string $insertId; + public ?int $time; - public function __construct(string $eventType) + /** + * @param array|null $eventProperties + * @param array|null $userProperties + */ + public function __construct( + string $eventType, + ?array $eventProperties = null, + ?array $userProperties = null, + ?string $userId = null, + ?string $deviceId = null, + ?string $insertId = null, + ?int $time = null + ) { + if (empty($eventType)) { + throw new InvalidArgumentException('Event type cannot be an empty string.'); + } + $this->eventType = $eventType; + $this->eventProperties = $eventProperties; + $this->userProperties = $userProperties; + $this->userId = $userId; + $this->deviceId = $deviceId; + $this->insertId = $insertId; + $this->time = $time; } /** - * @return array + * @return Payload + * @throws InvalidArgumentException */ public function toArray(): array { + if (empty($this->eventType)) { + throw new InvalidArgumentException('Event type cannot be an empty string.'); + } + return array_filter([ 'event_type' => $this->eventType, 'event_properties' => $this->eventProperties, @@ -37,7 +76,24 @@ public function toArray(): array 'user_id' => $this->userId, 'device_id' => $this->deviceId, 'insert_id' => $this->insertId, - 'time' => $this->time]); + 'time' => $this->time + ]); + } + + /** + * @param Payload $data + */ + public static function fromArray(array $data): self + { + return new self( + $data['event_type'], + $data['event_properties'] ?? null, + $data['user_properties'] ?? null, + $data['user_id'] ?? null, + $data['device_id'] ?? null, + $data['insert_id'] ?? null, + $data['time'] ?? null + ); } /** diff --git a/tests/Amplitude/EventTest.php b/tests/Amplitude/EventTest.php new file mode 100644 index 0000000..a20455a --- /dev/null +++ b/tests/Amplitude/EventTest.php @@ -0,0 +1,42 @@ + 'eventValue'], + ['userProperty' => 'userValue'], + 'userId', + 'deviceId', + 'insertId', + 1234567890 + ); + + $this->assertEquals( + [ + 'event_type' => 'eventType', + 'event_properties' => ['eventProperty' => 'eventValue'], + 'user_properties' => ['userProperty' => 'userValue'], + 'user_id' => 'userId', + 'device_id' => 'deviceId', + 'insert_id' => 'insertId', + 'time' => 1234567890 + ], + $event->toArray() + ); + + $this->assertEquals( + $event, + Event::fromArray($event->toArray()) + ); + } +}