Skip to content

Commit

Permalink
add more normalizer and update getting started docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 29, 2023
1 parent 293c694 commit f49fda1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
18 changes: 10 additions & 8 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ First we define the events that happen in our system.
A hotel can be created with a `name` and a `id`:

```php
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;
use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId;
use Patchlevel\EventSourcing\Serializer\Normalizer\UuidAggregateIdNormalizer;

#[Event('hotel.created')]
final class HotelCreated
{
public function __construct(
public readonly BasicAggregateRootId $hotelId,
#[UuidAggregateIdNormalizer]
public readonly UuidAggregateRootId $hotelId,
public readonly string $hotelName
) {
}
Expand Down Expand Up @@ -62,7 +64,7 @@ These events are thrown here and the state of the hotel is also changed.
```php
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;
use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\AggregateId;
use Patchlevel\EventSourcing\Attribute\Apply;
Expand All @@ -71,7 +73,7 @@ use Patchlevel\EventSourcing\Attribute\Apply;
final class Hotel extends BasicAggregateRoot
{
#[AggregateId]
private BasicAggregateRootId $id;
private UuidAggregateRootId $id;
private string $name;

/**
Expand All @@ -89,7 +91,7 @@ final class Hotel extends BasicAggregateRoot
return $this->guests;
}

public static function create(BasicAggregateRootId $id, string $hotelName): static
public static function create(UuidAggregateRootId $id, string $hotelName): static
{
$self = new static();
$self->recordThat(new HotelCreated($id, $hotelName));
Expand Down Expand Up @@ -356,16 +358,16 @@ $projectionist->boot();
We are now ready to use the Event Sourcing System. We can load, change and save aggregates.

```php
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;
use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId;

$hotel1 = Hotel::create(new BasicAggregateRootId('1'), 'HOTEL');
$hotel1 = Hotel::create(UuidAggregateRootId::generate(), 'HOTEL');
$hotel1->checkIn('David');
$hotel1->checkIn('Daniel');
$hotel1->checkOut('David');

$hotelRepository->save($hotel1);

$hotel2 = $hotelRepository->load(new BasicAggregateRootId('2'));
$hotel2 = $hotelRepository->load(UuidAggregateRootId::fromString('d0d0d0d0-d0d0-d0d0-d0d0-d0d0d0d0d0d0'));
$hotel2->checkIn('David');
$hotelRepository->save($hotel2);

Expand Down
17 changes: 17 additions & 0 deletions src/Serializer/Normalizer/BasicAggregateIdNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Normalizer;

use Attribute;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class BasicAggregateIdNormalizer extends AggregateIdNormalizer
{
public function __construct()
{
parent::__construct(BasicAggregateRootId::class);
}
}
17 changes: 17 additions & 0 deletions src/Serializer/Normalizer/UuidAggregateIdNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Serializer\Normalizer;

use Attribute;
use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class UuidAggregateIdNormalizer extends AggregateIdNormalizer
{
public function __construct()
{
parent::__construct(UuidAggregateRootId::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,14 @@
namespace Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\Normalizer;

use Attribute;
use InvalidArgumentException;
use Patchlevel\EventSourcing\Serializer\Normalizer\AggregateIdNormalizer;
use Patchlevel\EventSourcing\Tests\Benchmark\BasicImplementation\ProfileId;
use Patchlevel\Hydrator\Normalizer\Normalizer;

use function is_string;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class ProfileIdNormalizer implements Normalizer
final class ProfileIdNormalizer extends AggregateIdNormalizer
{
public function normalize(mixed $value): string
public function __construct()
{
if (!$value instanceof ProfileId) {
throw new InvalidArgumentException();
}

return $value->toString();
}

public function denormalize(mixed $value): ProfileId|null
{
if ($value === null) {
return null;
}

if (!is_string($value)) {
throw new InvalidArgumentException();
}

return ProfileId::fromString($value);
parent::__construct(ProfileId::class);
}
}

0 comments on commit f49fda1

Please sign in to comment.