te le riporto cosi come le ho trovate, alcuni passaggi ora si possono fare molto più semplici
l'interfaccia per i widget
Codice PHP:
inteface returnHtml {
public function toHtml();
}
tag.php
Codice PHP:
abstract class tag {
public $content;
public $output;
public function __construct( $attributes ) {
if ( count( $attributes ) < 1 ) {
throw new Exception( 'Invalid number of attributes! for "' . get_class( $this ) . '::construct()' );
}
$this->attributes = $attributes;
}
abstract protected function setContent( $content );
}
un esempio di widget html
p.php
Codice PHP:
class p extends tag implements returnHTML {
public $content;
public $output = '<p ';
public function setContent( $content ) {
$this->content = ($content instanceof returnHtml) ? $content->toHtml() : $content;
return $this;
}
public function toHtml() {
foreach ( $attributes AS $attribute=>$value ) {
$this->output .= $attribute . '="' . $value . '" ';
}
$this->output = substr_replace( $this->output, '>', -1 );
$this->output .= $this->content;
$this->output .= '</p>';
return $this->output;
}
}
la classe form (la gestione dell'interfaccia ora si può fare in modo più elegante)
form.php
Codice PHP:
class form {
private static $instance = NULL;
private $action;
private $autocomplete;
private $enctype;
private $method = 'POST';
private $name;
private $novalidate;
private $target;
private $attributes = array();
private $content = array();
private $nl = "\n";
private $html;
public function __construct( $attributes, $content ) {
$this->attributes = $attributes;
$this->content[] = $content;
}
protected function parseAttributes() {
$permittedAttributes = array( 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable',
'dropzone', 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title',
'accept-charset', 'action', 'autocomplete', 'enctype', 'method', 'name', 'novalidate',
'target'
);
foreach( $this->attributes AS $attribute=>$value ) {
if ( !in_array( $attribute, $permittedAttributes ) ) {
throw new Exception( __CLASS__ . "L'attributo " . $attribute . ' non è supportato.', 1 );
}
$this->$attribute = $value;
}
}
public function setId( $id ) {
$this->action = $id;
}
public function setAction( $action ) {
$this->action = $action;
}
public function setAutocomplete( $autocomplete ) {
$permittedValue = array( 'on', 'off' );
if ( in_array( $autocomplete, $permittedValue ) ) {
$this->autocomplete = $autocomplete;
} else {
throw new Exception( __CLASS__ . '"' . $autocomplete . '" non è un valore valido per l\'attributo "autocomplete". Usa "on" o "off".', 1 );
}
}
public function setEnctype( $enctype ) {
$this->enctype = $enctype;
}
public function setMethod( $method ) {
$permittedValue = array( 'GET', 'POST' );
if ( in_array( $method, $permittedValue ) ) {
$this->method = $method;
} else {
throw new Exception( __CLASS__ . '"' . $method . '" non è un valore valido per l\'attributo "method". Usa "GET" o "POST".', 1 );
}
}
public function setName( $name ) {
$this->name = $name;
}
public function setNovalidate( $novalidate ) {
$this->novalidate = $novalidate;
}
public function setTarget( $target ) {
$this->target = $target;
}
private function getContent() {
return $this->elementToHtml( $this->content );
}
private function elementToHtml( $element ) {
// initialize the variable that will collect our generated HTML
$html = '';
// Attempt to generate HTML code for what is passed
if ( is_object( $element ) ) {
// If this is an object, attempt to generate the appropriate HTML
// code.
if ( is_subclass_of( $element, 'tag' ) ) {
// Attempt to generate code using first toHtml and then toString
// methods. The result is not parsed with _elementToHtml because
// it would improperly add one tab indentation to the initial line
// of each object's output.
if ( method_exists( $element, 'toHtml' ) ) {
$html .= $element->toHtml();
}
}
} elseif ( is_array( $element ) ) {
foreach ( $element AS $item ) {
// Parse each element individually
$html .= $this->elementToHtml( $item );
}
} else {
// If we don't have an object or array, we can simply output
// the element after indenting it and properly ending the line.
$html .= $element;
}
return $html;
}
private function buildForm() {
$this->html = '<form ';
if ( isset( $this->action ) ) {
$this->html .= ' action="' . $this->action . '"';
} else {
throw new Exception( __CLASS__ . 'Per creare un form devi inserire un valore per l\'attributo "action".', 1 );
}
if ( isset( $this->autocomplete ) ) {
$this->html .= ' autocomplete="' . $this->autocomplete . '"';
}
if ( isset( $this->enctype ) ) {
$this->html .= ' enctype="' . $this->enctype . '"';
}
if ( isset( $this->method ) ) {
$this->html .= ' method="' . $this->method . '"';
}
if ( isset( $this->name ) ) {
$this->html .= ' name="' . $this->name . '"';
}
if ( isset( $this->id ) ) {
$this->html .= ' id="' . $this->id . '"';
}
if ( isset( $this->novalidate ) ) {
$this->html .= ' novalidate="' . $this->novalidate . '"';
}
if ( isset( $this->target ) ) {
$this->html .= ' target="' . $this->target . '"';
}
$this->html .= '>' . $this->nl;
$this->html .= $this->getContent();
$this->html .= '</form>' . $this->nl;
}
public function getHtml() {
$this->buildForm();
return $this->html;
}
}
uso
Codice PHP:
$p = new p( array( 'class'=>'blue' ) )->setContent( 'Questo è un paragrafo' );
$form = new form( array( 'name'=>'user-insert-form', 'method'=>'POST' ), $p );
$form->setAction( $_SERVER['PHP_SELF'] );
echo $form->getHtml();
ecco
il metodo più importante è elementToHtml