-
Notifications
You must be signed in to change notification settings - Fork 5
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 #253 from patchlevel/normalizer
Add native normalizer
- Loading branch information
Showing
38 changed files
with
925 additions
and
67 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
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Serializer\Hydrator; | ||
|
||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class DenormalizationFailure extends HydratorException | ||
{ | ||
/** | ||
* @param class-string $class | ||
* @param class-string $normalizer | ||
*/ | ||
public function __construct(string $class, string $property, string $normalizer, Throwable $e) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'denormalization for the property "%s" in the class "%s" with the normalizer "%s" failed.', | ||
$property, | ||
$class, | ||
$normalizer | ||
), | ||
0, | ||
$e | ||
); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Serializer\Hydrator; | ||
|
||
use Patchlevel\EventSourcing\Serializer\SerializeException; | ||
|
||
abstract class HydratorException extends SerializeException | ||
{ | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Serializer\Hydrator; | ||
|
||
use Throwable; | ||
|
||
use function sprintf; | ||
|
||
final class NormalizationFailure extends HydratorException | ||
{ | ||
/** | ||
* @param class-string $class | ||
* @param class-string $normalizer | ||
*/ | ||
public function __construct(string $class, string $property, string $normalizer, Throwable $e) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'normalization for the property "%s" in the class "%s" with the normalizer "%s" failed.', | ||
$property, | ||
$class, | ||
$normalizer | ||
), | ||
0, | ||
$e | ||
); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Serializer\Normalizer; | ||
|
||
use function array_map; | ||
use function is_array; | ||
|
||
class ArrayNormalizer implements Normalizer | ||
{ | ||
public function __construct(private readonly Normalizer $normalizer) | ||
{ | ||
} | ||
|
||
public function normalize(mixed $value): ?array | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new InvalidArgument(); | ||
} | ||
|
||
return array_map(fn (mixed $value): mixed => $this->normalizer->normalize($value), $value); | ||
} | ||
|
||
public function denormalize(mixed $value): ?array | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new InvalidArgument(); | ||
} | ||
|
||
return array_map(fn (mixed $value): mixed => $this->normalizer->denormalize($value), $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Serializer\Normalizer; | ||
|
||
use DateTimeImmutable; | ||
|
||
use function is_string; | ||
|
||
class DateTimeImmutableNormalizer implements Normalizer | ||
{ | ||
public function __construct( | ||
private readonly string $format = DateTimeImmutable::ATOM | ||
) { | ||
} | ||
|
||
public function normalize(mixed $value): ?string | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!$value instanceof DateTimeImmutable) { | ||
throw new InvalidArgument(); | ||
} | ||
|
||
return $value->format($this->format); | ||
} | ||
|
||
public function denormalize(mixed $value): ?DateTimeImmutable | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new InvalidArgument(); | ||
} | ||
|
||
$date = DateTimeImmutable::createFromFormat($this->format, $value); | ||
|
||
if ($date === false) { | ||
throw new InvalidArgument(); | ||
} | ||
|
||
return $date; | ||
} | ||
} |
Oops, something went wrong.