Mappers   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 54
ccs 3
cts 9
cp 0.3333
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 5 1
A mapBatches() 0 5 1
A mapRecords() 0 5 1
1
<?php
2
3
namespace mindplay\sql\model\components;
4
5
use mindplay\sql\framework\Mapper;
6
use mindplay\sql\framework\mappers\BatchMapper;
7
use mindplay\sql\framework\mappers\RecordMapper;
8
9
/**
10
 * This trait implements support for mapping of results.
11
 */
12
trait Mappers
13
{
14
    /**
15
     * @var Mapper[] list of Mappers
16
     */
17
    protected array $mappers = [];
18
19
    /**
20
     * Append a Mapper instance to apply when each batch of a record-set is fetched.
21
     *
22
     * @param Mapper $mapper
23
     *
24
     * @see mapRecords() to map an anonymous function against every record
25
     * @see mapBatches() to map an anonymous function against each batch of records
26
     *                   
27
     * @return $this
28
     */
29
    public function map(Mapper $mapper): static
30
    {
31
        $this->mappers[] = $mapper;
32
        
33
        return $this;
34
    }
35
36
    /**
37
     * Map an anonymous function against every record.
38
     *
39
     * @param callable $mapper function (mixed $record) : mixed
40
     *
41
     * @see mapBatches() to map an anonymous function against each batch of records
42
     *                   
43
     * @return $this
44
     */
45 1
    public function mapRecords(callable $mapper): static
46
    {
47 1
        $this->mappers[] = new RecordMapper($mapper);
48
        
49 1
        return $this;
50
    }
51
52
    /**
53
     * Map an anonymous function against each batch of records.
54
     *
55
     * @param callable $mapper function (array $record_set) : array
56
     *
57
     * @see mapRecords() to map an anonymous function against every record
58
     *                   
59
     * @return $this
60
     */
61
    public function mapBatches(callable $mapper): static
62
    {
63
        $this->mappers[] = new BatchMapper($mapper);
64
        
65
        return $this;
66
    }
67
}
68