mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
refactored AbstractFactory
This commit is contained in:
35
Creational/AbstractFactory/CsvParser.php
Normal file
35
Creational/AbstractFactory/CsvParser.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parsedLines[] = str_getcsv($line);
|
||||
}
|
||||
|
||||
return $parsedLines;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user