CSV::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MidasSoft\DominicanBankParser\Files;
4
5
class CSV extends AbstractFile
6
{
7
    /**
8
     * Eliminates all unnecesary
9
     * values in a CSV file.
10
     *
11
     * @return array
12
     */
13 12
    private function sanitize()
14
    {
15 12
        $fileWithoutEmptyLine = preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', '\n', $this->fileContent);
16 12
        $cleanedCsvAmounts = preg_replace('/,(?=[^\"]*\"[^\"]*(?:\"[^\"]*\"[^\"]*)*$)/', '', $fileWithoutEmptyLine);
17 12
        $cleanedCsvAmounts = preg_replace('/[\"]+/', '', $cleanedCsvAmounts);
18
19 12
        $lines = explode(PHP_EOL, $cleanedCsvAmounts);
20
21 12
        return $lines;
22
    }
23
24
    /**
25
     * Converts a CSV string to array.
26
     *
27
     * @param string $csvString
28
     *
29
     * @return array
30
     */
31 12
    public function toArray()
32
    {
33 12
        $csvArray = [];
34 12
        foreach ($this->sanitize() as $key => $line) {
35 12
            $actualLine = str_getcsv(strip_tags(stripslashes(trim($line))));
36 12
            $csvArray[] = array_filter($actualLine);
37
        }
38
39 12
        return array_values(array_filter($csvArray));
40
    }
41
}
42