Skip to content

Commit

Permalink
refactor: Reintroduce Context to MapperInterface.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 13, 2024
1 parent d8b5cfb commit 4ddcf91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ private function __construct(
) {
}

public static function create(): self
/**
* @param array<int,object> $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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
* @param class-string<T>|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;
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/MapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@

namespace Rekalogika\Mapper;

use Rekalogika\Mapper\Context\Context;

interface MapperInterface
{
/**
* @template T of object
* @param class-string<T>|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;
}

0 comments on commit 4ddcf91

Please sign in to comment.