mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-23 09:12:34 +01:00
38 lines
879 B
PHP
38 lines
879 B
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Structural\Composite;
|
|
|
|
/**
|
|
* The composite node MUST extend the component contract. This is mandatory for building
|
|
* a tree of components.
|
|
*/
|
|
class Form implements Renderable
|
|
{
|
|
/**
|
|
* @var Renderable[]
|
|
*/
|
|
private array $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
|
|
*/
|
|
public function render(): string
|
|
{
|
|
$formCode = '<form>';
|
|
|
|
foreach ($this->elements as $element) {
|
|
$formCode .= $element->render();
|
|
}
|
|
|
|
return $formCode . '</form>';
|
|
}
|
|
|
|
public function addElement(Renderable $element)
|
|
{
|
|
$this->elements[] = $element;
|
|
}
|
|
}
|