Skip to content

Commit

Permalink
add interface resolver option
Browse files Browse the repository at this point in the history
  • Loading branch information
mastir committed Sep 9, 2024
1 parent 4e0bc8b commit 6615a0f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/InterfaceResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace CuyZ\Valinor;

interface InterfaceResolver

Check failure on line 5 in src/InterfaceResolver.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Missing annotation `@api` or `@internal`.
{
public function resolve(string $interface, ?array $props) : ?string;

Check failure on line 7 in src/InterfaceResolver.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Method CuyZ\Valinor\InterfaceResolver::resolve() has parameter $props with no value type specified in iterable type array.

public function getResolverProps(string $interface) : array;

Check failure on line 9 in src/InterfaceResolver.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Method CuyZ\Valinor\InterfaceResolver::getResolverProps() return type has no value type specified in iterable type array.

public function transform(object $input, callable $next) : array;

Check failure on line 11 in src/InterfaceResolver.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Method CuyZ\Valinor\InterfaceResolver::transform() return type has no value type specified in iterable type 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,

Check failure on line 131 in src/Library/Container.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Parameter #5 $interfaceResolver of class CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder constructor expects CuyZ\Valinor\InterfaceResolver, CuyZ\Valinor\InterfaceResolver|null given.
);

$builder = new CasterProxyNodeBuilder($builder);
Expand Down
4 changes: 4 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,9 @@ final class Settings
/** @var array<class-string, null> */
public array $transformerAttributes = [];

/** @var InterfaceResolver|null */
public ?InterfaceResolver $interfaceResolver = null;

public function __construct()
{
$this->inferredMapping[DateTimeInterface::class] = static fn () => DateTimeImmutable::class;
Expand Down
15 changes: 15 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,19 @@ 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){

Check failure on line 58 in src/Mapper/Tree/Builder/InterfaceNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

If condition is always true.
$value = $shell->value();
$resolver_props = [];
foreach($this->interfaceResolver->getResolverProps($className) as $prop){
$resolver_props[$prop] = $value[$prop];

Check failure on line 62 in src/Mapper/Tree/Builder/InterfaceNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Cannot access offset mixed on mixed.
unset($value[$prop]);

Check failure on line 63 in src/Mapper/Tree/Builder/InterfaceNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Cannot access offset mixed on mixed.
}
$resolvedClassName = $this->interfaceResolver->resolve($className, $resolver_props);
if ($resolvedClassName !== null) {
$shell = $shell->withType(new NativeClassType($resolvedClassName))->withValue($value);

Check failure on line 67 in src/Mapper/Tree/Builder/InterfaceNodeBuilder.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

Parameter #1 $className of class CuyZ\Valinor\Type\Types\NativeClassType constructor expects class-string, string given.
return $this->delegate->build($shell, $rootBuilder);
}
}
throw new CannotResolveObjectType($className);
}

Expand Down
14 changes: 14 additions & 0 deletions src/MapperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CuyZ\Valinor;

use CuyZ\Valinor\InterfaceResolver;
use CuyZ\Valinor\Library\Container;
use CuyZ\Valinor\Library\Settings;
use CuyZ\Valinor\Mapper\ArgumentsMapper;
Expand Down Expand Up @@ -530,6 +531,19 @@ public function registerTransformer(callable|string $transformer, int $priority
return $clone;
}

/**
* @param string $factoryClass
* @return void
*/
public function withInterfaceResolver(InterfaceResolver $resolver): self

Check failure on line 538 in src/MapperBuilder.php

View workflow job for this annotation

GitHub Actions / Quality Assurance

PHPDoc tag @param references unknown parameter: $factoryClass
{

$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

0 comments on commit 6615a0f

Please sign in to comment.