Completed
Push — master ( 6bd930...6adfd5 )
by Rasmus
02:26
created

Mappers::mapBatches()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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