File tree 8 files changed +66
-32
lines changed
8 files changed +66
-32
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 16
16
*/
17
17
18
18
/**
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
20
20
* of this design pattern. If not, this is no longer a Decorator but just a dumb
21
21
* wrapper.
22
22
*/
23
- abstract class Decorator implements Renderer
23
+ abstract class Decorator implements RendererInterface
24
24
{
25
-
26
- protected $ _wrapped ;
25
+ /**
26
+ * @var RendererInterface
27
+ */
28
+ protected $ wrapped ;
27
29
28
30
/**
29
31
* You must type-hint the wrapped component :
30
32
* It ensures you can call renderData() in the subclasses !
31
33
*
32
- * @param Renderer $wrappable
34
+ * @param RendererInterface $wrappable
33
35
*/
34
- public function __construct (Renderer $ wrappable )
36
+ public function __construct (RendererInterface $ wrappable )
35
37
{
36
- $ this ->_wrapped = $ wrappable ;
38
+ $ this ->wrapped = $ wrappable ;
37
39
}
38
40
}
39
-
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Decorator ;
4
4
5
+ /**
6
+ * Class RenderInJson
7
+ */
5
8
class RenderInJson extends Decorator
6
9
{
7
-
10
+ /**
11
+ * render data as JSON
12
+ *
13
+ * @return mixed|string
14
+ */
8
15
public function renderData ()
9
16
{
10
- $ output = $ this ->_wrapped ->renderData ();
17
+ $ output = $ this ->wrapped ->renderData ();
18
+
11
19
return json_encode ($ output );
12
20
}
13
-
14
21
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Decorator ;
4
4
5
+ /**
6
+ * Class RenderInXml
7
+ */
5
8
class RenderInXml extends Decorator
6
9
{
7
-
10
+ /**
11
+ * render data as XML
12
+ *
13
+ * @return mixed|string
14
+ */
8
15
public function renderData ()
9
16
{
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
+
12
21
$ doc = new \DOMDocument ();
22
+
13
23
foreach ($ output as $ key => $ val ) {
14
24
$ doc ->appendChild ($ doc ->createElement ('foo ' , 'bar ' ));
15
25
}
16
26
17
27
return $ doc ->saveXML ();
18
28
}
19
-
20
29
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Decorator ;
4
4
5
- class Webservice implements Renderer
5
+ /**
6
+ * Class Webservice
7
+ */
8
+ class Webservice implements RendererInterface
6
9
{
10
+ /**
11
+ * @var mixed
12
+ */
13
+ protected $ data ;
7
14
8
- protected $ _data ;
9
-
15
+ /**
16
+ * @param mixed $data
17
+ */
10
18
public function __construct ($ data )
11
19
{
12
- $ this ->_data = $ data ;
20
+ $ this ->data = $ data ;
13
21
}
14
22
23
+ /**
24
+ * @return string
25
+ */
15
26
public function renderData ()
16
27
{
17
- return $ this ->_data ;
28
+ return $ this ->data ;
18
29
}
19
-
20
30
}
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ public function testXmlDecorator()
42
42
*/
43
43
public function testDecoratorMustImplementsRenderer ()
44
44
{
45
- $ this ->assertTrue (is_subclass_of ('DesignPatterns\Decorator\Decorator ' , 'DesignPatterns\Decorator\Renderer ' ));
45
+ $ this ->assertTrue (is_subclass_of ('DesignPatterns\Decorator\Decorator ' , 'DesignPatterns\Decorator\RendererInterface ' ));
46
46
}
47
47
48
48
/**
@@ -60,7 +60,7 @@ public function testDecoratorTypeHinted()
60
60
*/
61
61
public function testDecoratorOnlyAcceptRenderer ()
62
62
{
63
- $ mock = $ this ->getMock ('DesignPatterns\Decorator\Renderer ' );
63
+ $ mock = $ this ->getMock ('DesignPatterns\Decorator\RendererInterface ' );
64
64
$ dec = $ this ->getMockForAbstractClass ('DesignPatterns\Decorator\Decorator ' , array ($ mock ));
65
65
$ this ->assertNotNull ($ dec );
66
66
}
You can’t perform that action at this time.
0 commit comments