|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Mapper; |
|
6
|
|
|
|
|
7
|
|
|
use Fp\Collections\Entry; |
|
8
|
|
|
use Fp\Collections\HashMap; |
|
9
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsService; |
|
10
|
|
|
use FRZB\Component\PhpDocReader\Reader\ReaderInterface as PhpDocReader; |
|
11
|
|
|
use FRZB\Component\RequestMapper\Helper\ClassHelper; |
|
12
|
|
|
use FRZB\Component\RequestMapper\Helper\PropertyHelper; |
|
13
|
|
|
use FRZB\Component\RequestMapper\Mapper\MapperLocatorInterface as MapperLocator; |
|
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag; |
|
15
|
|
|
|
|
16
|
|
|
#[AutoconfigureTag(MapperInterface::class)] |
|
17
|
|
|
#[AsService(calls: ['setMapperLocator' => [MapperLocator::class]])] |
|
18
|
|
|
class ClassMapper implements MapperInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private readonly MapperLocator $mapperLocator; |
|
21
|
|
|
|
|
22
|
19 |
|
public function __construct( |
|
23
|
|
|
private readonly PhpDocReader $reader, |
|
24
|
|
|
) { |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
19 |
|
public function __invoke(string $typeName, mixed $value, array $parameters = []): mixed |
|
28
|
|
|
{ |
|
29
|
19 |
|
return HashMap::collect(PropertyHelper::getMapping($typeName, $parameters, $this->reader)) |
|
30
|
19 |
|
->map(fn (Entry $e) => match (true) { |
|
31
|
19 |
|
\is_array($e->value) => $this->mapperLocator->get($e->value, $value)($e->value, $e->value, $parameters), |
|
32
|
19 |
|
default => $this->mapperLocator->get($e->value, $value[$e->key] ?? null)($e->value, $value[$e->key] ?? null, $parameters) |
|
33
|
|
|
}) |
|
34
|
19 |
|
->toAssocArray() |
|
35
|
19 |
|
->getOrElse([]) |
|
36
|
|
|
; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
19 |
|
public function canMap(string $typeName, mixed $value = null): bool |
|
40
|
|
|
{ |
|
41
|
19 |
|
return \is_array($value) && ClassHelper::isNotBuiltinAndExists($typeName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public static function getPriority(): int |
|
45
|
|
|
{ |
|
46
|
1 |
|
return 3; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
19 |
|
public function setMapperLocator(MapperLocator $mapperLocator): void |
|
50
|
|
|
{ |
|
51
|
19 |
|
$this->mapperLocator = $mapperLocator; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: