-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
147 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Context; | ||
|
||
/** | ||
* Adds additional values to the target object | ||
*/ | ||
final readonly class ExtraTargetValues | ||
{ | ||
/** | ||
* @param array<class-string,array<string,mixed>> $arguments | ||
*/ | ||
public function __construct( | ||
private array $arguments = [], | ||
) {} | ||
|
||
/** | ||
* @param list<class-string> $classes The class and its parent classes and interfaces. | ||
* @return array<string,mixed> | ||
*/ | ||
public function getArgumentsForClass(array $classes): array | ||
{ | ||
$arguments = []; | ||
|
||
foreach ($classes as $class) { | ||
if (isset($this->arguments[$class])) { | ||
$arguments = array_merge($arguments, $this->arguments[$class]); | ||
} | ||
} | ||
|
||
return $arguments; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\ExtraTargetValues; | ||
|
||
class SomeObject | ||
{ | ||
public string $property = 'someProperty'; | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/src/Fixtures/ExtraTargetValues/SomeObjectWithConstructorDto.php
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\Fixtures\ExtraTargetValues; | ||
|
||
final readonly class SomeObjectWithConstructorDto | ||
{ | ||
public function __construct( | ||
public string $property, | ||
public ?\DateTimeInterface $date, | ||
) {} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Tests\IntegrationTest; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Context\ExtraTargetValues; | ||
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; | ||
use Rekalogika\Mapper\Tests\Fixtures\ExtraTargetValues\SomeObject; | ||
use Rekalogika\Mapper\Tests\Fixtures\ExtraTargetValues\SomeObjectWithConstructorDto; | ||
|
||
class ExtraTargetValuesTest extends FrameworkTestCase | ||
{ | ||
public function testExtraArgumentsOnTargetConstructor(): void | ||
{ | ||
$target = $this->mapper->map( | ||
source: new SomeObject(), | ||
target: SomeObjectWithConstructorDto::class, | ||
context: Context::create( | ||
new ExtraTargetValues([ | ||
SomeObjectWithConstructorDto::class => [ | ||
'date' => new \DateTimeImmutable('2021-01-01'), | ||
], | ||
]), | ||
), | ||
); | ||
|
||
$this->assertSame('someProperty', $target->property); | ||
$this->assertInstanceOf(\DateTimeImmutable::class, $target->date); | ||
$this->assertSame('2021-01-01', $target->date->format('Y-m-d')); | ||
} | ||
} |