mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 13:07:27 +02:00
cs Decorator
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
interface Renderer
|
||||
{
|
||||
|
||||
public function renderData();
|
||||
}
|
16
Decorator/RendererInterface.php
Normal file
16
Decorator/RendererInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
/**
|
||||
* Class RendererInterface
|
||||
*/
|
||||
interface RendererInterface
|
||||
{
|
||||
/**
|
||||
* render data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function renderData();
|
||||
}
|
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user