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

Record::getLineNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
crap 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