mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 09:11:17 +02:00
PHP7 Decorator
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
11
Structural/Decorator/JsonRenderer.php
Normal file
11
Structural/Decorator/JsonRenderer.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Decorator;
|
||||
|
||||
class JsonRenderer extends RendererDecorator
|
||||
{
|
||||
public function renderData(): string
|
||||
{
|
||||
return json_encode($this->wrapped->renderData());
|
||||
}
|
||||
}
|
@@ -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:
|
||||
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
8
Structural/Decorator/RenderableInterface.php
Normal file
8
Structural/Decorator/RenderableInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Decorator;
|
||||
|
||||
interface RenderableInterface
|
||||
{
|
||||
public function renderData(): string;
|
||||
}
|
24
Structural/Decorator/RendererDecorator.php
Normal file
24
Structural/Decorator/RendererDecorator.php
Normal 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;
|
||||
}
|
||||
}
|
@@ -1,13 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Structural\Decorator;
|
||||
|
||||
interface RendererInterface
|
||||
{
|
||||
/**
|
||||
* render data.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function renderData();
|
||||
}
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
|
15
Structural/Decorator/XmlRenderer.php
Normal file
15
Structural/Decorator/XmlRenderer.php
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user