forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The method `MapperBuilder::bind()` can be used to define a custom way to build an object during the mapping. The return type of the callback will be resolved by the mapping to know when to use it. The callback can take any arguments, that will automatically be mapped using the given source. These arguments can then be used to instantiate the object in the desired way. Example: ```php (new \CuyZ\Valinor\MapperBuilder()) ->bind(function(string $string, OtherClass $otherClass): SomeClass { $someClass = new SomeClass($string); $someClass->addOtherClass($otherClass); return $someClass; }) ->mapper() ->map(SomeClass::class, [ // … ]); ```
- Loading branch information
Showing
11 changed files
with
212 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Object; | ||
|
||
use CuyZ\Valinor\Definition\FunctionDefinition; | ||
|
||
use function array_values; | ||
|
||
/** @internal */ | ||
final class CallbackObjectBuilder implements ObjectBuilder | ||
{ | ||
private FunctionDefinition $function; | ||
|
||
/** @var callable(): object */ | ||
private $callback; | ||
|
||
/** | ||
* @param callable(): object $callback | ||
*/ | ||
public function __construct(FunctionDefinition $function, callable $callback) | ||
{ | ||
$this->function = $function; | ||
$this->callback = $callback; | ||
} | ||
|
||
public function describeArguments(): iterable | ||
{ | ||
foreach ($this->function->parameters() as $parameter) { | ||
$argument = $parameter->isOptional() | ||
? Argument::optional($parameter->name(), $parameter->type(), $parameter->defaultValue()) | ||
: Argument::required($parameter->name(), $parameter->type()); | ||
|
||
yield $argument->withAttributes($parameter->attributes()); | ||
} | ||
} | ||
|
||
public function build(array $arguments): object | ||
{ | ||
// @PHP8.0 `array_values` can be removed | ||
/** @infection-ignore-all */ | ||
$arguments = array_values($arguments); | ||
|
||
return ($this->callback)(...$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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Mapper\Object\Factory; | ||
|
||
use CuyZ\Valinor\Definition\ClassDefinition; | ||
use CuyZ\Valinor\Definition\FunctionDefinition; | ||
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository; | ||
use CuyZ\Valinor\Mapper\Object\CallbackObjectBuilder; | ||
use CuyZ\Valinor\Mapper\Object\ObjectBuilder; | ||
use CuyZ\Valinor\Mapper\Object\ObjectBuilderFilterer; | ||
|
||
/** @internal */ | ||
final class ObjectBindingBuilderFactory implements ObjectBuilderFactory | ||
{ | ||
private ObjectBuilderFactory $delegate; | ||
|
||
private FunctionDefinitionRepository $functionDefinitionRepository; | ||
|
||
private ObjectBuilderFilterer $objectBuilderFilterer; | ||
|
||
/** @var list<callable> */ | ||
private array $callbacks; | ||
|
||
/** @var list<FunctionDefinition> */ | ||
private array $functions; | ||
|
||
/** | ||
* @param list<callable> $callbacks | ||
*/ | ||
public function __construct( | ||
ObjectBuilderFactory $delegate, | ||
FunctionDefinitionRepository $functionDefinitionRepository, | ||
ObjectBuilderFilterer $objectBuilderFilterer, | ||
array $callbacks | ||
) { | ||
$this->delegate = $delegate; | ||
$this->functionDefinitionRepository = $functionDefinitionRepository; | ||
$this->objectBuilderFilterer = $objectBuilderFilterer; | ||
$this->callbacks = $callbacks; | ||
} | ||
|
||
public function for(ClassDefinition $class, $source): ObjectBuilder | ||
{ | ||
$builders = []; | ||
|
||
foreach ($this->functions() as $key => $function) { | ||
if ($function->returnType()->matches($class->type())) { | ||
$builders[] = new CallbackObjectBuilder($function, $this->callbacks[$key]); | ||
} | ||
} | ||
|
||
if (empty($builders)) { | ||
return $this->delegate->for($class, $source); | ||
} | ||
|
||
return $this->objectBuilderFilterer->filter($source, ...$builders); | ||
} | ||
|
||
/** | ||
* @return FunctionDefinition[] | ||
*/ | ||
private function functions(): array | ||
{ | ||
if (! isset($this->functions)) { | ||
$this->functions = []; | ||
|
||
foreach ($this->callbacks as $key => $callback) { | ||
$this->functions[$key] = $this->functionDefinitionRepository->for($callback); | ||
} | ||
} | ||
|
||
return $this->functions; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.