Skip to content

Commit

Permalink
feat: Introduce Event::fromArray to create assignment event from array (
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc authored Sep 10, 2024
1 parent b39a1a1 commit 6ba0618
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 12 deletions.
80 changes: 68 additions & 12 deletions src/Amplitude/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,98 @@

namespace AmplitudeExperiment\Amplitude;

use InvalidArgumentException;
use RuntimeException;

/**
* @phpstan-type Payload array{
* event_type: string,
* event_properties?: array<mixed>|null,
* user_properties?: array<mixed>|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<mixed>
* @var array<mixed>|null
*/
public ?array $eventProperties = null;
public ?array $eventProperties;
/**
* @var ?array<mixed>
* @var array<mixed>|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<mixed>|null $eventProperties
* @param array<mixed>|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<string, mixed>
* @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,
'user_properties' => $this->userProperties,
'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
);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Amplitude/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AmplitudeExperiment\Test\Amplitude;

use AmplitudeExperiment\Amplitude\Event;
use PHPUnit\Framework\TestCase;

final class EventTest extends TestCase
{
public function testToAndFromArray(): void
{
$event = new Event(
'eventType',
['eventProperty' => '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())
);
}
}

0 comments on commit 6ba0618

Please sign in to comment.