mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 04:00:18 +02:00
refactored AbstractFactory
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* In this case, the abstract factory is a contract for creating some components
|
||||
* for the web. There are two ways of rendering text: HTML and JSON
|
||||
*/
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
abstract public function createText(string $content): Text;
|
||||
}
|
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;
|
||||
}
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class HtmlFactory extends AbstractFactory
|
||||
{
|
||||
public function createText(string $content): Text
|
||||
{
|
||||
return new HtmlText($content);
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class HtmlText extends Text
|
||||
{
|
||||
// do something here
|
||||
}
|
@@ -1,11 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class JsonFactory extends AbstractFactory
|
||||
{
|
||||
public function createText(string $content): Text
|
||||
{
|
||||
return new JsonText($content);
|
||||
}
|
||||
}
|
11
Creational/AbstractFactory/JsonParser.php
Normal file
11
Creational/AbstractFactory/JsonParser.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class JsonParser implements Parser
|
||||
{
|
||||
public function parse(string $input): array
|
||||
{
|
||||
return json_decode($input, true);
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class JsonText extends Text
|
||||
{
|
||||
// do something here
|
||||
}
|
8
Creational/AbstractFactory/Parser.php
Normal file
8
Creational/AbstractFactory/Parser.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
interface Parser
|
||||
{
|
||||
public function parse(string $input): array;
|
||||
}
|
16
Creational/AbstractFactory/ParserFactory.php
Normal file
16
Creational/AbstractFactory/ParserFactory.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class ParserFactory
|
||||
{
|
||||
public function createCsvParser(bool $skipHeaderLine): CsvParser
|
||||
{
|
||||
return new CsvParser($skipHeaderLine);
|
||||
}
|
||||
|
||||
public function createJsonParser(): JsonParser
|
||||
{
|
||||
return new JsonParser();
|
||||
}
|
||||
}
|
@@ -21,39 +21,27 @@ Code
|
||||
|
||||
You can also find this code on `GitHub`_
|
||||
|
||||
AbstractFactory.php
|
||||
Parser.php
|
||||
|
||||
.. literalinclude:: AbstractFactory.php
|
||||
.. literalinclude:: Parser.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
JsonFactory.php
|
||||
CsvParser.php
|
||||
|
||||
.. literalinclude:: JsonFactory.php
|
||||
.. literalinclude:: CsvParser.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
HtmlFactory.php
|
||||
JsonParser.php
|
||||
|
||||
.. literalinclude:: HtmlFactory.php
|
||||
.. literalinclude:: JsonParser.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Text.php
|
||||
ParserFactory.php
|
||||
|
||||
.. literalinclude:: Text.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
JsonText.php
|
||||
|
||||
.. literalinclude:: JsonText.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
HtmlText.php
|
||||
|
||||
.. literalinclude:: HtmlText.php
|
||||
.. literalinclude:: ParserFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
@@ -2,27 +2,26 @@
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Tests;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\HtmlFactory;
|
||||
use DesignPatterns\Creational\AbstractFactory\HtmlText;
|
||||
use DesignPatterns\Creational\AbstractFactory\JsonFactory;
|
||||
use DesignPatterns\Creational\AbstractFactory\JsonText;
|
||||
use DesignPatterns\Creational\AbstractFactory\CsvParser;
|
||||
use DesignPatterns\Creational\AbstractFactory\JsonParser;
|
||||
use DesignPatterns\Creational\AbstractFactory\ParserFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AbstractFactoryTest extends TestCase
|
||||
{
|
||||
public function testCanCreateHtmlText()
|
||||
public function testCanCreateCsvParser()
|
||||
{
|
||||
$factory = new HtmlFactory();
|
||||
$text = $factory->createText('foobar');
|
||||
$factory = new ParserFactory();
|
||||
$parser = $factory->createCsvParser(CsvParser::OPTION_CONTAINS_HEADER);
|
||||
|
||||
$this->assertInstanceOf(HtmlText::class, $text);
|
||||
$this->assertInstanceOf(CsvParser::class, $parser);
|
||||
}
|
||||
|
||||
public function testCanCreateJsonText()
|
||||
public function testCanCreateJsonParser()
|
||||
{
|
||||
$factory = new JsonFactory();
|
||||
$text = $factory->createText('foobar');
|
||||
$factory = new ParserFactory();
|
||||
$parser = $factory->createJsonParser();
|
||||
|
||||
$this->assertInstanceOf(JsonText::class, $text);
|
||||
$this->assertInstanceOf(JsonParser::class, $parser);
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
abstract class Text
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $text;
|
||||
|
||||
public function __construct(string $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
}
|
@@ -1,50 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Diagram>
|
||||
<ID>PHP</ID>
|
||||
<OriginalElement>\DesignPatterns\Creational\AbstractFactory\AbstractFactory</OriginalElement>
|
||||
<OriginalElement>\DesignPatterns\Creational\AbstractFactory\CsvParser</OriginalElement>
|
||||
<nodes>
|
||||
<node x="214.0" y="101.0">\DesignPatterns\Creational\AbstractFactory\JsonFactory</node>
|
||||
<node x="107.0" y="0.0">\DesignPatterns\Creational\AbstractFactory\AbstractFactory</node>
|
||||
<node x="0.0" y="101.0">\DesignPatterns\Creational\AbstractFactory\HtmlFactory</node>
|
||||
<node x="111.0" y="298.0">\DesignPatterns\Creational\AbstractFactory\JsonText</node>
|
||||
<node x="0.0" y="298.0">\DesignPatterns\Creational\AbstractFactory\HtmlText</node>
|
||||
<node x="51.5" y="197.0">\DesignPatterns\Creational\AbstractFactory\Text</node>
|
||||
<node x="116.5" y="0.0">\DesignPatterns\Creational\AbstractFactory\Parser</node>
|
||||
<node x="0.0" y="288.0">\DesignPatterns\Creational\AbstractFactory\ParserFactory</node>
|
||||
<node x="0.0" y="146.5">\DesignPatterns\Creational\AbstractFactory\JsonParser</node>
|
||||
<node x="168.0" y="101.0">\DesignPatterns\Creational\AbstractFactory\CsvParser</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\HtmlText" target="\DesignPatterns\Creational\AbstractFactory\Text">
|
||||
<point x="0.0" y="-14.5" />
|
||||
<point x="45.5" y="273.0" />
|
||||
<point x="75.75" y="273.0" />
|
||||
<point x="-24.25" y="25.5" />
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\CsvParser" target="\DesignPatterns\Creational\AbstractFactory\Parser">
|
||||
<point x="0.0" y="-71.0" />
|
||||
<point x="307.0" y="76.0" />
|
||||
<point x="227.5" y="76.0" />
|
||||
<point x="37.0" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\JsonText" target="\DesignPatterns\Creational\AbstractFactory\Text">
|
||||
<point x="0.0" y="-14.5" />
|
||||
<point x="154.5" y="273.0" />
|
||||
<point x="124.25" y="273.0" />
|
||||
<point x="24.25" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\JsonFactory" target="\DesignPatterns\Creational\AbstractFactory\AbstractFactory">
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\JsonParser" target="\DesignPatterns\Creational\AbstractFactory\Parser">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="311.0" y="76.0" />
|
||||
<point x="252.5" y="76.0" />
|
||||
<point x="48.5" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\HtmlFactory" target="\DesignPatterns\Creational\AbstractFactory\AbstractFactory">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="97.0" y="76.0" />
|
||||
<point x="155.5" y="76.0" />
|
||||
<point x="-48.5" y="25.5" />
|
||||
<point x="74.0" y="76.0" />
|
||||
<point x="153.5" y="76.0" />
|
||||
<point x="-37.0" y="25.5" />
|
||||
</edge>
|
||||
</edges>
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="117.0" y="130.5" />
|
||||
<SelectedNodes>
|
||||
<node>\DesignPatterns\Creational\AbstractFactory\AbstractFactory</node>
|
||||
</SelectedNodes>
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="107.5" y="91.0" />
|
||||
<SelectedNodes />
|
||||
<Categories>
|
||||
<Category>Methods</Category>
|
||||
<Category>Constants</Category>
|
||||
<Category>Fields</Category>
|
||||
<Category>Constants</Category>
|
||||
<Category>Constructors</Category>
|
||||
<Category>Methods</Category>
|
||||
</Categories>
|
||||
<VISIBILITY>private</VISIBILITY>
|
||||
</Diagram>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 48 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 67 KiB |
Reference in New Issue
Block a user