forked from sofascore/purgatory-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompoundInverseValuesBuilder.php
More file actions
54 lines (46 loc) · 1.61 KB
/
CompoundInverseValuesBuilder.php
File metadata and controls
54 lines (46 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
namespace Sofascore\PurgatoryBundle\Cache\PropertyResolver\InverseValuesBuilder;
use Psr\Container\ContainerInterface;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\CompoundValues;
use Sofascore\PurgatoryBundle\Attribute\RouteParamValue\ValuesInterface;
/**
* @implements InverseValuesBuilderInterface<CompoundValues>
*/
final class CompoundInverseValuesBuilder implements InverseValuesBuilderInterface
{
public function __construct(
private readonly ContainerInterface $inverseValuesBuilderLocator,
) {
}
public static function for(): string
{
return CompoundValues::type();
}
public function build(ValuesInterface $values, string $associationClass, string $associationTarget): ValuesInterface
{
return new CompoundValues(
...array_map(
fn (ValuesInterface $values): ValuesInterface => $this->getInverseValuesBuilderFor($values)
?->build($values, $associationClass, $associationTarget)
?? $values,
$values->values,
),
);
}
/**
* @template T of ValuesInterface
*
* @param T $values
*
* @return ?InverseValuesBuilderInterface<T>
*/
private function getInverseValuesBuilderFor(ValuesInterface $values): ?InverseValuesBuilderInterface
{
/** @var ?InverseValuesBuilderInterface<T> $builder */
$builder = $this->inverseValuesBuilderLocator->has($type = $values::type())
? $this->inverseValuesBuilderLocator->get($type)
: null;
return $builder;
}
}