-
-
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.
Deprecate
ArrayToObjectTransformer
& ObjectToArrayTransformer
, re…
…place with `ArrayObjectTransformer`.
- Loading branch information
Showing
12 changed files
with
279 additions
and
69 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,87 @@ | ||
<?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\Implementation; | ||
|
||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\MainTransformer\MainTransformerInterface; | ||
use Rekalogika\Mapper\Transformer\MainTransformerAwareInterface; | ||
use Rekalogika\Mapper\Transformer\TransformerInterface; | ||
use Rekalogika\Mapper\Transformer\TypeMapping; | ||
use Rekalogika\Mapper\Util\TypeCheck; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* Do object to array, array to object, and array to array transformation by | ||
* converting array to stdClass & passing the task to ObjectToObjectTransformer. | ||
*/ | ||
final readonly class ArrayObjectTransformer implements | ||
TransformerInterface, | ||
MainTransformerAwareInterface | ||
{ | ||
public function __construct( | ||
private ObjectToObjectTransformer $objectToObjectTransformer, | ||
) { | ||
} | ||
|
||
public function withMainTransformer(MainTransformerInterface $mainTransformer): static | ||
{ | ||
$objectToObjectTransformer = $this->objectToObjectTransformer | ||
->withMainTransformer($mainTransformer); | ||
|
||
return new self($objectToObjectTransformer); | ||
} | ||
|
||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
?Type $sourceType, | ||
?Type $targetType, | ||
Context $context | ||
): mixed { | ||
$originalTargetType = $targetType; | ||
|
||
if (TypeCheck::isArray($sourceType)) { | ||
$source = (object) $source; | ||
$sourceType = TypeFactory::objectOfClass(\stdClass::class); | ||
} | ||
|
||
if (TypeCheck::isArray($targetType)) { | ||
$target = (object) $target; | ||
$targetType = TypeFactory::objectOfClass(\stdClass::class); | ||
} | ||
|
||
/** @var mixed */ | ||
$result = $this->objectToObjectTransformer->transform( | ||
source: $source, | ||
target: $target, | ||
sourceType: $sourceType, | ||
targetType: $targetType, | ||
context: $context | ||
); | ||
|
||
if (TypeCheck::isArray($originalTargetType)) { | ||
return (array) $result; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping(TypeFactory::array(), TypeFactory::object(), true); | ||
yield new TypeMapping(TypeFactory::object(), TypeFactory::array()); | ||
yield new TypeMapping(TypeFactory::array(), TypeFactory::array()); | ||
} | ||
} |
Oops, something went wrong.