Completed
Push — master ( dd3d23...310596 )
by Hannes
02:39
created

RecordReader::readRecord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace byrokrat\autogiro;
6
7
/**
8
 * Match and capture parts of a line into a Record
9
 */
10
class RecordReader
11
{
12
    /**
13
     * @var Mathcer\Matcher
14
     */
15
    private $matchers = [];
16
17
    /**
18
     * @var callable Record builder
19
     */
20
    private $builder;
21
22
    /**
23
     * Inject Record builder
24
     *
25
     * The builder should take an array of values captured by registered
26
     * matchers and return a Record object.
27
     */
28 1
    public function __construct(callable $builder)
29
    {
30 1
        $this->builder = $builder;
31 1
    }
32
33
    /**
34
     * Capture content matching $matcher
35
     */
36 1
    public function match(string $key, Matcher\Matcher $matcher): self
37
    {
38 1
        $this->matchers[$key] = $matcher;
39 1
        return $this;
40
    }
41
42
    /**
43
     * Create Record from matched $line content
44
     */
45 1
    public function readRecord(Line $line): Record
46
    {
47 1
        $parts = [];
48
49 1
        foreach ($this->matchers as $key => $matcher) {
50 1
            $parts[$key] = $matcher->match($line);
51
        }
52
53 1
        return ($this->builder)($parts);
54
    }
55
}
56