Created
September 2, 2014 14:10
-
-
Save brunoaugusto/af1b7b25687fe3945c0d to your computer and use it in GitHub Desktop.
Composite Design Patter para Stack Overflow PT (http://pt.stackoverflow.com/questions/27424)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components; | |
abstract class AbstractComponent implements Component { | |
/** | |
* Component Childrens | |
* | |
* @var ArrayObject $childrens | |
*/ | |
protected $children; | |
/** | |
* Flag to condition whether or not Component | |
* is a leaf or a Composite | |
* | |
* @var boolean $leaf | |
*/ | |
protected $leaf = FALSE; | |
/** | |
* Component Constructor | |
* | |
* Initialized Components' Children ArrayObject and | |
* sets an ID for the Component | |
*/ | |
public function __construct() { | |
if( ! $this -> leaf ) { | |
$this -> children = new \ArrayObject; | |
} | |
} | |
/** | |
* Adds a new Component | |
* | |
* @param Composite\Components\Component $component | |
* Component to add | |
* | |
* @throws \LogicException | |
* Thrown if trying to add a new Component to a leaf Component | |
*/ | |
public function add( Component $component ) { | |
if( $this -> leaf ) { | |
throw new \LogicException( 'A leaf Component cannot have childrens' ); | |
} | |
$this -> children -> append( $component ); | |
return $this; | |
} | |
// Auxiliary Methods Defintiion | |
/** | |
* Draws Components Childrens | |
*/ | |
protected function drawChildren() { | |
if( ! $this -> leaf ) { | |
$iterator = $this -> children -> getIterator(); | |
for( $iterator -> rewind(); $iterator -> valid(); $iterator -> next() ) { | |
$iterator -> current() -> draw(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components; | |
interface Component extends Drawable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components; | |
interface Drawable { | |
/** | |
* Draws a Component | |
*/ | |
public function draw(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components\HTML; | |
use Composite\Components\AbstractComponent; | |
class Cell extends AbstractComponent { | |
/** | |
* Flag to condition whether or not Component | |
* is a leaf or a COmposite | |
* | |
* A Table-cell is not really a leaf as more HTML | |
* can be added to it, but let's keep it simple | |
* | |
* @var boolean $leaf | |
*/ | |
protected $leaf = TRUE; | |
/** | |
* Cell data | |
* | |
* @var string $data | |
*/ | |
private $data; | |
/** | |
* Cell Constructor | |
*/ | |
public function __construct( $data ) { | |
parent::__construct(); | |
$this -> data = $data; | |
} | |
// Drawable Interface method Implementation | |
public function draw() { | |
echo ' <td>', $this -> data, "</td>\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components\HTML; | |
use Composite\Components\AbstractComponent; | |
class Row extends AbstractComponent { | |
// Drawable Interface method Implementation | |
public function draw() { | |
echo "\n <tr>\n", $this -> drawChildren(), " </tr>\n "; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Composite\Components\HTML; | |
use Composite\Components\AbstractComponent; | |
class Table extends AbstractComponent { | |
// Drawable Interface method Implementation | |
public function draw() { | |
echo "<table>\n ", $this -> drawChildren(), "\n</table>"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<style> | |
table { | |
border-collapse: collapse; | |
width: 100%; | |
} | |
table, table td { | |
border: 1px solid #000; | |
} | |
</style> | |
</head> | |
<body> | |
<?php | |
spl_autoload_register( function( $classname ) { | |
$classname = stream_resolve_include_path( | |
str_replace( '\\', DIRECTORY_SEPARATOR, $classname ) . '.php' | |
); | |
if( $classname !== FALSE ) { | |
include $classname; | |
} | |
}); | |
$groups = array( | |
'Administração' => array( | |
0 => array( | |
'nome' => 'nome0', | |
'ramal' => 'ramal0' | |
), | |
1 => array( | |
'nome' => 'nome1', | |
'ramal' => 'ramal1' | |
), | |
2 => array( | |
'nome' => 'nome2', | |
'ramal' => 'ramal2' | |
), | |
3 => array( | |
'nome' => 'nome3', | |
'ramal' => 'ramal3' | |
) | |
), | |
'Financeiro' => array( | |
4 => array( | |
'nome' => 'nome4', | |
'ramal' => 'ramal4' | |
), | |
5 => array( | |
'nome' => 'nome5', | |
'ramal' => 'ramal5' | |
), | |
6 => array( | |
'nome' => 'nome6', | |
'ramal' => 'ramal6' | |
), | |
7 => array( | |
'nome' => 'nome7', | |
'ramal' => 'ramal7' | |
), | |
8 => array( | |
'nome' => 'nome8', | |
'ramal' => 'ramal8' | |
), | |
9 => array( | |
'nome' => 'nome9', | |
'ramal' => 'ramal9' | |
) | |
) | |
); | |
$tableGroup = new Composite\Components\HTML\Table; | |
foreach( $groups as $group => $persons ) { | |
// Groups | |
$groupRow = new Composite\Components\HTML\Row; | |
$groupRow -> add( new Composite\Components\HTML\CellCol2( $group ) ); | |
//-> add( new Composite\Components\HTML\Cell( '' ) ); | |
$tableGroup -> add( $groupRow ); | |
// Persons | |
foreach( $persons as $person ) { | |
$nameCell = new Composite\Components\HTML\Cell( $person['nome'] ); | |
$dialCell = new Composite\Components\HTML\Cell( $person['ramal'] ); | |
$personRow = new Composite\Components\HTML\Row; | |
$personRow -> add( $nameCell ) -> add( $dialCell ); | |
$tableGroup -> add( $personRow ); | |
} | |
} | |
$tableGroup -> draw(); | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment