diff --git a/Command/Command.php b/Command/CommandInterface.php similarity index 100% rename from Command/Command.php rename to Command/CommandInterface.php diff --git a/Decorator/Decorator.php b/Decorator/Decorator.php index 74d526d..88c1b84 100644 --- a/Decorator/Decorator.php +++ b/Decorator/Decorator.php @@ -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; } } - diff --git a/Decorator/RenderInJson.php b/Decorator/RenderInJson.php index 0c521bd..0edc7ea 100644 --- a/Decorator/RenderInJson.php +++ b/Decorator/RenderInJson.php @@ -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); } - } diff --git a/Decorator/RenderInXml.php b/Decorator/RenderInXml.php index 599c2f5..5967b98 100644 --- a/Decorator/RenderInXml.php +++ b/Decorator/RenderInXml.php @@ -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(); } - } diff --git a/Decorator/Renderer.php b/Decorator/Renderer.php deleted file mode 100644 index af3c46b..0000000 --- a/Decorator/Renderer.php +++ /dev/null @@ -1,9 +0,0 @@ -_data = $data; + $this->data = $data; } + /** + * @return string + */ public function renderData() { - return $this->_data; + return $this->data; } - } diff --git a/Tests/Decorator/DecoratorTest.php b/Tests/Decorator/DecoratorTest.php index fce9e56..7f20bb6 100644 --- a/Tests/Decorator/DecoratorTest.php +++ b/Tests/Decorator/DecoratorTest.php @@ -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); }