Numeric::castFromLine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Claudsonm\Pedi\Structure\Types;
4
5
use Claudsonm\Pedi\Exceptions\PediException;
6
use Claudsonm\Pedi\Structure\Field;
7
8
class Numeric implements Type
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13 66
    public function castFromLine(Field $field, $value)
14
    {
15 66
        if (! $this->isValidInput($value)) {
16 2
            throw PediException::invalidInput($field, $value);
17
        }
18
19 64
        return (int) $value;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 18
    public function castToString(Field $field, $value): string
26
    {
27 18
        return str_pad($value, $field->getSize(), '0', STR_PAD_LEFT);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 67
    public function isValidInput($value): bool
34
    {
35 67
        return preg_match('/^[0-9]+$/', $value);
36
    }
37
}
38