FixedWidthColumnsParser::parseLine()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3
Metric Value
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
namespace LWI\DeliveryTracking\Parser;
4
5
/**
6
 * Class FixedWidthColumnsParser
7
 */
8
class FixedWidthColumnsParser
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $structure = [];
14
15
    /**
16
     * FixedWidthColumnsParser constructor.
17
     * @param $structure
18
     */
19 9
    public function __construct($structure)
20
    {
21 9
        $this->structure = $structure;
22 9
    }
23
24
    /**
25
     * @param $line
26
     *
27
     * @return array
28
     */
29 6
    public function parseLine($line)
30
    {
31 6
        $result = [];
32 6
        $cursor = 0;
33
34 6
        foreach ($this->structure as $field => $width) {
35 6
            $fieldValue = trim(mb_substr($line, $cursor, $width));
36
37 6
            if ($fieldValue !== '') {
38 6
                $result[$field] = $fieldValue;
39 4
            }
40
41 6
            $cursor += $width;
42 4
        }
43
44 6
        return $result;
45
    }
46
47
    /**
48
     * @param $content
49
     *
50
     * @return array
51
     */
52 3
    public function parseMultiLine($content)
53
    {
54 3
        $lines = explode("\n", $content);
55 3
        $results = [];
56
57 3
        foreach ($lines as $line) {
58 3
            $lineData = $this->parseLine($line);
59
60 3
            if (!empty($lineData)) {
61 3
                $results[] = $lineData;
62 2
            }
63 2
        }
64
65 3
        return $results;
66
    }
67
}
68