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
* 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
@@ -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
*
* @param int $indent
*
* @return string
*/
public function render($indent = 0)
public function render(): string
{
$formCode = '';
$formCode = '<form>';
foreach ($this->elements as $element) {
$formCode .= $element->render($indent + 1).PHP_EOL;
$formCode .= $element->render();
}
$formCode .= '</form>';
return $formCode;
}
/**
* @param FormElement $element
* @param RenderableInterface $element
*/
public function addElement(FormElement $element)
public function addElement(RenderableInterface $element)
{
$this->elements[] = $element;
}