-
-
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.
feat: Uses transformer class names as service IDs for easier decoration.
- Loading branch information
Showing
8 changed files
with
188 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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\Transformer; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
abstract class AbstractTransformerDecorator implements TransformerInterface | ||
{ | ||
public function __construct( | ||
private TransformerInterface $decorated, | ||
) { | ||
} | ||
|
||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
?Type $sourceType, | ||
?Type $targetType, | ||
Context $context | ||
): mixed { | ||
return $this->decorated->transform( | ||
$source, | ||
$target, | ||
$sourceType, | ||
$targetType, | ||
$context | ||
); | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
return $this->decorated->getSupportedTransformation(); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
tests/Fixtures/TransformerOverride/OverrideTransformer.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,40 @@ | ||
<?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\TransformerOverride; | ||
|
||
use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithArrayProperty; | ||
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDto; | ||
use Rekalogika\Mapper\Transformer\AbstractTransformerDecorator; | ||
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface; | ||
use Rekalogika\Mapper\Transformer\Contracts\TypeMapping; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
|
||
/** | ||
* Forcing scalar to scalar transformer, erroneously | ||
*/ | ||
class OverrideTransformer extends AbstractTransformerDecorator | ||
{ | ||
public function __construct(TransformerInterface $transformer) | ||
{ | ||
parent::__construct($transformer); | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(ObjectWithArrayProperty::class), | ||
TypeFactory::objectOfClass(ObjectWithArrayPropertyDto::class), | ||
); | ||
} | ||
} |
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); | ||
|
||
/* | ||
* 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\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest; | ||
use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithArrayProperty; | ||
use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDto; | ||
use Rekalogika\Mapper\Tests\Fixtures\TransformerOverride\OverrideTransformer; | ||
use Rekalogika\Mapper\Transformer\ScalarToScalarTransformer; | ||
|
||
class TransformerOverrideTest extends AbstractIntegrationTest | ||
{ | ||
protected function getAdditionalTransformers(): array | ||
{ | ||
$scalarToScalarTransformer = new ScalarToScalarTransformer(); | ||
|
||
return [ | ||
OverrideTransformer::class => new OverrideTransformer($scalarToScalarTransformer), | ||
]; | ||
} | ||
|
||
public function testTransformerOverride(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Source must be scalar'); | ||
$source = new ObjectWithArrayProperty(); | ||
$target = $this->mapper->map($source, ObjectWithArrayPropertyDto::class); | ||
} | ||
} |