Merge branch 'decorator-the-right-way'

This commit is contained in:
Trismegiste
2013-05-11 02:50:42 +02:00
6 changed files with 110 additions and 49 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace DesignPatterns;
namespace DesignPatterns\Decorator;
/**
* Decorator pattern
@@ -15,60 +15,25 @@ namespace DesignPatterns;
*
*/
interface Renderer
{
public function renderData();
}
class Webservice implements Renderer
{
protected $_data;
public function __construct($data)
{
$this->_data = $data;
}
public function renderData()
{
return $this->_data;
}
}
abstract class Decorator
/**
* the Deoorator MUST implement the Renderer 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
{
protected $_wrapped;
public function __construct($wrappable)
/**
* You must type-hint the wrapped component :
* It ensures you can call renderData() in the subclasses !
*
* @param Renderer $wrappable
*/
public function __construct(Renderer $wrappable)
{
$this->_wrapped = $wrappable;
}
}
class RenderInJson extends Decorator implements Renderer
{
public function renderData()
{
$output = $this->_wrapped->renderData();
return json_encode($output);
}
}
class RenderInXml extends Decorator implements Renderer
{
public function renderData()
{
$output = $this->_wrapped->renderData();
// do some fany conversion to xml from array ...
return simplexml_load_string($output);
}
}
// Create a normal service
$service = new Webservice(array('foo' => 'bar'));
// Wrap service with a JSON decorator for renderers
$service = new RenderInJson($service);
// Our Renderer will now output JSON instead of an array
echo $service->renderData();

View File

@@ -0,0 +1,12 @@
<?php
namespace DesignPatterns\Decorator;
class RenderInJson extends Decorator
{
public function renderData()
{
$output = $this->_wrapped->renderData();
return json_encode($output);
}
}

18
Decorator/RenderInXml.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace DesignPatterns\Decorator;
class RenderInXml extends Decorator
{
public function renderData()
{
$output = $this->_wrapped->renderData();
// do some fany conversion to xml from array ...
$doc = new \DOMDocument();
foreach ($output as $key => $val) {
$doc->appendChild($doc->createElement('foo', 'bar'));
}
return $doc->saveXML();
}
}

8
Decorator/Renderer.php Normal file
View File

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

18
Decorator/Webservice.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace DesignPatterns\Decorator;
class Webservice implements Renderer
{
protected $_data;
public function __construct($data)
{
$this->_data = $data;
}
public function renderData()
{
return $this->_data;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Tests\Decorator;
use DesignPatterns\Decorator;
/**
* DecoratorTest tests the decorator pattern
*/
class DecoratorTest extends \PHPUnit_Framework_TestCase
{
protected $service;
protected function setUp()
{
$this->service = new Decorator\Webservice(array('foo' => 'bar'));
}
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());
}
public function testXmlDecorator()
{
// Wrap service with a JSON decorator for renderers
$service = new Decorator\RenderInXml($this->service);
// Our Renderer will now output XML instead of an array
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><foo>bar</foo>', $service->renderData());
}
}