2018-06-15 18:47:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DesignPatterns\Creational\AbstractFactory;
|
|
|
|
|
|
|
|
class CsvParser implements Parser
|
|
|
|
{
|
|
|
|
const OPTION_CONTAINS_HEADER = true;
|
|
|
|
const OPTION_CONTAINS_NO_HEADER = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $skipHeaderLine;
|
|
|
|
|
|
|
|
public function __construct(bool $skipHeaderLine)
|
|
|
|
{
|
|
|
|
$this->skipHeaderLine = $skipHeaderLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parse(string $input): array
|
|
|
|
{
|
|
|
|
$headerWasParsed = false;
|
|
|
|
$parsedLines = [];
|
|
|
|
|
|
|
|
foreach (explode(PHP_EOL, $input) as $line) {
|
|
|
|
if (!$headerWasParsed && $this->skipHeaderLine === self::OPTION_CONTAINS_HEADER) {
|
2018-12-04 09:47:31 +01:00
|
|
|
$headerWasParsed = true;
|
2018-06-15 18:47:18 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedLines[] = str_getcsv($line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parsedLines;
|
|
|
|
}
|
|
|
|
}
|