mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns;
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
/**
|
||||
* Decorator pattern
|
||||
@@ -14,61 +14,26 @@ namespace DesignPatterns;
|
||||
* course)
|
||||
*
|
||||
*/
|
||||
|
||||
interface Renderer
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
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
|
||||
{
|
||||
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 fancy 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();
|
||||
|
14
Decorator/RenderInJson.php
Normal file
14
Decorator/RenderInJson.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
class RenderInJson extends Decorator
|
||||
{
|
||||
|
||||
public function renderData()
|
||||
{
|
||||
$output = $this->_wrapped->renderData();
|
||||
return json_encode($output);
|
||||
}
|
||||
|
||||
}
|
20
Decorator/RenderInXml.php
Normal file
20
Decorator/RenderInXml.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
}
|
9
Decorator/Renderer.php
Normal file
9
Decorator/Renderer.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
interface Renderer
|
||||
{
|
||||
|
||||
public function renderData();
|
||||
}
|
20
Decorator/Webservice.php
Normal file
20
Decorator/Webservice.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
class Webservice implements Renderer
|
||||
{
|
||||
|
||||
protected $_data;
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->_data = $data;
|
||||
}
|
||||
|
||||
public function renderData()
|
||||
{
|
||||
return $this->_data;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user