cs Decorator

This commit is contained in:
Dominik Liebler
2013-09-11 16:18:40 +02:00
parent 9b3389ba4a
commit 79f94ba501
8 changed files with 66 additions and 32 deletions

View File

@@ -16,24 +16,25 @@ namespace DesignPatterns\Decorator;
*/
/**
* the Deoorator MUST implement the Renderer contract, this is the key-feature
* the Deoorator 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 Decorator implements Renderer
abstract class Decorator implements RendererInterface
{
protected $_wrapped;
/**
* @var RendererInterface
*/
protected $wrapped;
/**
* You must type-hint the wrapped component :
* It ensures you can call renderData() in the subclasses !
*
* @param Renderer $wrappable
* @param RendererInterface $wrappable
*/
public function __construct(Renderer $wrappable)
public function __construct(RendererInterface $wrappable)
{
$this->_wrapped = $wrappable;
$this->wrapped = $wrappable;
}
}

View File

@@ -2,13 +2,20 @@
namespace DesignPatterns\Decorator;
/**
* Class RenderInJson
*/
class RenderInJson extends Decorator
{
/**
* render data as JSON
*
* @return mixed|string
*/
public function renderData()
{
$output = $this->_wrapped->renderData();
$output = $this->wrapped->renderData();
return json_encode($output);
}
}

View File

@@ -2,19 +2,28 @@
namespace DesignPatterns\Decorator;
/**
* Class RenderInXml
*/
class RenderInXml extends Decorator
{
/**
* render data as XML
*
* @return mixed|string
*/
public function renderData()
{
$output = $this->_wrapped->renderData();
// do some fany conversion to xml from array ...
$output = $this->wrapped->renderData();
// do some fancy conversion to xml from array ...
$doc = new \DOMDocument();
foreach ($output as $key => $val) {
$doc->appendChild($doc->createElement('foo', 'bar'));
}
return $doc->saveXML();
}
}

View File

@@ -1,9 +0,0 @@
<?php
namespace DesignPatterns\Decorator;
interface Renderer
{
public function renderData();
}

View File

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

View File

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

View File

@@ -42,7 +42,7 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase
*/
public function testDecoratorMustImplementsRenderer()
{
$this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\Renderer'));
$this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\RendererInterface'));
}
/**
@@ -60,7 +60,7 @@ class DecoratorTest extends \PHPUnit_Framework_TestCase
*/
public function testDecoratorOnlyAcceptRenderer()
{
$mock = $this->getMock('DesignPatterns\Decorator\Renderer');
$mock = $this->getMock('DesignPatterns\Decorator\RendererInterface');
$dec = $this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array($mock));
$this->assertNotNull($dec);
}