diff --git a/CHANGELOG.md b/CHANGELOG.md index 47cce143..37a797bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * refactor: Change context array to `Context` object. * refactor: Move `Context` to its own namespace. * style(Context): Rename `set` to `with` and `remove` to `without`. +* refactor: Reintroduce `Context` to `MapperInterface`. ## 0.5.4 diff --git a/src/Context/Context.php b/src/Context/Context.php index 0af509b0..eb82f4bf 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -28,9 +28,19 @@ private function __construct( ) { } - public static function create(): self + /** + * @param array $context + * @return self + */ + public static function create(array $context = []): self { - return new self(); + $self = new self(); + + foreach($context as $value) { + $self = $self->with($value); + } + + return $self; } /** diff --git a/src/Mapper.php b/src/Mapper.php index a1c8a9cf..18007f0a 100644 --- a/src/Mapper.php +++ b/src/Mapper.php @@ -30,7 +30,7 @@ public function __construct( * @param class-string|T $target * @return T */ - public function map(mixed $source, object|string $target): object + public function map(mixed $source, object|string $target, ?Context $context = null): object { if (is_string($target)) { $targetClass = $target; @@ -53,7 +53,7 @@ public function map(mixed $source, object|string $target): object source: $source, target: $target, targetTypes: [$targetType], - context: Context::create(), + context: $context ?? Context::create(), ); if ($target === null) { diff --git a/src/MapperInterface.php b/src/MapperInterface.php index 1a3781f3..f6011e4b 100644 --- a/src/MapperInterface.php +++ b/src/MapperInterface.php @@ -13,6 +13,8 @@ namespace Rekalogika\Mapper; +use Rekalogika\Mapper\Context\Context; + interface MapperInterface { /** @@ -20,5 +22,5 @@ interface MapperInterface * @param class-string|T $target * @return T */ - public function map(mixed $source, object|string $target): mixed; + public function map(mixed $source, object|string $target, ?Context $context = null): mixed; }