mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 15:06:31 +02:00
start a restructure
This commit is contained in:
31
Structural/Decorator/Decorator.php
Normal file
31
Structural/Decorator/Decorator.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\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;
|
||||
}
|
||||
}
|
10
Structural/Decorator/README.md
Normal file
10
Structural/Decorator/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Decorator
|
||||
|
||||
## Purpose
|
||||
|
||||
To dynamically add new functionality to class instances.
|
||||
|
||||
## Examples
|
||||
|
||||
* Zend Framework: decorators for `Zend_Form_Element` instances
|
||||
* Web Service Layer: Decorators JSON and XML for a REST service (in this case, only one of these should be allowed of course)
|
21
Structural/Decorator/RenderInJson.php
Normal file
21
Structural/Decorator/RenderInJson.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
/**
|
||||
* Class RenderInJson
|
||||
*/
|
||||
class RenderInJson extends Decorator
|
||||
{
|
||||
/**
|
||||
* render data as JSON
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function renderData()
|
||||
{
|
||||
$output = $this->wrapped->renderData();
|
||||
|
||||
return json_encode($output);
|
||||
}
|
||||
}
|
29
Structural/Decorator/RenderInXml.php
Normal file
29
Structural/Decorator/RenderInXml.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
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 fancy conversion to xml from array ...
|
||||
|
||||
$doc = new \DOMDocument();
|
||||
|
||||
foreach ($output as $key => $val) {
|
||||
$doc->appendChild($doc->createElement($key, $val));
|
||||
}
|
||||
|
||||
return $doc->saveXML();
|
||||
}
|
||||
}
|
16
Structural/Decorator/RendererInterface.php
Normal file
16
Structural/Decorator/RendererInterface.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
/**
|
||||
* Class RendererInterface
|
||||
*/
|
||||
interface RendererInterface
|
||||
{
|
||||
/**
|
||||
* render data
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function renderData();
|
||||
}
|
63
Structural/Decorator/Test/DecoratorTest.php
Normal file
63
Structural/Decorator/Test/DecoratorTest.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
/**
|
||||
* The first key-point of this pattern :
|
||||
*/
|
||||
public function testDecoratorMustImplementsRenderer()
|
||||
{
|
||||
$this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\RendererInterface'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Second key-point of this pattern : the decorator is type-hinted
|
||||
*
|
||||
* @expectedException \PHPUnit_Framework_Error
|
||||
*/
|
||||
public function testDecoratorTypeHinted()
|
||||
{
|
||||
$this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array(new \stdClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The decorator implements and wraps the same interface
|
||||
*/
|
||||
public function testDecoratorOnlyAcceptRenderer()
|
||||
{
|
||||
$mock = $this->getMock('DesignPatterns\Decorator\RendererInterface');
|
||||
$dec = $this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array($mock));
|
||||
$this->assertNotNull($dec);
|
||||
}
|
||||
}
|
30
Structural/Decorator/Webservice.php
Normal file
30
Structural/Decorator/Webservice.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
/**
|
||||
* Class Webservice
|
||||
*/
|
||||
class Webservice implements RendererInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function renderData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user