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

Mappers   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 33.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 56
ccs 3
cts 9
cp 0.3333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A map() 0 6 1
A mapRecords() 0 6 1
A mapBatches() 0 6 1
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