mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 14:07:25 +02:00
Merge branch 'composite-the-right-way'
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns;
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
/**
|
||||
* composite pattern
|
||||
@@ -13,8 +13,10 @@ namespace DesignPatterns;
|
||||
* 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
|
||||
*
|
||||
* The composite node MUST extend the component contract. This is mandatory for building
|
||||
* a tree of component.
|
||||
*/
|
||||
class Form
|
||||
class Form extends FormElement
|
||||
{
|
||||
protected $_elements;
|
||||
|
||||
@@ -26,11 +28,11 @@ class Form
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
public function render($indent = 0)
|
||||
{
|
||||
$formCode = '';
|
||||
foreach ($this->_elements as $element) {
|
||||
$formCode .= $element->render();
|
||||
$formCode .= $element->render($indent + 1) . PHP_EOL;
|
||||
}
|
||||
|
||||
return $formCode;
|
||||
@@ -41,29 +43,3 @@ class Form
|
||||
$this->_elements[] = $element;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class FormElement
|
||||
{
|
||||
abstract public function render();
|
||||
}
|
||||
|
||||
class TextElement extends FormElement
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return 'this is a text element';
|
||||
}
|
||||
}
|
||||
|
||||
class InputElement extends FormElement
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
return '<input type="text" />';
|
||||
}
|
||||
}
|
||||
|
||||
$form = new Form();
|
||||
$form->addElement(new TextElement());
|
||||
$form->addElement(new InputElement());
|
||||
echo $form->render();
|
8
Composite/FormElement.php
Normal file
8
Composite/FormElement.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
abstract class FormElement
|
||||
{
|
||||
abstract public function render($indent = 0);
|
||||
}
|
11
Composite/InputElement.php
Normal file
11
Composite/InputElement.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
class InputElement extends FormElement
|
||||
{
|
||||
public function render($indent = 0)
|
||||
{
|
||||
return str_repeat(' ', $indent) . '<input type="text" />';
|
||||
}
|
||||
}
|
11
Composite/TextElement.php
Normal file
11
Composite/TextElement.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Composite;
|
||||
|
||||
class TextElement extends FormElement
|
||||
{
|
||||
public function render($indent = 0)
|
||||
{
|
||||
return str_repeat(' ', $indent) . 'this is a text element';
|
||||
}
|
||||
}
|
30
Tests/Composite/FormTest.php
Normal file
30
Tests/Composite/FormTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user