Skip to content

Commit 79f94ba

Browse files
author
Dominik Liebler
committed
cs Decorator
1 parent 9b3389b commit 79f94ba

File tree

8 files changed

+66
-32
lines changed

8 files changed

+66
-32
lines changed
File renamed without changes.

Decorator/Decorator.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616
*/
1717

1818
/**
19-
* the Deoorator MUST implement the Renderer contract, this is the key-feature
19+
* the Deoorator MUST implement the RendererInterface contract, this is the key-feature
2020
* of this design pattern. If not, this is no longer a Decorator but just a dumb
2121
* wrapper.
2222
*/
23-
abstract class Decorator implements Renderer
23+
abstract class Decorator implements RendererInterface
2424
{
25-
26-
protected $_wrapped;
25+
/**
26+
* @var RendererInterface
27+
*/
28+
protected $wrapped;
2729

2830
/**
2931
* You must type-hint the wrapped component :
3032
* It ensures you can call renderData() in the subclasses !
3133
*
32-
* @param Renderer $wrappable
34+
* @param RendererInterface $wrappable
3335
*/
34-
public function __construct(Renderer $wrappable)
36+
public function __construct(RendererInterface $wrappable)
3537
{
36-
$this->_wrapped = $wrappable;
38+
$this->wrapped = $wrappable;
3739
}
3840
}
39-

Decorator/RenderInJson.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@
22

33
namespace DesignPatterns\Decorator;
44

5+
/**
6+
* Class RenderInJson
7+
*/
58
class RenderInJson extends Decorator
69
{
7-
10+
/**
11+
* render data as JSON
12+
*
13+
* @return mixed|string
14+
*/
815
public function renderData()
916
{
10-
$output = $this->_wrapped->renderData();
17+
$output = $this->wrapped->renderData();
18+
1119
return json_encode($output);
1220
}
13-
1421
}

Decorator/RenderInXml.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
namespace DesignPatterns\Decorator;
44

5+
/**
6+
* Class RenderInXml
7+
*/
58
class RenderInXml extends Decorator
69
{
7-
10+
/**
11+
* render data as XML
12+
*
13+
* @return mixed|string
14+
*/
815
public function renderData()
916
{
10-
$output = $this->_wrapped->renderData();
11-
// do some fany conversion to xml from array ...
17+
$output = $this->wrapped->renderData();
18+
19+
// do some fancy conversion to xml from array ...
20+
1221
$doc = new \DOMDocument();
22+
1323
foreach ($output as $key => $val) {
1424
$doc->appendChild($doc->createElement('foo', 'bar'));
1525
}
1626

1727
return $doc->saveXML();
1828
}
19-
2029
}

Decorator/Renderer.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

Decorator/RendererInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DesignPatterns\Decorator;
4+
5+
/**
6+
* Class RendererInterface
7+
*/
8+
interface RendererInterface
9+
{
10+
/**
11+
* render data
12+
*
13+
* @return mixed
14+
*/
15+
public function renderData();
16+
}

Decorator/Webservice.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
namespace DesignPatterns\Decorator;
44

5-
class Webservice implements Renderer
5+
/**
6+
* Class Webservice
7+
*/
8+
class Webservice implements RendererInterface
69
{
10+
/**
11+
* @var mixed
12+
*/
13+
protected $data;
714

8-
protected $_data;
9-
15+
/**
16+
* @param mixed $data
17+
*/
1018
public function __construct($data)
1119
{
12-
$this->_data = $data;
20+
$this->data = $data;
1321
}
1422

23+
/**
24+
* @return string
25+
*/
1526
public function renderData()
1627
{
17-
return $this->_data;
28+
return $this->data;
1829
}
19-
2030
}

Tests/Decorator/DecoratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testXmlDecorator()
4242
*/
4343
public function testDecoratorMustImplementsRenderer()
4444
{
45-
$this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\Renderer'));
45+
$this->assertTrue(is_subclass_of('DesignPatterns\Decorator\Decorator', 'DesignPatterns\Decorator\RendererInterface'));
4646
}
4747

4848
/**
@@ -60,7 +60,7 @@ public function testDecoratorTypeHinted()
6060
*/
6161
public function testDecoratorOnlyAcceptRenderer()
6262
{
63-
$mock = $this->getMock('DesignPatterns\Decorator\Renderer');
63+
$mock = $this->getMock('DesignPatterns\Decorator\RendererInterface');
6464
$dec = $this->getMockForAbstractClass('DesignPatterns\Decorator\Decorator', array($mock));
6565
$this->assertNotNull($dec);
6666
}

0 commit comments

Comments
 (0)