Passed
Push — main ( 2dd0b4...7b6409 )
by Fractal
03:40 queued 12s
created

ArrayMapper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canMap() 0 3 2
A __invoke() 0 7 1
A getPriority() 0 3 1
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 Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
10
11
#[AutoconfigureTag(MapperInterface::class)]
12
class ArrayMapper implements MapperInterface
13
{
14
    public function __invoke(string $typeName, array $value, array $parameters = []): array
15
    {
16
        return HashMap::collect($parameters[key($value)] ?? [])
17
            ->map(static fn (Entry $paramEntry) => $value[$paramEntry->key])
18
            ->map(fn (Entry $paramEntry) => ($this)($paramEntry->value, $parameters[key($value)][$paramEntry->key] ?? []))
19
            ->toAssocArray()
20
            ->get()
21
        ;
22
    }
23
24
    public function canMap(string $typeName, mixed $value = null): bool
25
    {
26
        return \is_array($value) && 'array' === $typeName;
27
    }
28
29
    public static function getPriority(): int
30
    {
31
        return 4;
32
    }
33
}
34