<?php
// define abstract class HTMLElement
abstract class HTMLElement{
protected $attributes;
protected function __construct($attributes){
if(!is_array($attributes)){
throw new Exception('Invalid attribute type');
}
$this->attributes=$attributes;
}
// abstract 'getHTML()' method
abstract protected function getHTML();
}
// define 'Div' class
class Div extends HTMLElement{
private $output='<div ';
private $data;
public function __construct($attributes=array(),$data){
if(!$data instanceof HTMLElement&&!is_string($data)){
throw new Exception('Invalid parameter type');
}
parent::__construct($attributes);
$this->data=$data;
}
// concrete implementation for 'getHTML()' method
public function getHTML(){
foreach($this->attributes as $attribute=>$value){
$this->output.=$attribute.'="'.$value.'" ';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</div>';
return $this->output;
}
}
// define 'Header1' class
class Header1 extends HTMLElement{
private $output='<h1 ';
private $data;
public function __construct($attributes=array(),$data){
if(!$data instanceof HTMLElement&&!is_string($data)){
throw new Exception('Invalid parameter type');
}
parent::__construct($attributes);
$this->data=$data;
}
// concrete implementation for 'getHTML()' method
public function getHTML(){
foreach($this->attributes as $attribute=>$value){
$this->output.=$attribute.'="'.$value.'" ';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</h1>';
return $this->output;
}
}
// define 'Paragraph' class
class Paragraph extends HTMLElement{
private $output='<p ';
private $data;
public function __construct($attributes=array(),$data){
if(!$data instanceof HTMLElement&&!is_string($data)){
throw new Exception('Invalid parameter type');
}
parent::__construct($attributes);
$this->data=$data;
}
// concrete implementation for 'getHTML()' method
public function getHTML(){
foreach($this->attributes as $attribute=>$value){
$this->output.=$attribute.'="'.$value.'" ';
}
$this->output=substr_replace($this->output,'>',-1);
$this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
$this->output.='</p>';
return $this->output;
}
}
// define 'UnorderedList' class
class UnorderedList extends HTMLElement{
private $output='<ul ';
private $items=array();
public function __construct($attributes=array(),$items=array
()){
parent::__construct($attributes);
if(!is_array($items)){
throw new Exception('Invalid parameter for list
items');
}
$this->items=$items;
}
// concrete implementation for 'getHTML()' method
public function getHTML(){
foreach($this->attributes as $attribute=>$value){
$this->output.=$attribute.'="'.$value.'" ';
}
$this->output=substr_replace($this->output,'>',-1);
foreach($this->items as $item){
$this->output.=($item instanceof
HTMLElement)?'<li>'.$item->getHTML ().'</li>':'<li>'.$item.'</li>';
}
$this->output.='</ul>';
return $this->output;
}
}
class PageGenerator{
private $output='';
private $title;
public function __construct($title='Default Page'){
$this->title=$title;
}
public function doHeader(){
$this->output='<html><head><title>'.$this->title.'</title></head><body>';
}
public function addHTMLElements($htmlElements){
foreach($htmlElements as $htmlElement){
if(is_array($htmlElement)){
$this->addHTMLElements($htmlElement);
}
else{
$this->output.=$htmlElement->getHTML();
}
}
}
public function doFooter(){
$this->output.='</body></html>';
}
public function fetchHTML(){
return $this->output;
}
}
// spawn some HTML elements
$h1=new Header1(array ('name'=>'header1','class'=>'headerclass'),'Content for H1 element goes here');
$div=new Div(array('name'=>'div1','class'=>'divclass'),'Content for Div element goes here');
$par=new Paragraph(array ('name'=>'par1','class'=>'parclass'),'Content for Paragraph element goes here');
$ul=new UnorderedList(array ('name'=>'list1','class'=>'listclass'),array ('item1'=>'value1','item2'=>'value2','item3'=>'value3'));
// make recursive array with HTML objects
$htmlElements=array($h1,array($div,$par,$ul));
// instantiate 'PageGenerator' object
$pageGen=new PageGenerator();
// generate web page
$pageGen->doHeader();
$pageGen->addHTMLElements($htmlElements);
$pageGen->doFooter();
// display web page
echo $pageGen->fetchHTML();
?>