mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
fix PSR-0
This commit is contained in:
parent
cfa9014906
commit
55017fb43f
@ -15,25 +15,6 @@ namespace DesignPatterns\Decorator;
|
||||
*
|
||||
*/
|
||||
|
||||
interface Renderer
|
||||
{
|
||||
public function renderData();
|
||||
}
|
||||
|
||||
class Webservice implements Renderer
|
||||
{
|
||||
protected $_data;
|
||||
|
||||
public function __construct($data)
|
||||
{
|
||||
$this->_data = $data;
|
||||
}
|
||||
|
||||
public function renderData()
|
||||
{
|
||||
return $this->_data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* the Deoorator MUST implement the Renderer contract, this is the key-feature
|
||||
@ -56,30 +37,3 @@ abstract class Decorator implements Renderer
|
||||
}
|
||||
}
|
||||
|
||||
class RenderInJson extends Decorator
|
||||
{
|
||||
public function renderData()
|
||||
{
|
||||
$output = $this->_wrapped->renderData();
|
||||
return json_encode($output);
|
||||
}
|
||||
}
|
||||
|
||||
class RenderInXml extends Decorator
|
||||
{
|
||||
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();
|
||||
|
12
Decorator/RenderInJson.php
Normal file
12
Decorator/RenderInJson.php
Normal 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
18
Decorator/RenderInXml.php
Normal 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
8
Decorator/Renderer.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Decorator;
|
||||
|
||||
interface Renderer
|
||||
{
|
||||
public function renderData();
|
||||
}
|
18
Decorator/Webservice.php
Normal file
18
Decorator/Webservice.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user