PHP7 Decorator

This commit is contained in:
Dominik Liebler
2016-09-22 21:16:43 +02:00
parent 243456b2da
commit 1f9348d9a8
11 changed files with 81 additions and 161 deletions

View File

@@ -1,31 +0,0 @@
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* the Decorator MUST implement the RendererInterface contract, this is the key-feature
* of this design pattern. If not, this is no longer a Decorator but just a dumb
* wrapper.
*/
/**
* class Decorator.
*/
abstract class Decorator implements RendererInterface
{
/**
* @var RendererInterface
*/
protected $wrapped;
/**
* You must type-hint the wrapped component :
* It ensures you can call renderData() in the subclasses !
*
* @param RendererInterface $wrappable
*/
public function __construct(RendererInterface $wrappable)
{
$this->wrapped = $wrappable;
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace DesignPatterns\Structural\Decorator;
class JsonRenderer extends RendererDecorator
{
public function renderData(): string
{
return json_encode($this->wrapped->renderData());
}
}

View File

@@ -25,9 +25,9 @@ Code
You can also find these code on `GitHub`_
RendererInterface.php
RenderableInterface.php
.. literalinclude:: RendererInterface.php
.. literalinclude:: RenderableInterface.php
:language: php
:linenos:
@@ -37,21 +37,21 @@ Webservice.php
:language: php
:linenos:
Decorator.php
RendererDecorator.php
.. literalinclude:: Decorator.php
.. literalinclude:: RendererDecorator.php
:language: php
:linenos:
RenderInXml.php
XmlRenderer.php
.. literalinclude:: RenderInXml.php
.. literalinclude:: XmlRenderer.php
:language: php
:linenos:
RenderInJson.php
JsonRenderer.php
.. literalinclude:: RenderInJson.php
.. literalinclude:: JsonRenderer.php
:language: php
:linenos:

View File

@@ -1,16 +0,0 @@
<?php
namespace DesignPatterns\Structural\Decorator;
class RenderInJson extends Decorator
{
/**
* render data as JSON.
*
* @return string
*/
public function renderData()
{
return json_encode($this->wrapped->renderData());
}
}

View File

@@ -1,24 +0,0 @@
<?php
namespace DesignPatterns\Structural\Decorator;
class RenderInXml extends Decorator
{
/**
* render data as XML.
*
* @return string
*/
public function renderData()
{
// do some fancy conversion to xml from array ...
$doc = new \DOMDocument();
foreach ($this->wrapped->renderData() as $key => $val) {
$doc->appendChild($doc->createElement($key, $val));
}
return $doc->saveXML();
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace DesignPatterns\Structural\Decorator;
interface RenderableInterface
{
public function renderData(): string;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace DesignPatterns\Structural\Decorator;
/**
* the Decorator MUST implement the RendererInterface contract, this is the key-feature
* of this design pattern. If not, this is no longer a Decorator but just a dumb
* wrapper.
*/
abstract class RendererDecorator
{
/**
* @var RenderableInterface
*/
protected $wrapped;
/**
* @param RenderableInterface $renderer
*/
public function __construct(RenderableInterface $renderer)
{
$this->wrapped = $renderer;
}
}

View File

@@ -1,13 +0,0 @@
<?php
namespace DesignPatterns\Structural\Decorator;
interface RendererInterface
{
/**
* render data.
*
* @return string
*/
public function renderData();
}

View File

@@ -4,77 +4,29 @@ namespace DesignPatterns\Structural\Decorator\Tests;
use DesignPatterns\Structural\Decorator;
/**
* DecoratorTest tests the decorator pattern.
*/
class DecoratorTest extends \PHPUnit_Framework_TestCase
{
protected $service;
/**
* @var Decorator\Webservice
*/
private $service;
protected function setUp()
{
$this->service = new Decorator\Webservice(array('foo' => 'bar'));
$this->service = new Decorator\Webservice('foobar');
}
public function testJsonDecorator()
{
// Wrap service with a JSON decorator for renderers
$service = new Decorator\RenderInJson($this->service);
// Our Renderer will now output JSON instead of an array
$this->assertEquals('{"foo":"bar"}', $service->renderData());
$service = new Decorator\JsonRenderer($this->service);
$this->assertEquals('"foobar"', $service->renderData());
}
public function testXmlDecorator()
{
// Wrap service with a XML decorator for renderers
$service = new Decorator\RenderInXml($this->service);
// Our Renderer will now output XML instead of an array
$xml = '<?xml version="1.0"?><foo>bar</foo>';
$this->assertXmlStringEqualsXmlString($xml, $service->renderData());
}
$service = new Decorator\XmlRenderer($this->service);
/**
* The first key-point of this pattern :.
*/
public function testDecoratorMustImplementsRenderer()
{
$className = 'DesignPatterns\Structural\Decorator\Decorator';
$interfaceName = 'DesignPatterns\Structural\Decorator\RendererInterface';
$this->assertTrue(is_subclass_of($className, $interfaceName));
}
/**
* Second key-point of this pattern : the decorator is type-hinted.
*
* @expectedException \PHPUnit_Framework_Error
*/
public function testDecoratorTypeHinted()
{
if (version_compare(PHP_VERSION, '7', '>=')) {
throw new \PHPUnit_Framework_Error('Skip test for PHP 7', 0, __FILE__, __LINE__);
}
$this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array(new \stdClass()));
}
/**
* Second key-point of this pattern : the decorator is type-hinted.
*
* @requires PHP 7
* @expectedException TypeError
*/
public function testDecoratorTypeHintedForPhp7()
{
$this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array(new \stdClass()));
}
/**
* The decorator implements and wraps the same interface.
*/
public function testDecoratorOnlyAcceptRenderer()
{
$mock = $this->createMock('DesignPatterns\Structural\Decorator\RendererInterface');
$dec = $this->getMockForAbstractClass('DesignPatterns\Structural\Decorator\Decorator', array($mock));
$this->assertNotNull($dec);
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><content>foobar</content>', $service->renderData());
}
}

View File

@@ -2,25 +2,19 @@
namespace DesignPatterns\Structural\Decorator;
class Webservice implements RendererInterface
class Webservice implements RenderableInterface
{
/**
* @var mixed
* @var string
*/
protected $data;
private $data;
/**
* @param mixed $data
*/
public function __construct($data)
public function __construct(string $data)
{
$this->data = $data;
}
/**
* @return string
*/
public function renderData()
public function renderData(): string
{
return $this->data;
}

View File

@@ -0,0 +1,15 @@
<?php
namespace DesignPatterns\Structural\Decorator;
class XmlRenderer extends RendererDecorator
{
public function renderData(): string
{
$doc = new \DOMDocument();
$data = $this->wrapped->renderData();
$doc->appendChild($doc->createElement('content', $data));
return $doc->saveXML();
}
}