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

RecordReader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A match() 0 5 1
A readRecord() 0 10 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