Passed
Push — master ( 08379c...c55c96 )
by Claudson
07:14
created

Record   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
dl 0
loc 60
ccs 23
cts 23
cp 1
c 1
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 3 1
A getLength() 0 3 1
A add() 0 6 1
A parse() 0 5 2
A setLineNumber() 0 5 1
A getLineContent() 0 8 2
A getLineNumber() 0 3 1
1
<?php
2
3
namespace Claudsonm\Pedi\Structure;
4
5
class Record
6
{
7
    /**
8
     * @var array|Field[]
9
     */
10
    protected array $fields;
11
12
    protected int $length = 0;
13
14
    protected ?int $lineNumber = null;
15
16
    /**
17
     * @return Record
18
     */
19 63
    public function add(Field $field)
20
    {
21 63
        $this->fields[] = $field;
22 63
        $this->length += $field->getSize();
23
24 63
        return $this;
25
    }
26
27 8
    public function getLineContent(): string
28
    {
29 8
        $line = '';
30 8
        foreach ($this->fields as $field) {
31 8
            $line .= $field->getContent();
32
        }
33
34 8
        return $line;
35
    }
36
37 2
    public function getLength(): int
38
    {
39 2
        return $this->length;
40
    }
41
42 49
    public function setLineNumber(?int $number): self
43
    {
44 49
        $this->lineNumber = $number;
45
46 49
        return $this;
47
    }
48
49 43
    public function getLineNumber(): ?int
50
    {
51 43
        return $this->lineNumber;
52
    }
53
54 61
    public function parse(string $line): void
55
    {
56 61
        foreach ($this->fields as $field) {
57 61
            $value = substr($line, $field->getStart() - 1, $field->getSize());
58 61
            $field->setContent($value);
59
        }
60 61
    }
61
62 2
    public function matches(string $line): bool
63
    {
64 2
        return $this->getLength() === strlen($line);
65
    }
66
}
67