HomePHP Page 4 - The Singleton and Factory Patterns in PHP: Building a Form Generator Class
The source code box: listing the full developed classes - PHP
In this final part of the series, Alejandro Gervasio examines a point that he has not taking into consideration so far: that the layout of form elements plays a relevant role within the overall development process. With this in mind, he encapsulates the logic needed to generate web forms by defining a form generator class. This class will implement the form element factory along with all of the required form element classes.
As I said before, here is the full list of the classes used over the series:
/* *************************************** class definitions for form elements *************************************** */ // class textinput class textinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="text" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class passwordinput class passwordinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="password" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class hiddeninput class hiddeninput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="hidden" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class fileinput class fileinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="file" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class imageinput class imageinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="image" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class radiobutton class radiobutton{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="radio" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class checkbox class checkbox{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="checkbox" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class button class button{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="button" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class submitbutton class submitbutton{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="submit" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class resetbutton class resetbutton{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="reset" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } } // class textarea class textarea{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<textarea '; $textvalue=''; foreach($attributes as $attribute=>$value){ ($attribute!='value')?$this- >html.=$attribute.'="'.$value.'" ':$textvalue=$value; } $this->html=preg_replace("/\"? $/","\">",$this->html); $this->html.=$textvalue.'</textarea>'; } public function getHTML(){ return $this->html; } } // class selectbox class selectbox{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<select '; $options=''; foreach($attributes as $attribute=>$value){ if($attribute!='options'){ $this->html.=$attribute.'="'.$value.'" '; } else{ foreach($value as $values=>$label){ $options.='<option value="'.$values.'">'.$label.'</option>'; } } } $this->html=preg_replace("/\"? $/","\">",$this->html); $this->html.=$options.'</select>'; } public function getHTML(){ return $this->html; } } /* ***************************************** abstract formElementFactory class ***************************************** */ // abstract class formElementFactory // this class is abstract thus cannot be instantiated abstract class formElementFactory{ private function __construct(){} public static function createElement($type,$attributes=array ()){ if(!class_exists($type)||!is_array($attributes)){ throw new Exception('Invalid method parameters'); } // instantiate a new form element object $formElement=new $type($attributes); // return HTML of form element code return $formElement->getHTML(); } } /* ************************************** // formGenerator class definition ************************************** */ class formGenerator{ // data member declaration private $elements=array(); // array of form elements private $output=''; // dynamic output private $elementHeader=''; // element header private $elementFooter='<br />'; // element footer private $name='theform'; // form name private $method='post'; // form method private $action; // form action // constructor public function __construct($elements=array()){ if(count($elements)<1){ throw new Exception('Invalid number of elements'); } // data member initialization $this->elements=$elements; $this->action=$_SERVER['PHP_SELF']; } // create form code public function createForm(){ $this->output.='<form name="'.$this->name.'" action="'.$this->action.'" method="'.$this->method.'">'; foreach($this->elements as $element=>$attributes){ // call the abstract class formElementFactory $this->output.=$this- >elementHeader.formElementFactory::createElement ($element,$attributes).$this->elementFooter; } $this->output.='</form>'; } // add form part public function addFormPart($html='<br />'){ $this->output.=$html; } // add element header public function addElementHeader($header){ $this->elementHeader=$header; } // add element footer public function addElementFooter($footer){ $this->elementFooter=$footer; } // set form name public function setName($name){ $this->name=$name; } // set form action public function setAction($action){ $this->action=$action; } // set form method public function setMethod($method){ if($method!='post'&&$method!='get'){ throw new Exception('Invalid form method'); } $this->method=$method; } // get dynamic form output public function getFormCode(){ return $this->output; } }
That’s about it. Hopefully you’ll have some fun studying the code and tweaking it to fit your personal needs. Also, so as to not make the code excessively long, I’ve not listed the classes suitable for PHP 4, but you may want to take a look at them in the previous article. A final note: all of the examples have been tested on PHP 4.3.4 and PHP 5.0.4. respectively. Happy coding!