Home arrow PHP arrow Page 4 - User-defined Interfaces in PHP 5: Building a Page Generator

Full source code: listing the complete classes - PHP

In this final part of the series on user-defined interfaces in PHP 5, Alejandro puts all of the classes created in the previous articles together to work as a page generator. This allows complex page generation to be translated into a more flexible process through an object-based solution.

TABLE OF CONTENTS:
  1. User-defined Interfaces in PHP 5: Building a Page Generator
  2. Working with multiple interface implementers: defining a page generator class
  3. Building object-based web pages: implementing the “PageGenerator” class
  4. Full source code: listing the complete classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
January 09, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

For the sake of completeness, here is the full list of all the classes developed during the series. I hope you enjoy studying them and introducing your own modifications. See you soon!

// interface HTMLRenderer
// defines generic method toHTML()
interface HTMLRenderer {
    public abstract function toHTML();
}
// class MySQL
class MySQL{
    private $conId; // connection identifier
    private $host; // MySQL host
    private $user; // MySQL username
    private $password; // MySQL password
    private $database; // MySQL database
    // constructor
    public function __construct($options=array()){
        // validate incoming parameters
        if(count($options)<4){
            throw new Exception('Invalid number of connection
parameters');
        }
        foreach($options as $parameter=>$value){
            if(!$parameter||!$value){
                throw new Exception('Invalid connection
parameters');
            }
            $this->{$parameter}=$value;
        }
        // connect to MySQL
 $this->connectDB();
        }
        // connect to MYSQL server and select database
        private function connectDB(){
            if(!$this->conId=mysql_connect($this->host,$this-
>user,$this->password)){
                 throw new Exception('Error connecting to the server');
 }
 if(!mysql_select_db($this->database,$this->conId)){
     throw new Exception('Error selecting database');
 }
        }
        // perform query
        public function query($query){
 if(!$this->result=mysql_query($query,$this->conId)){
     throw new Exception('Error performing query '.$query);
 }
 // return new Result object
 return new Result($this,$this->result);
       }
}
//class Result
class Result implements HTMLRenderer{
    private $output; // dynamic output
    private $mysql; // instance of MySQL object
    private $result; // MySQL result set
    // constructor
    public function __construct($mysql,$result){
        $this->mysql=$mysql;
        $this->result=$result;
    }
    // fetch row
    public function fetchRow(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    }
    // count rows
    public function countRows(){
        if(!$rows=mysql_num_rows($this->result)){
            throw new Exception('Error counting rows');
        }
        return $rows;
    }
    // count affected rows
    public function countAffectedRows(){
        if(!$rows=mysql_affected_rows($this->mysql->conId)){
 throw new Exception('Error counting affected rows');
        }
        return $rows;
    }
    // get ID from last inserted row
    public function getInsertID(){
        if(!$id=mysql_insert_id($this->mysql->conId)){
 throw new Exception('Error getting ID');
        }
        return $id;
    }
    // seek row
    public function seekRow($row=0){
        if(!mysql_data_seek($this->result,$row)){
 throw new Exception('Error seeking data');
        }
    }
    // return HTML rendered result set
    public function toHTML(){
        $this->output='<table>';
        while($row=$this->fetchRow()){
            $this->output.='<tr>';
            foreach($row as $field=>$value){
                $this->output.='<td>'.$value.'</td>';
            }
            $this->output.='</tr>';
        }
        $this->output.='</table>';
        return $this->output;
    }
}
// ************************************
// begin of core class definitions
// ************************************
// class ObjectFactory - returns HTML widget objects
abstract class ObjectFactory{
    private function __construct(){}
    public function createObject($type,$attributes=array()){
        if(!class_exists($type)||!is_array($attributes)){
            throw new Exception('Invalid object parameters');
        }
        return new $type($attributes);
    }
}
// class PageGenerator - creates web pages
class PageGenerator{
    private $output;
    private $title;
    private $keywords;
    private $cssFile;
    private $jsFile;
    public function __construct(){
        $this->output='';
        $this->title='Sample Page';
        $this->keywords='PHP,Interfaces,OOP,Type,Hinting';
        $this->cssFile='styles.css';
        $this->jsFile='global_functions.js';
    }
    public function setTitle($title){
        $this->title=$title;
    }
    public function setKeywords($keywords){
        $this->keywords=$keywords;
    }
    public function setCssFile($cssFile){
        $this->cssFile=$cssFile;
    }
    public function setJsFile($jsFile){
        $this->jsFile=$jsFile;
    }
    public function makeHeader(){
        $this->output='<html><head><title>'.$this-
>title.'</title><meta name="keywords" content="'.$this-
>keywords.'" /><link rel="stylesheet" href="'.$this->cssFile.'"
type="text/css"><script language="javascript" src="'.$this-
>jsFile.'"></script></head><body>';
    }
    public function addHTMLRenderableObject(HTMLRenderer $object){
        $this->output.=$object->toHTML();
    }
    public function makeFooter(){
        $this->output.='</body></html>';
    }
    public function getHTML(){
        return $this->output;
    }
}
// *********************************************
// end of core class definitions
// *********************************************
// *********************************************
// begin of HTML widget class definitions
// *********************************************
// class Table
class Table implements HTMLRenderer{
    private $output='<table ';
    private $data=array();
    private $attributes=array();
    public function __construct($attributes=array()){
        if(count($attributes)<1){
            throw new Exception('Invalid number of attributes');
        }
        $this->attributes=$attributes;
    }
    public function setData($data=array()){
        if(count($data)<1){
            throw new Exception('Empty data set');
        }
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        foreach($this->data as $data){
            $data=($data instanceof HTMLRenderer)?$data->toHTML
():$data;
            $this->output.='<tr><td>'.$data.'</td></tr>';
        }
        $this->output.='</table>';
        return $this->output;
    }
}
// class Div
class Div implements HTMLRenderer{
    private $output='<div ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</div>';
        return $this->output;
    }
}
// class Header1
class Header1 implements HTMLRenderer{
    private $output='<h1 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h1>';
        return $this->output;
    }
}
// class Header2
class Header2 implements HTMLRenderer{
    private $output='<h2 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h2>';
        return $this->output;
    }
}
// class Header3
class Header3 implements HTMLRenderer{
    private $output='<h3 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h3>';
        return $this->output;
    }
}
// class Header4
class Header4 implements HTMLRenderer{
    private $output='<h4 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h4>';
        return $this->output;
    }
}
// class Header5
class Header5 implements HTMLRenderer{
    private $output='<h5 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h5>';
        return $this->output;
    }
}
// class Header6
class Header6 implements HTMLRenderer{
    private $output='<h6 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</h6>';
        return $this->output;
    }
}
// class Paragraph
class Paragraph implements HTMLRenderer{
    private $output='<p ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?
$this->data->toHTML():$this->data;
        $this->output.='</p>';
        return $this->output;
    }
}
// class HTMLlist
class HTMLlist implements HTMLRenderer{
    private $output='<ul ';
    private $data=array();
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data=array()){
        if(count($data)<1){
            throw new Exception('Empty data set');
        }
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        foreach($this->data as $data){
            $data=($data instanceof HTMLRenderer)?$data->toHTML
():$data;
            $this->output.='<li>'.$data.'</li>';
        }
        $this->output.='</ul>';
        return $this->output;
    }
}
// class Form
class Form implements HTMLRenderer{
    private $output='<form ';
    private $data;
    private $fields=array();
    private $attributes=array();
    public function __construct($attributes){
        $this->attributes=$attributes;
    }
    // set generic form data
    public function setData($data){
        $this->data=$data;
    }
    // add <input> element
    public function addInputField($fieldAttributes=array
(),$inputPart=''){
        $fieldOutput=$inputPart.'<input ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $this->fields[]=$fieldOutput.'/>';
    }
    // add <textarea> element
    public function addTextArea($fieldAttributes=array
(),$inputPart=''){
        $fieldOutput=$inputPart.'<textarea ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $fieldOutput=substr_replace($fieldOutput,'>',-1);
        $this->fields[]=$fieldOutput.'</textarea>';
    }
    // add <select> element
    public function addSelectField($fieldAttributes=array(),$options=array(),$inputPart=''){
        $fieldOutput=$inputPart.'<select ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $fieldOutput=substr_replace($fieldOutput,'>',-1);
        foreach($options as $option=>$value){
            $fieldOutput.='<option
value="'.$value.'">'.$option.'</option>';
        }
        $this->fields[]=$fieldOutput.'</select>';
       
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?$this->data->toHTML():$this->data;
        foreach($this->fields as $field){
            $this->output.=$field;
        }
        $this->output.='</form>';
        return $this->output;
    }
}
// *******************************************
// end of HTML widget class definitions
// *******************************************

Note: all the classes have been tested on Apache 2.0.48 and PHP 5.0.4.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: