Completed
Push — task/csv-delimiters ( e13308 )
by Oliver
01:46
created

SimpleParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MarcusJaschen\Collmex\Csv;
5
6
/**
7
 * CSV Parser Implementation using PHP's built-in CSV handling capabilities.
8
 *
9
 * @author   Marcus Jaschen <[email protected]>
10
 */
11
class SimpleParser implements ParserInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private const DELIMITER = ';';
17
18
    /**
19
     * @var string
20
     */
21
    private const ENCLOSURE = '"';
22
23
    /**
24
     * @param string $csv one or multiple lines of CSV data
25
     *
26
     * @return array
27
     */
28
    public function parse(string $csv): array
29
    {
30
        $tmpHandle = tmpfile();
31
        fwrite($tmpHandle, $csv);
32
        rewind($tmpHandle);
33
34
        $data = [];
35
36
        while ($line = fgetcsv($tmpHandle, 0, self::DELIMITER, self::ENCLOSURE)) {
37
            $data[] = $line;
38
        }
39
40
        fclose($tmpHandle);
41
42
        return $data;
43
    }
44
}
45