Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce Event::fromArray to create of assignment event from array #27

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
);
}
}
Loading