mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-24 23:39:37 +02:00
PSR-0 with tests
This commit is contained in:
parent
5f457c2701
commit
6443fecebc
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace DesignPatterns;
|
namespace DesignPatterns\Composite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* composite pattern
|
* composite pattern
|
||||||
@ -41,29 +41,3 @@ class Form
|
|||||||
$this->_elements[] = $element;
|
$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();
|
||||||
|
}
|
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()
|
||||||
|
{
|
||||||
|
return '<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()
|
||||||
|
{
|
||||||
|
return 'this is a text element';
|
||||||
|
}
|
||||||
|
}
|
25
Tests/Composite/FormTest.php
Normal file
25
Tests/Composite/FormTest.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?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());
|
||||||
|
echo $form->render();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user