-
-
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
15 changed files
with
724 additions
and
4 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
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,119 @@ | ||
<?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\MethodMapper; | ||
|
||
use Rekalogika\Mapper\Contracts\MainTransformerAwareInterface; | ||
use Rekalogika\Mapper\Contracts\MainTransformerAwareTrait; | ||
use Rekalogika\Mapper\Contracts\TransformerInterface; | ||
use Rekalogika\Mapper\Contracts\TypeMapping; | ||
use Rekalogika\Mapper\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\MainTransformer; | ||
use Rekalogika\Mapper\ObjectCache\ObjectCache; | ||
use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final class ClassMethodTransformer implements | ||
TransformerInterface, | ||
MainTransformerAwareInterface | ||
{ | ||
use MainTransformerAwareTrait; | ||
|
||
public function __construct( | ||
private SubMapper $subMapper, | ||
private ObjectCacheFactoryInterface $objectCacheFactory, | ||
) { | ||
} | ||
|
||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
Type $sourceType, | ||
?Type $targetType, | ||
array $context | ||
): mixed { | ||
// target type must not be null | ||
|
||
if ($targetType === null) { | ||
throw new InvalidArgumentException('Target type must not be null.'); | ||
} | ||
|
||
// prepare subMapper | ||
|
||
$subMapper = $this->subMapper->withMainTransformer($this->getMainTransformer()); | ||
|
||
// target class must be valid | ||
|
||
$targetClass = $targetType->getClassName(); | ||
|
||
if ( | ||
!is_string($targetClass) | ||
|| !\class_exists($targetClass) | ||
) { | ||
throw new InvalidArgumentException(sprintf('Target class "%s" is not a valid class.', (string) $targetClass)); | ||
} | ||
|
||
|
||
if (is_a($targetClass, MapFromObjectInterface::class, true)) { | ||
|
||
// map from object to self path | ||
|
||
if (!is_object($source)) { | ||
throw new InvalidArgumentException(sprintf('Source must be object, "%s" given', get_debug_type($source))); | ||
} | ||
|
||
$result = $targetClass::mapFromObject($source, $subMapper, $context); | ||
} elseif ($source instanceof MapToObjectInterface) { | ||
|
||
// map self to object path | ||
|
||
if (!is_object($target)) { | ||
$target = $targetClass; | ||
} | ||
|
||
$result = $source->mapToObject($target, $subMapper, $context); | ||
} else { | ||
throw new \LogicException('Should not reach here'); | ||
} | ||
|
||
// get object cache | ||
|
||
if (!isset($context[MainTransformer::OBJECT_CACHE])) { | ||
$objectCache = $this->objectCacheFactory->createObjectCache(); | ||
$context[MainTransformer::OBJECT_CACHE] = $objectCache; | ||
} else { | ||
/** @var ObjectCache */ | ||
$objectCache = $context[MainTransformer::OBJECT_CACHE]; | ||
} | ||
|
||
// save to object cache | ||
|
||
$objectCache->saveTarget($source, $targetType, $target); | ||
|
||
return $result; | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(MapToObjectInterface::class), | ||
TypeFactory::object(), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::object(), | ||
TypeFactory::objectOfClass(MapFromObjectInterface::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,26 @@ | ||
<?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\MethodMapper; | ||
|
||
interface MapFromObjectInterface | ||
{ | ||
/** | ||
* @param array<string,mixed> $context | ||
*/ | ||
public static function mapFromObject( | ||
object $source, | ||
SubMapperInterface $mapper, | ||
array $context = [] | ||
): static; | ||
} |
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,26 @@ | ||
<?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\MethodMapper; | ||
|
||
interface MapToObjectInterface | ||
{ | ||
/** | ||
* @param array<string,mixed> $context | ||
*/ | ||
public function mapToObject( | ||
object|string $target, | ||
SubMapperInterface $mapper, | ||
array $context = [] | ||
): object; | ||
} |
Oops, something went wrong.