PHP7 Composite

This commit is contained in:
Dominik Liebler 2016-09-22 20:44:28 +02:00
parent 72f32359c6
commit 320dc3c6bf
7 changed files with 41 additions and 63 deletions

View File

@ -6,12 +6,12 @@ namespace DesignPatterns\Structural\Composite;
* The composite node MUST extend the component contract. This is mandatory for building * The composite node MUST extend the component contract. This is mandatory for building
* a tree of components. * a tree of components.
*/ */
class Form extends FormElement class Form implements RenderableInterface
{ {
/** /**
* @var array|FormElement[] * @var RenderableInterface[]
*/ */
protected $elements; private $elements;
/** /**
* runs through all elements and calls render() on them, then returns the complete representation * runs through all elements and calls render() on them, then returns the complete representation
@ -19,25 +19,25 @@ class Form extends FormElement
* *
* from the outside, one will not see this and the form will act like a single object instance * from the outside, one will not see this and the form will act like a single object instance
* *
* @param int $indent
*
* @return string * @return string
*/ */
public function render($indent = 0) public function render(): string
{ {
$formCode = ''; $formCode = '<form>';
foreach ($this->elements as $element) { foreach ($this->elements as $element) {
$formCode .= $element->render($indent + 1).PHP_EOL; $formCode .= $element->render();
} }
$formCode .= '</form>';
return $formCode; return $formCode;
} }
/** /**
* @param FormElement $element * @param RenderableInterface $element
*/ */
public function addElement(FormElement $element) public function addElement(RenderableInterface $element)
{ {
$this->elements[] = $element; $this->elements[] = $element;
} }

View File

@ -1,15 +0,0 @@
<?php
namespace DesignPatterns\Structural\Composite;
abstract class FormElement
{
/**
* renders the elements' code.
*
* @param int $indent
*
* @return mixed
*/
abstract public function render($indent = 0);
}

View File

@ -2,17 +2,10 @@
namespace DesignPatterns\Structural\Composite; namespace DesignPatterns\Structural\Composite;
class InputElement extends FormElement class InputElement implements RenderableInterface
{ {
/** public function render(): string
* renders the input element HTML.
*
* @param int $indent
*
* @return mixed|string
*/
public function render($indent = 0)
{ {
return str_repeat(' ', $indent).'<input type="text" />'; return '<input type="text" />';
} }
} }

View File

@ -28,9 +28,9 @@ Code
You can also find these code on `GitHub`_ You can also find these code on `GitHub`_
FormElement.php RenderableInterface.php
.. literalinclude:: FormElement.php .. literalinclude:: RenderableInterface.php
:language: php :language: php
:linenos: :linenos:

View File

@ -0,0 +1,8 @@
<?php
namespace DesignPatterns\Structural\Composite;
interface RenderableInterface
{
public function render(): string;
}

View File

@ -4,32 +4,21 @@ namespace DesignPatterns\Structural\Composite\Tests;
use DesignPatterns\Structural\Composite; use DesignPatterns\Structural\Composite;
/**
* FormTest tests the composite pattern on Form.
*/
class CompositeTest extends \PHPUnit_Framework_TestCase class CompositeTest extends \PHPUnit_Framework_TestCase
{ {
public function testRender() public function testRender()
{ {
$form = new Composite\Form(); $form = new Composite\Form();
$form->addElement(new Composite\TextElement()); $form->addElement(new Composite\TextElement('Email:'));
$form->addElement(new Composite\InputElement()); $form->addElement(new Composite\InputElement());
$embed = new Composite\Form(); $embed = new Composite\Form();
$embed->addElement(new Composite\TextElement()); $embed->addElement(new Composite\TextElement('Password:'));
$embed->addElement(new Composite\InputElement()); $embed->addElement(new Composite\InputElement());
$form->addElement($embed); // here we have a embedded form (like SF2 does) $form->addElement($embed);
$this->assertRegExp('#^\s{4}#m', $form->render()); $this->assertEquals(
} '<form>Email:<input type="text" /><form>Password:<input type="text" /></form></form>',
$form->render()
/** );
* The point of this pattern, a Composite must inherit from the node
* if you want to build trees.
*/
public function testFormImplementsFormEelement()
{
$className = 'DesignPatterns\Structural\Composite\Form';
$abstractName = 'DesignPatterns\Structural\Composite\FormElement';
$this->assertTrue(is_subclass_of($className, $abstractName));
} }
} }

View File

@ -2,17 +2,20 @@
namespace DesignPatterns\Structural\Composite; namespace DesignPatterns\Structural\Composite;
class TextElement extends FormElement class TextElement implements RenderableInterface
{ {
/** /**
* renders the text element. * @var string
*
* @param int $indent
*
* @return mixed|string
*/ */
public function render($indent = 0) private $text;
public function __construct(string $text)
{ {
return str_repeat(' ', $indent).'this is a text element'; $this->text = $text;
}
public function render(): string
{
return $this->text;
} }
} }