mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-11 17:34:09 +02:00
start a restructure
This commit is contained in:
44
Structural/Composite/Form.php
Normal file
44
Structural/Composite/Form.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* The composite node MUST extend the component contract. This is mandatory for building
|
||||
* a tree of components.
|
||||
*/
|
||||
class Form extends FormElement
|
||||
{
|
||||
/**
|
||||
* @var array|FormElement[]
|
||||
*/
|
||||
protected $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
|
||||
*
|
||||
* @param int $indent
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render($indent = 0)
|
||||
{
|
||||
$formCode = '';
|
||||
|
||||
foreach ($this->elements as $element) {
|
||||
$formCode .= $element->render($indent + 1) . PHP_EOL;
|
||||
}
|
||||
|
||||
return $formCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormElement $element
|
||||
*/
|
||||
public function addElement(FormElement $element)
|
||||
{
|
||||
$this->elements[] = $element;
|
||||
}
|
||||
}
|
18
Structural/Composite/FormElement.php
Normal file
18
Structural/Composite/FormElement.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* Class FormElement
|
||||
*/
|
||||
abstract class FormElement
|
||||
{
|
||||
/**
|
||||
* renders the elements' code
|
||||
*
|
||||
* @param int $indent
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function render($indent = 0);
|
||||
}
|
21
Structural/Composite/InputElement.php
Normal file
21
Structural/Composite/InputElement.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* Class InputElement
|
||||
*/
|
||||
class InputElement extends FormElement
|
||||
{
|
||||
/**
|
||||
* renders the input element HTML
|
||||
*
|
||||
* @param int $indent
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function render($indent = 0)
|
||||
{
|
||||
return str_repeat(' ', $indent) . '<input type="text" />';
|
||||
}
|
||||
}
|
12
Structural/Composite/README.md
Normal file
12
Structural/Composite/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# Composite
|
||||
|
||||
# Purpose
|
||||
|
||||
To treat a group of objects the same way as a single instance of the object.
|
||||
|
||||
# Examples
|
||||
|
||||
* a form class instance handles all its form elements like a single instance of the form, when `render()` is called, it
|
||||
subsequently runs trough all its child elements and calls `render()` on them
|
||||
* `Zend_Config`: a tree of configuration options, each one is a `Zend_Config` object itself
|
||||
|
34
Structural/Composite/Test/FormTest.php
Normal file
34
Structural/Composite/Test/FormTest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Test\Composite;
|
||||
|
||||
use DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* FormTest tests the composite pattern on Form
|
||||
*/
|
||||
class FormTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
$form = new Composite\Form();
|
||||
$form->addElement(new Composite\TextElement());
|
||||
$form->addElement(new Composite\InputElement());
|
||||
$embed = new Composite\Form();
|
||||
$embed->addElement(new Composite\TextElement());
|
||||
$embed->addElement(new Composite\InputElement());
|
||||
$form->addElement($embed); // here we have a embedded form (like SF2 does)
|
||||
|
||||
$this->assertRegExp('#^\s{4}#m', $form->render());
|
||||
}
|
||||
|
||||
/**
|
||||
* The all point of this pattern, a Composite must inherit from the node
|
||||
* if you want to builld trees
|
||||
*/
|
||||
public function testFormImplementsFormEelement()
|
||||
{
|
||||
$this->assertTrue(is_subclass_of('DesignPatterns\Composite\Form', 'DesignPatterns\Composite\FormElement'));
|
||||
}
|
||||
}
|
21
Structural/Composite/TextElement.php
Normal file
21
Structural/Composite/TextElement.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* Class TextElement
|
||||
*/
|
||||
class TextElement extends FormElement
|
||||
{
|
||||
/**
|
||||
* renders the text element
|
||||
*
|
||||
* @param int $indent
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function render($indent = 0)
|
||||
{
|
||||
return str_repeat(' ', $indent) . 'this is a text element';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user