diff --git a/Creational/AbstractFactory/AbstractFactory.php b/Creational/AbstractFactory/AbstractFactory.php index fd69448..85bd878 100644 --- a/Creational/AbstractFactory/AbstractFactory.php +++ b/Creational/AbstractFactory/AbstractFactory.php @@ -3,38 +3,10 @@ namespace DesignPatterns\Creational\AbstractFactory; /** - * class AbstractFactory. - * - * Sometimes also known as "Kit" in a GUI libraries. - * - * This design pattern implements the Dependency Inversion Principle since - * it is the concrete subclass which creates concrete components. - * * In this case, the abstract factory is a contract for creating some components - * for the web. There are two components : Text and Picture. There are two ways - * of rendering : HTML or JSON. - * - * Therefore 4 concrete classes, but the client just needs to know this contract - * to build a correct HTTP response (for a HTML page or for an AJAX request). + * for the web. There are two ways of rendering text: HTML and JSON */ abstract class AbstractFactory { - /** - * Creates a text component. - * - * @param string $content - * - * @return Text - */ - abstract public function createText($content); - - /** - * Creates a picture component. - * - * @param string $path - * @param string $name - * - * @return Picture - */ - abstract public function createPicture($path, $name = ''); + abstract public function createText(string $content): Text; } diff --git a/Creational/AbstractFactory/Html/Picture.php b/Creational/AbstractFactory/Html/Picture.php deleted file mode 100644 index e5f430b..0000000 --- a/Creational/AbstractFactory/Html/Picture.php +++ /dev/null @@ -1,18 +0,0 @@ -', $this->path, $this->name); - } -} diff --git a/Creational/AbstractFactory/Html/Text.php b/Creational/AbstractFactory/Html/Text.php deleted file mode 100644 index 32d9350..0000000 --- a/Creational/AbstractFactory/Html/Text.php +++ /dev/null @@ -1,18 +0,0 @@ -'.htmlspecialchars($this->text).''; - } -} diff --git a/Creational/AbstractFactory/HtmlFactory.php b/Creational/AbstractFactory/HtmlFactory.php index 1d622e1..ba44216 100644 --- a/Creational/AbstractFactory/HtmlFactory.php +++ b/Creational/AbstractFactory/HtmlFactory.php @@ -4,28 +4,8 @@ namespace DesignPatterns\Creational\AbstractFactory; class HtmlFactory extends AbstractFactory { - /** - * Creates a picture component. - * - * @param string $path - * @param string $name - * - * @return Html\Picture|Picture - */ - public function createPicture($path, $name = '') + public function createText(string $content): Text { - return new Html\Picture($path, $name); - } - - /** - * Creates a text component. - * - * @param string $content - * - * @return Html\Text|Text - */ - public function createText($content) - { - return new Html\Text($content); + return new HtmlText($content); } } diff --git a/Creational/AbstractFactory/HtmlText.php b/Creational/AbstractFactory/HtmlText.php new file mode 100644 index 0000000..a3764da --- /dev/null +++ b/Creational/AbstractFactory/HtmlText.php @@ -0,0 +1,8 @@ + $this->name, 'path' => $this->path)); - } -} diff --git a/Creational/AbstractFactory/Json/Text.php b/Creational/AbstractFactory/Json/Text.php deleted file mode 100644 index df69a4d..0000000 --- a/Creational/AbstractFactory/Json/Text.php +++ /dev/null @@ -1,18 +0,0 @@ - $this->text)); - } -} diff --git a/Creational/AbstractFactory/JsonFactory.php b/Creational/AbstractFactory/JsonFactory.php index 945f55d..a767f7e 100644 --- a/Creational/AbstractFactory/JsonFactory.php +++ b/Creational/AbstractFactory/JsonFactory.php @@ -4,28 +4,8 @@ namespace DesignPatterns\Creational\AbstractFactory; class JsonFactory extends AbstractFactory { - /** - * Creates a picture component. - * - * @param string $path - * @param string $name - * - * @return Json\Picture|Picture - */ - public function createPicture($path, $name = '') + public function createText(string $content): Text { - return new Json\Picture($path, $name); - } - - /** - * Creates a text component. - * - * @param string $content - * - * @return Json\Text|Text - */ - public function createText($content) - { - return new Json\Text($content); + return new JsonText($content); } } diff --git a/Creational/AbstractFactory/JsonText.php b/Creational/AbstractFactory/JsonText.php new file mode 100644 index 0000000..a0386a0 --- /dev/null +++ b/Creational/AbstractFactory/JsonText.php @@ -0,0 +1,8 @@ +name = (string) $name; - $this->path = (string) $path; - } -} diff --git a/Creational/AbstractFactory/README.rst b/Creational/AbstractFactory/README.rst index 69980eb..5251b6c 100644 --- a/Creational/AbstractFactory/README.rst +++ b/Creational/AbstractFactory/README.rst @@ -39,45 +39,21 @@ HtmlFactory.php :language: php :linenos: -MediaInterface.php - -.. literalinclude:: MediaInterface.php - :language: php - :linenos: - -Picture.php - -.. literalinclude:: Picture.php - :language: php - :linenos: - Text.php .. literalinclude:: Text.php :language: php :linenos: -Json/Picture.php +JsonText.php -.. literalinclude:: Json/Picture.php +.. literalinclude:: JsonText.php :language: php :linenos: -Json/Text.php +HtmlText.php -.. literalinclude:: Json/Text.php - :language: php - :linenos: - -Html/Picture.php - -.. literalinclude:: Html/Picture.php - :language: php - :linenos: - -Html/Text.php - -.. literalinclude:: Html/Text.php +.. literalinclude:: HtmlText.php :language: php :linenos: diff --git a/Creational/AbstractFactory/Tests/AbstractFactoryTest.php b/Creational/AbstractFactory/Tests/AbstractFactoryTest.php index 97f4417..1bdbd1c 100644 --- a/Creational/AbstractFactory/Tests/AbstractFactoryTest.php +++ b/Creational/AbstractFactory/Tests/AbstractFactoryTest.php @@ -2,43 +2,24 @@ namespace DesignPatterns\Creational\AbstractFactory\Tests; -use DesignPatterns\Creational\AbstractFactory\AbstractFactory; use DesignPatterns\Creational\AbstractFactory\HtmlFactory; use DesignPatterns\Creational\AbstractFactory\JsonFactory; -/** - * AbstractFactoryTest tests concrete factories. - */ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase { - public function getFactories() + public function testCanCreateHtmlText() { - return array( - array(new JsonFactory()), - array(new HtmlFactory()), - ); + $factory = new HtmlFactory(); + $text = $factory->createText('foobar'); + + $this->assertInstanceOf('DesignPatterns\Creational\AbstractFactory\HtmlText', $text); } - /** - * This is the client of factories. Note that the client does not - * care which factory is given to him, he can create any component he - * wants and render how he wants. - * - * @dataProvider getFactories - */ - public function testComponentCreation(AbstractFactory $factory) + public function testCanCreateJsonText() { - $article = array( - $factory->createText('Lorem Ipsum'), - $factory->createPicture('/image.jpg', 'caption'), - $factory->createText('footnotes'), - ); + $factory = new JsonFactory(); + $text = $factory->createText('foobar'); - $this->assertContainsOnly('DesignPatterns\Creational\AbstractFactory\MediaInterface', $article); - - /* this is the time to look at the Builder pattern. This pattern - * helps you to create complex object like that article above with - * a given Abstract Factory - */ + $this->assertInstanceOf('DesignPatterns\Creational\AbstractFactory\JsonText', $text); } } diff --git a/Creational/AbstractFactory/Text.php b/Creational/AbstractFactory/Text.php index 593c4e5..60846bb 100644 --- a/Creational/AbstractFactory/Text.php +++ b/Creational/AbstractFactory/Text.php @@ -2,18 +2,15 @@ namespace DesignPatterns\Creational\AbstractFactory; -abstract class Text implements MediaInterface +abstract class Text { /** * @var string */ - protected $text; + private $text; - /** - * @param string $text - */ - public function __construct($text) + public function __construct(string $text) { - $this->text = (string) $text; + $this->text = $text; } } diff --git a/Creational/AbstractFactory/uml/AbstractFactory.uml b/Creational/AbstractFactory/uml/AbstractFactory.uml index a44e64c..224cc06 100644 --- a/Creational/AbstractFactory/uml/AbstractFactory.uml +++ b/Creational/AbstractFactory/uml/AbstractFactory.uml @@ -1,38 +1,51 @@ - - - PHP - \DesignPatterns\Creational\AbstractFactory\AbstractFactory - - \DesignPatterns\Creational\AbstractFactory\Html\Text - \DesignPatterns\Creational\AbstractFactory\Html\Picture - \DesignPatterns\Creational\AbstractFactory\HtmlFactory - \DesignPatterns\Creational\AbstractFactory\JsonFactory - \DesignPatterns\Creational\AbstractFactory\AbstractFactory - \DesignPatterns\Creational\AbstractFactory\MediaInterface - - - - - - - - - - - - - - - - - - - - Fields - Constants - Constructors - Methods - - private - - + + + PHP + \DesignPatterns\Creational\AbstractFactory\AbstractFactory + + \DesignPatterns\Creational\AbstractFactory\JsonFactory + \DesignPatterns\Creational\AbstractFactory\AbstractFactory + \DesignPatterns\Creational\AbstractFactory\HtmlFactory + \DesignPatterns\Creational\AbstractFactory\JsonText + \DesignPatterns\Creational\AbstractFactory\HtmlText + \DesignPatterns\Creational\AbstractFactory\Text + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \DesignPatterns\Creational\AbstractFactory\AbstractFactory + + + Methods + Constants + Fields + + private + + diff --git a/Creational/AbstractFactory/uml/uml.png b/Creational/AbstractFactory/uml/uml.png index 6de2767..68d3a8b 100644 Binary files a/Creational/AbstractFactory/uml/uml.png and b/Creational/AbstractFactory/uml/uml.png differ diff --git a/Creational/AbstractFactory/uml/uml.svg b/Creational/AbstractFactory/uml/uml.svg index 17b4b70..e7eff24 100644 --- a/Creational/AbstractFactory/uml/uml.svg +++ b/Creational/AbstractFactory/uml/uml.svg @@ -1,379 +1,560 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - createPicture(path, name) - - - - - - - - - createText(content) - - - - - - - - - - - - - HtmlFactory - - - HtmlFactory - - - - - - - - - - - - - - - - - - render() - - - - - - - - - - - - - Picture - - - Picture - - - - - - - - - - - - - - - - - - render() - - - - - - - - - - - - - Text - - - Text - - - - - - - - - - - - - - - - - - createPicture(path, name) - - - - - - - - - createText(content) - - - - - - - - - - - - - JsonFactory - - - JsonFactory - - - - - - - - - - - - - - - - - - createText(content) - - - - - - - - - createPicture(path, name) - - - - - - - - - - - - - AbstractFactory - - - AbstractFactory - - - - - - - - - - - - - - - - - - render() - - - - - - - - - - - - - MediaInterface - - - MediaInterface - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + JsonFactory + + + JsonFactory + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + JsonFactory + + + JsonFactory + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + AbstractFactory + + + AbstractFactory + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + AbstractFactory + + + AbstractFactory + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + HtmlFactory + + + HtmlFactory + + + + + + + + + + + + + + + + + + + + + + createText(content) + + + + + + + + + + + + + HtmlFactory + + + HtmlFactory + + + + + + + + + + + + + + + + JsonText + + + JsonText + + + + + + + + + + + + + JsonText + + + JsonText + + + + + + + + + + + + + + + + HtmlText + + + HtmlText + + + + + + + + + + + + + HtmlText + + + HtmlText + + + + + + + + + + + + + + + + + + + + + + + + + text + + + + + + + + + + + + + Text + + + Text + + + + + + + + + + + + + + + + + + + + + + text + + + + + + + + + + + + + Text + + + Text + + + + + + + + + + + + + + +