File tree 7 files changed +41
-63
lines changed
7 files changed +41
-63
lines changed Original file line number Diff line number Diff line change 6
6
* The composite node MUST extend the component contract. This is mandatory for building
7
7
* a tree of components.
8
8
*/
9
- class Form extends FormElement
9
+ class Form implements RenderableInterface
10
10
{
11
11
/**
12
- * @var array|FormElement []
12
+ * @var RenderableInterface []
13
13
*/
14
- protected $ elements ;
14
+ private $ elements ;
15
15
16
16
/**
17
17
* runs through all elements and calls render() on them, then returns the complete representation
18
18
* of the form.
19
19
*
20
20
* from the outside, one will not see this and the form will act like a single object instance
21
21
*
22
- * @param int $indent
23
- *
24
22
* @return string
25
23
*/
26
- public function render ($ indent = 0 )
24
+ public function render (): string
27
25
{
28
- $ formCode = '' ;
26
+ $ formCode = '<form> ' ;
29
27
30
28
foreach ($ this ->elements as $ element ) {
31
- $ formCode .= $ element ->render ($ indent + 1 ). PHP_EOL ;
29
+ $ formCode .= $ element ->render () ;
32
30
}
33
31
32
+ $ formCode .= '</form> ' ;
33
+
34
34
return $ formCode ;
35
35
}
36
36
37
37
/**
38
- * @param FormElement $element
38
+ * @param RenderableInterface $element
39
39
*/
40
- public function addElement (FormElement $ element )
40
+ public function addElement (RenderableInterface $ element )
41
41
{
42
42
$ this ->elements [] = $ element ;
43
43
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Structural \Composite ;
4
4
5
- class InputElement extends FormElement
5
+ class InputElement implements RenderableInterface
6
6
{
7
- /**
8
- * renders the input element HTML.
9
- *
10
- * @param int $indent
11
- *
12
- * @return mixed|string
13
- */
14
- public function render ($ indent = 0 )
7
+ public function render (): string
15
8
{
16
- return str_repeat ( ' ' , $ indent ). '<input type="text" /> ' ;
9
+ return '<input type="text" /> ' ;
17
10
}
18
11
}
Original file line number Diff line number Diff line change 28
28
29
29
You can also find these code on `GitHub `_
30
30
31
- FormElement .php
31
+ RenderableInterface .php
32
32
33
- .. literalinclude :: FormElement .php
33
+ .. literalinclude :: RenderableInterface .php
34
34
:language: php
35
35
:linenos:
36
36
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DesignPatterns \Structural \Composite ;
4
+
5
+ interface RenderableInterface
6
+ {
7
+ public function render (): string ;
8
+ }
Original file line number Diff line number Diff line change 4
4
5
5
use DesignPatterns \Structural \Composite ;
6
6
7
- /**
8
- * FormTest tests the composite pattern on Form.
9
- */
10
7
class CompositeTest extends \PHPUnit_Framework_TestCase
11
8
{
12
9
public function testRender ()
13
10
{
14
11
$ form = new Composite \Form ();
15
- $ form ->addElement (new Composite \TextElement ());
12
+ $ form ->addElement (new Composite \TextElement (' Email: ' ));
16
13
$ form ->addElement (new Composite \InputElement ());
17
14
$ embed = new Composite \Form ();
18
- $ embed ->addElement (new Composite \TextElement ());
15
+ $ embed ->addElement (new Composite \TextElement (' Password: ' ));
19
16
$ embed ->addElement (new Composite \InputElement ());
20
- $ form ->addElement ($ embed ); // here we have a embedded form (like SF2 does)
17
+ $ form ->addElement ($ embed );
21
18
22
- $ this ->assertRegExp ('#^\s{4}#m ' , $ form ->render ());
23
- }
24
-
25
- /**
26
- * The point of this pattern, a Composite must inherit from the node
27
- * if you want to build trees.
28
- */
29
- public function testFormImplementsFormEelement ()
30
- {
31
- $ className = 'DesignPatterns\Structural\Composite\Form ' ;
32
- $ abstractName = 'DesignPatterns\Structural\Composite\FormElement ' ;
33
- $ this ->assertTrue (is_subclass_of ($ className , $ abstractName ));
19
+ $ this ->assertEquals (
20
+ '<form>Email:<input type="text" /><form>Password:<input type="text" /></form></form> ' ,
21
+ $ form ->render ()
22
+ );
34
23
}
35
24
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace DesignPatterns \Structural \Composite ;
4
4
5
- class TextElement extends FormElement
5
+ class TextElement implements RenderableInterface
6
6
{
7
7
/**
8
- * renders the text element.
9
- *
10
- * @param int $indent
11
- *
12
- * @return mixed|string
8
+ * @var string
13
9
*/
14
- public function render ($ indent = 0 )
10
+ private $ text ;
11
+
12
+ public function __construct (string $ text )
13
+ {
14
+ $ this ->text = $ text ;
15
+ }
16
+
17
+ public function render (): string
15
18
{
16
- return str_repeat ( ' ' , $ indent ). ' this is a text element ' ;
19
+ return $ this -> text ;
17
20
}
18
21
}
You can’t perform that action at this time.
0 commit comments