RecordMapper::map()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace mindplay\sql\framework\mappers;
4
5
use mindplay\sql\framework\Mapper;
6
use Traversable;
7
8
/**
9
 * Use this Mapper for quick, on-demand record mapping - as an alternative to implementing an actual Mapper class.
10
 */
11
class RecordMapper implements Mapper
12
{
13
    /**
14
     * @var callable(array<string,mixed>):array<string,mixed>
15
     */
16
    private $mapper;
17
18
    /**
19
     * @param callable(array<string,mixed>):array<string,mixed> $mapper function (array $record_set): array
20
     */
21 1
    public function __construct(callable $mapper)
22
    {
23 1
        $this->mapper = $mapper;
24
    }
25
26 1
    public function map(array $record_set): array|Traversable
27
    {
28 1
        $result = [];
29
30 1
        foreach ($record_set as $key => $record) {
31 1
            $result[$key] = call_user_func($this->mapper, $record);
32
        }
33
34 1
        return $result;
35
    }
36
}
37