start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View 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;
}
}

View 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);
}

View 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" />';
}
}

View 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

View 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'));
}
}

View 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';
}
}