Skip to content

Commit

Permalink
fix: Allow creation of assignment event from array
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Sep 9, 2024
1 parent 0aa016f commit 5faa58f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 19 deletions.
84 changes: 65 additions & 19 deletions src/Amplitude/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,86 @@

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
)
{
$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
*/
public function toArray(): array
{
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]);
return [
'event_type' => $this->eventType ?? '',
'event_properties' => $this->eventProperties ?? null,
'user_properties' => $this->userProperties ?? null,
'user_id' => $this->userId ?? null,
'device_id' => $this->deviceId ?? null,
'insert_id' => $this->insertId ?? null,
'time' => $this->time ?? null,
];
}

/**
* @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 5faa58f

Please sign in to comment.