fix PSR-0

This commit is contained in:
Trismegiste 2013-05-11 02:50:05 +02:00
parent cfa9014906
commit 55017fb43f
5 changed files with 56 additions and 46 deletions

View File

@ -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();

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;
}
}