Skip to content

Commit 5954a57

Browse files
author
Dominik Liebler
committed
refactored AbstractFactory
1 parent bca6af0 commit 5954a57

15 files changed

+145
-647
lines changed

Creational/AbstractFactory/AbstractFactory.php

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class CsvParser implements Parser
6+
{
7+
const OPTION_CONTAINS_HEADER = true;
8+
const OPTION_CONTAINS_NO_HEADER = false;
9+
10+
/**
11+
* @var bool
12+
*/
13+
private $skipHeaderLine;
14+
15+
public function __construct(bool $skipHeaderLine)
16+
{
17+
$this->skipHeaderLine = $skipHeaderLine;
18+
}
19+
20+
public function parse(string $input): array
21+
{
22+
$headerWasParsed = false;
23+
$parsedLines = [];
24+
25+
foreach (explode(PHP_EOL, $input) as $line) {
26+
if (!$headerWasParsed && $this->skipHeaderLine === self::OPTION_CONTAINS_HEADER) {
27+
continue;
28+
}
29+
30+
$parsedLines[] = str_getcsv($line);
31+
}
32+
33+
return $parsedLines;
34+
}
35+
}

Creational/AbstractFactory/HtmlFactory.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

Creational/AbstractFactory/HtmlText.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Creational/AbstractFactory/JsonFactory.php

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class JsonParser implements Parser
6+
{
7+
public function parse(string $input): array
8+
{
9+
return json_decode($input, true);
10+
}
11+
}

Creational/AbstractFactory/JsonText.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

Creational/AbstractFactory/Parser.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
interface Parser
6+
{
7+
public function parse(string $input): array;
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Creational\AbstractFactory;
4+
5+
class ParserFactory
6+
{
7+
public function createCsvParser(bool $skipHeaderLine): CsvParser
8+
{
9+
return new CsvParser($skipHeaderLine);
10+
}
11+
12+
public function createJsonParser(): JsonParser
13+
{
14+
return new JsonParser();
15+
}
16+
}

Creational/AbstractFactory/README.rst

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,27 @@ Code
2121

2222
You can also find this code on `GitHub`_
2323

24-
AbstractFactory.php
24+
Parser.php
2525

26-
.. literalinclude:: AbstractFactory.php
26+
.. literalinclude:: Parser.php
2727
:language: php
2828
:linenos:
2929

30-
JsonFactory.php
30+
CsvParser.php
3131

32-
.. literalinclude:: JsonFactory.php
32+
.. literalinclude:: CsvParser.php
3333
:language: php
3434
:linenos:
3535

36-
HtmlFactory.php
36+
JsonParser.php
3737

38-
.. literalinclude:: HtmlFactory.php
38+
.. literalinclude:: JsonParser.php
3939
:language: php
4040
:linenos:
4141

42-
Text.php
42+
ParserFactory.php
4343

44-
.. literalinclude:: Text.php
45-
:language: php
46-
:linenos:
47-
48-
JsonText.php
49-
50-
.. literalinclude:: JsonText.php
51-
:language: php
52-
:linenos:
53-
54-
HtmlText.php
55-
56-
.. literalinclude:: HtmlText.php
44+
.. literalinclude:: ParserFactory.php
5745
:language: php
5846
:linenos:
5947

0 commit comments

Comments
 (0)