Skip to content

Commit 320dc3c

Browse files
author
Dominik Liebler
committed
PHP7 Composite
1 parent 72f3235 commit 320dc3c

File tree

7 files changed

+41
-63
lines changed

7 files changed

+41
-63
lines changed

Structural/Composite/Form.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@
66
* The composite node MUST extend the component contract. This is mandatory for building
77
* a tree of components.
88
*/
9-
class Form extends FormElement
9+
class Form implements RenderableInterface
1010
{
1111
/**
12-
* @var array|FormElement[]
12+
* @var RenderableInterface[]
1313
*/
14-
protected $elements;
14+
private $elements;
1515

1616
/**
1717
* runs through all elements and calls render() on them, then returns the complete representation
1818
* of the form.
1919
*
2020
* from the outside, one will not see this and the form will act like a single object instance
2121
*
22-
* @param int $indent
23-
*
2422
* @return string
2523
*/
26-
public function render($indent = 0)
24+
public function render(): string
2725
{
28-
$formCode = '';
26+
$formCode = '<form>';
2927

3028
foreach ($this->elements as $element) {
31-
$formCode .= $element->render($indent + 1).PHP_EOL;
29+
$formCode .= $element->render();
3230
}
3331

32+
$formCode .= '</form>';
33+
3434
return $formCode;
3535
}
3636

3737
/**
38-
* @param FormElement $element
38+
* @param RenderableInterface $element
3939
*/
40-
public function addElement(FormElement $element)
40+
public function addElement(RenderableInterface $element)
4141
{
4242
$this->elements[] = $element;
4343
}

Structural/Composite/FormElement.php

-15
This file was deleted.

Structural/Composite/InputElement.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,10 @@
22

33
namespace DesignPatterns\Structural\Composite;
44

5-
class InputElement extends FormElement
5+
class InputElement implements RenderableInterface
66
{
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
158
{
16-
return str_repeat(' ', $indent).'<input type="text" />';
9+
return '<input type="text" />';
1710
}
1811
}

Structural/Composite/README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Code
2828

2929
You can also find these code on `GitHub`_
3030

31-
FormElement.php
31+
RenderableInterface.php
3232

33-
.. literalinclude:: FormElement.php
33+
.. literalinclude:: RenderableInterface.php
3434
:language: php
3535
:linenos:
3636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Composite;
4+
5+
interface RenderableInterface
6+
{
7+
public function render(): string;
8+
}

Structural/Composite/Tests/CompositeTest.php

+7-18
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,21 @@
44

55
use DesignPatterns\Structural\Composite;
66

7-
/**
8-
* FormTest tests the composite pattern on Form.
9-
*/
107
class CompositeTest extends \PHPUnit_Framework_TestCase
118
{
129
public function testRender()
1310
{
1411
$form = new Composite\Form();
15-
$form->addElement(new Composite\TextElement());
12+
$form->addElement(new Composite\TextElement('Email:'));
1613
$form->addElement(new Composite\InputElement());
1714
$embed = new Composite\Form();
18-
$embed->addElement(new Composite\TextElement());
15+
$embed->addElement(new Composite\TextElement('Password:'));
1916
$embed->addElement(new Composite\InputElement());
20-
$form->addElement($embed); // here we have a embedded form (like SF2 does)
17+
$form->addElement($embed);
2118

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+
);
3423
}
3524
}

Structural/Composite/TextElement.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace DesignPatterns\Structural\Composite;
44

5-
class TextElement extends FormElement
5+
class TextElement implements RenderableInterface
66
{
77
/**
8-
* renders the text element.
9-
*
10-
* @param int $indent
11-
*
12-
* @return mixed|string
8+
* @var string
139
*/
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
1518
{
16-
return str_repeat(' ', $indent).'this is a text element';
19+
return $this->text;
1720
}
1821
}

0 commit comments

Comments
 (0)