-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from bwaidelich/feature/event-metadata
FEATURE: Event metadata and recorded at timestamp
- Loading branch information
Showing
11 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wwwision\DCBEventStore\Types; | ||
|
||
use InvalidArgumentException; | ||
use JsonException; | ||
use JsonSerializable; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Array-based metadata of an event | ||
*/ | ||
final class EventMetadata implements JsonSerializable | ||
{ | ||
/** | ||
* @param array<string, mixed> $value | ||
*/ | ||
private function __construct( | ||
public readonly array $value, | ||
) { | ||
Assert::isMap($value, 'EventMetadata must consist of an associative array with string keys'); | ||
} | ||
|
||
public static function none(): self | ||
{ | ||
return new self([]); | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $value | ||
*/ | ||
public static function fromArray(array $value): self | ||
{ | ||
return new self($value); | ||
} | ||
|
||
public static function fromJson(string $json): self | ||
{ | ||
try { | ||
$metadata = json_decode($json, true, 512, JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new InvalidArgumentException(sprintf('Failed to decode JSON to event metadata: %s', $e->getMessage()), 1692197194, $e); | ||
} | ||
Assert::isArray($metadata, 'Failed to decode JSON to event metadata'); | ||
return self::fromArray($metadata); | ||
} | ||
|
||
public function with(string $key, mixed $value): self | ||
{ | ||
return new self([...$this->value, $key => $value]); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Unit\Types; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Wwwision\DCBEventStore\Types\EventMetadata; | ||
|
||
#[CoversClass(EventMetadata::class)] | ||
final class EventMetadataTest extends TestCase | ||
{ | ||
|
||
public function test_none_creates_empty_instance(): void | ||
{ | ||
self::assertSame([], EventMetadata::none()->value); | ||
} | ||
|
||
public function test_fromArray_fails_if_array_is_not_associative(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
EventMetadata::fromArray(['foo', 'bar']); | ||
} | ||
|
||
public function test_fromJson_fails_if_value_is_no_valid_json(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
EventMetadata::fromJson('no json'); | ||
} | ||
|
||
public function test_fromJson_fails_if_value_is_no_json_object(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
EventMetadata::fromJson('"no array"'); | ||
} | ||
|
||
public function test_fromJson_fails_if_value_is_no_associative_json_object(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
EventMetadata::fromJson('["foo", "bar"]'); | ||
} | ||
|
||
public function test_with_sets_metadata_value(): void | ||
{ | ||
self::assertSame(['foo' => 'bar'], EventMetadata::none()->with('foo', 'bar')->value); | ||
} | ||
|
||
public function test_with_overrides_previously_set_value(): void | ||
{ | ||
self::assertSame(['foo' => 'replaced'], EventMetadata::none()->with('foo', 'bar')->with('foo', 'replaced')->value); | ||
} | ||
|
||
public function test_jsonSerializable(): void | ||
{ | ||
$actualResult = json_encode(EventMetadata::none()->with('foo', 'bar')->with('bar', 'baz')); | ||
self::assertJsonStringEqualsJsonString('{"foo": "bar", "bar": "baz"}', $actualResult); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters