Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dynamic interface resolution #560

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/InterfaceResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CuyZ\Valinor;

/**
* @api
*/
interface InterfaceResolver
{
/**
* @param array<string,mixed>|null $props
* @return class-string|null
*/
public function resolve(string $interface, ?array $props): ?string;

/**
* @return string[]
*/
public function getResolverProps(string $interface): array;

/**
* @return array<string|int,mixed>
*/
public function transform(object $input, callable $next): array;
}
1 change: 1 addition & 0 deletions src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public function __construct(Settings $settings)
$this->get(FunctionDefinitionRepository::class),
$settings->customConstructors
),
$settings->interfaceResolver,
);

$builder = new CasterProxyNodeBuilder($builder);
Expand Down
3 changes: 3 additions & 0 deletions src/Library/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CuyZ\Valinor\Library;

use CuyZ\Valinor\InterfaceResolver;
use CuyZ\Valinor\Mapper\Object\Constructor;
use CuyZ\Valinor\Mapper\Object\DynamicConstructor;
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
Expand Down Expand Up @@ -59,6 +60,8 @@ final class Settings
/** @var array<class-string, null> */
public array $transformerAttributes = [];

public ?InterfaceResolver $interfaceResolver = null;

public function __construct()
{
$this->inferredMapping[DateTimeInterface::class] = static fn () => DateTimeImmutable::class;
Expand Down
17 changes: 17 additions & 0 deletions src/Mapper/Tree/Builder/InterfaceNodeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CuyZ\Valinor\Definition\FunctionsContainer;
use CuyZ\Valinor\Definition\Repository\ClassDefinitionRepository;
use CuyZ\Valinor\InterfaceResolver;
use CuyZ\Valinor\Mapper\Object\Arguments;
use CuyZ\Valinor\Mapper\Object\ArgumentsValues;
use CuyZ\Valinor\Mapper\Object\Exception\InvalidSource;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private ObjectImplementations $implementations,
private ClassDefinitionRepository $classDefinitionRepository,
private FunctionsContainer $constructors,
private ?InterfaceResolver $interfaceResolver,
) {}

public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode
Expand All @@ -53,6 +55,21 @@ public function build(Shell $shell, RootNodeBuilder $rootBuilder): TreeNode

if (! $this->implementations->has($className)) {
if ($type instanceof InterfaceType || $this->classDefinitionRepository->for($type)->isAbstract) {
if ($this->interfaceResolver) {
$value = $shell->value();
if (is_array($value)) {
$resolver_props = [];
foreach($this->interfaceResolver->getResolverProps($className) as $prop) {
$resolver_props[$prop] = $value[$prop];
unset($value[$prop]);
}
$resolvedClassName = $this->interfaceResolver->resolve($className, $resolver_props);
if ($resolvedClassName !== null) {
$shell = $shell->withType(new NativeClassType($resolvedClassName))->withValue($value);
return $this->delegate->build($shell, $rootBuilder);
}
}
}
throw new CannotResolveObjectType($className);
}

Expand Down
9 changes: 9 additions & 0 deletions src/MapperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ public function registerTransformer(callable|string $transformer, int $priority
return $clone;
}

public function withInterfaceResolver(InterfaceResolver $resolver): self
{

$clone = clone $this;
$clone->settings->interfaceResolver = $resolver;

return $clone->registerTransformer($resolver->transform(...));
}

/**
* Warms up the injected cache implementation with the provided class names.
*
Expand Down
Loading