MultispacedParser::getMap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace TraderInteractive\ColumnParser\HeaderParser;
4
5
/**
6
 * This determines the widths of each column in a header row.
7
 */
8
class MultispacedParser
9
{
10
    /**
11
     * Splits a line on consecutive spaces (2+) to determine the width of the column and the column header.
12
     *
13
     * The trailing spaces are counted as part of the width of the column, but aren't included in the header.
14
     *
15
     * @param string $line The line of data.
16
     * @return array A map of column header => column width.
17
     */
18
    public function getMap(string $line) : array
19
    {
20
        preg_match_all('/(.+?)( {2,}|$)/', $line, $matches);
21
        $columnHeaders = $matches[1];
22
        $columnWidths = array_map('strlen', $matches[0]);
23
24
        return array_combine($columnHeaders, $columnWidths);
25
    }
26
}
27