1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| 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 form objects HTML
return $formElement->getHTML();
}
}
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 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;
}
}
$input1=array('textinput'=>array('name'=>'textbox1','value'=>$_POST['textbox1']));
$input2=array('textinput'=>array('name'=>'textbox2','value'=>$_POST['textbox2']));
$input3=array('submitbutton'=>array('name'=>'submit','value'=>'Envoyer'));
$formElements=array($input1,$input2,$input3);
echo '<form name="form" action="'.$_SERVER['PHP_SELF'].'" method="post">';
foreach($formElements as $element)
{
foreach($element as $type=>$attributes)
{
echo formElementFactory::createElement($type,$attributes);
}
}
echo '</form>'; |
Partager