mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-09 07:55:26 +02:00
45 lines
984 B
PHP
45 lines
984 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Structural\Composite;
|
|
|
|
/**
|
|
* The composite node MUST extend the component contract. This is mandatory for building
|
|
* a tree of components.
|
|
*/
|
|
class Form implements RenderableInterface
|
|
{
|
|
/**
|
|
* @var RenderableInterface[]
|
|
*/
|
|
private $elements;
|
|
|
|
/**
|
|
* runs through all elements and calls render() on them, then returns the complete representation
|
|
* of the form.
|
|
*
|
|
* from the outside, one will not see this and the form will act like a single object instance
|
|
*
|
|
* @return string
|
|
*/
|
|
public function render(): string
|
|
{
|
|
$formCode = '<form>';
|
|
|
|
foreach ($this->elements as $element) {
|
|
$formCode .= $element->render();
|
|
}
|
|
|
|
$formCode .= '</form>';
|
|
|
|
return $formCode;
|
|
}
|
|
|
|
/**
|
|
* @param RenderableInterface $element
|
|
*/
|
|
public function addElement(RenderableInterface $element)
|
|
{
|
|
$this->elements[] = $element;
|
|
}
|
|
}
|