Completed
Push — master ( 86da7a...3c4846 )
by Lucas
02:02
created

ParseHelper::parseRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Kasifi\PdfParserBundle\Util;
4
5
class ParseHelper {
6
    /**
7
     * @param $data
8
     * @param $startConditions
9
     *
10
     * @return bool|int|null
11
     */
12
    public static function findPosition($data, $startConditions)
13
    {
14
        $firstResult = null;
15
        foreach ($startConditions as $startCondition) {
16
            preg_match($startCondition, $data, $matches);
17
            if (count($matches)) {
18
                $pos = strpos($data, $matches[0]);
19
                if (is_null($firstResult) || $pos < $firstResult) {
20
                    $firstResult = $pos;
21
                }
22
            }
23
            unset($matches);
24
        }
25
26
        return $firstResult;
27
    }
28
29
    /**
30
     * @param $rawRow
31
     *
32
     * @return array
33
     */
34
    public static function getSpacePositions($rawRow)
35
    {
36
        $spacePositions = [];
37
        foreach (str_split($rawRow) as $key => $char) {
38
            if ($char == ' ') {
39
                $spacePositions[] = $key;
40
            }
41
        }
42
43
        return $spacePositions;
44
    }
45
46
    /**
47
     * @param $row
48
     * @param $newRow
49
     *
50
     * @return mixed
51
     */
52
    public static function mergeRows($row, $newRow)
53
    {
54
        foreach ($newRow as $newRowColumnIndex => $newRowColumnValue) {
55
            if (strlen($newRowColumnValue)) {
56
                $row[$newRowColumnIndex] = trim($row[$newRowColumnIndex] . "\n" . $newRowColumnValue);
57
            }
58
        }
59
60
        return $row;
61
    }
62
63
    /**
64
     * @param $colWidths
65
     * @param $rawRow
66
     *
67
     * @return array
68
     */
69
    public static function parseRow($colWidths, $rawRow)
70
    {
71
        $colValues = [];
72
        foreach ($colWidths as $item) {
73
            $colValues[] = trim(substr($rawRow, $item['start'], $item['length']));
74
        }
75
76
        return $colValues;
77
    }
78
79
    /**
80
     * @param $rows
81
     *
82
     * @return mixed
83
     */
84
    public static function inlineDates($rows)
85
    {
86
        foreach ($rows as &$row) {
87
            foreach ($row as &$col) {
88
                $col = $col instanceof \DateTime ? $col->format(\DateTime::ISO8601) : $col;
89
            }
90
        }
91
92
        return $rows;
93
    }
94
95
    /**
96
     * @param $rawRows
97
     * @param $skipKeys
98
     * @param $rowSkipConditions
99
     *
100
     * @return array
101
     */
102
    public static function prepareRows($rawRows, $skipKeys, $rowSkipConditions)
103
    {
104
        $rows = [];
105
        $maxWidth = 0;
106
        foreach ($rawRows as $key => $rawRow) {
107
            // BYPASS EMPTY ROWS OR SPECIFIED ONES TO BE BYPASSED
108
            if (!strlen(trim($rawRow)) || in_array($key, $skipKeys)) {
109
                continue;
110
            }
111
            // SKIP ROWS TO SKIP
112
            foreach ($rowSkipConditions as $rowSkipCondition) {
113
                if (strpos($rawRow, $rowSkipCondition) !== false) {
114
                    continue 2;
115
                }
116
            }
117
118
            if (strlen($rawRow) > $maxWidth) {
119
                $maxWidth = strlen($rawRow);
120
            }
121
            $rows[] = $rawRow;
122
        }
123
        // SET SAME PADDING FOR EACH ROWS
124
        foreach ($rows as &$row) {
125
            $row = str_pad($row, $maxWidth, ' ');
126
        }
127
        unset($row);
128
129
        return $rows;
130
    }
131
132
}
133