Created
May 30, 2012 20:18
-
-
Save gordonmcvey/2838669 to your computer and use it in GitHub Desktop.
Experiment with DOM as markup widget generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Widget | |
{ | |
const | |
TYPE = ''; | |
protected | |
/** | |
* @var DOMDocument | |
*/ | |
$dom = NULL, | |
/** | |
* @var DOMElement | |
*/ | |
$node = NULL, | |
/** | |
* @var array | |
*/ | |
$classes = array (), | |
/** | |
* @var string | |
*/ | |
$elemName = ''; | |
/** | |
* Get the element node | |
* | |
* @return DOMElement | |
*/ | |
public function getNode () | |
{ | |
if ($this -> node === NULL) | |
{ | |
$this -> node = new DOMElement (static::TYPE); | |
$this -> dom -> appendChild ($this -> node); | |
} | |
return $this -> node; | |
} | |
/** | |
* Check if the element node has been initialized | |
* | |
* @return bool | |
*/ | |
protected function nodeInitialized () | |
{ | |
return $this -> node !== NULL; | |
} | |
/** | |
* Set an ID for the element | |
* | |
* @param string $id | |
* @return \FormWidget | |
* @throws InvalidArgumentException | |
*/ | |
public function setId ($id) | |
{ | |
if (is_string ($id)) | |
{ | |
$this -> elemId = $id; | |
!empty ($id)? | |
$this -> getNode () -> setAttribute ('id', $id): | |
$this -> getNode () -> removeAttribute ('id'); | |
} | |
else | |
{ | |
throw new InvalidArgumentException (); | |
} | |
return $this; | |
} | |
/** | |
* Add a class to the element | |
* | |
* @param type $class | |
* @return \FormWidget | |
* @throws InvalidArgumentException | |
*/ | |
public function addClass ($class) | |
{ | |
if (is_string ($class)) | |
{ | |
if (!in_array ($class, $this -> classes)) | |
{ | |
$this -> classes [] = $class; | |
$this -> getNode () -> setAttribute ('class', implode (' ', $this -> classes)); | |
} | |
} | |
else | |
{ | |
throw new InvalidArgumentException (); | |
} | |
return $this; | |
} | |
/** | |
* Constructor | |
* | |
* @param DOMDocument $dom | |
*/ | |
public function __construct (DOMDocument $dom) | |
{ | |
$this -> dom = $dom; | |
} | |
/** | |
* Cast to string | |
* | |
* @return string | |
*/ | |
public function __toString () | |
{ | |
return $this -> dom -> saveHTML ($this -> getNode ()); | |
} | |
} | |
/** | |
* Form widget class | |
*/ | |
abstract class FormWidget extends Widget | |
{ | |
protected | |
/** | |
* @var string | |
*/ | |
$elemId = ''; | |
/** | |
* Set the name for the element | |
* | |
* @param string $name | |
* @return \FormWidget | |
* @throws InvalidArgumentException | |
*/ | |
public function setName ($name) | |
{ | |
if (is_string ($name)) | |
{ | |
$this -> elemName = $name; | |
!empty ($name)? | |
$this -> getNode () -> setAttribute ('name', $name): | |
$this -> getNode () -> removeAttribute ('name'); | |
} | |
else | |
{ | |
throw new InvalidArgumentException (); | |
} | |
return $this; | |
} | |
} | |
/** | |
* Form inputs (input tag) | |
*/ | |
class InputWidget extends FormWidget | |
{ | |
const | |
TYPE = 'input'; | |
protected | |
$value = ''; | |
/** | |
* Set the value for the widget | |
* | |
* @param type $value | |
* @return \InputWidget | |
* @throws InvalidArgumentException | |
*/ | |
public function setValue ($value) | |
{ | |
if (is_string ($value)) | |
{ | |
$this -> value = $value; | |
!empty ($value)? | |
$this -> getNode () -> setAttribute ('value', $value): | |
$this -> getNode () -> removeAttribute ('value'); | |
} | |
else | |
{ | |
throw new InvalidArgumentException (); | |
} | |
return $this; | |
} | |
} | |
/** | |
* Text widget (input type=text) | |
*/ | |
class TextWidget extends InputWidget | |
{ | |
/** | |
* Get the input element | |
* | |
* @return type | |
*/ | |
public function getNode () | |
{ | |
$initialized = $this -> nodeInitialized (); | |
$node = parent::getNode (); | |
if (!$initialized) | |
{ | |
$node -> setAttribute ('type', 'text'); | |
} | |
return $node; | |
} | |
} | |
/** | |
* Select widget | |
*/ | |
class SelectWidget extends FormWidget | |
{ | |
const | |
TYPE = 'select'; | |
} | |
/** | |
* Textarea widget | |
*/ | |
class TextAreaWidget extends FormWidget | |
{ | |
const | |
TYPE = 'textarea'; | |
} | |
/** | |
* Button widget | |
*/ | |
class ButtonWidget extends FormWidget | |
{ | |
const | |
TYPE = 'button'; | |
} | |
$dom = new DOMDocument (); | |
$test = new TextWidget ($dom); | |
$test -> setId ('thisId') | |
-> setName ('thisName') | |
-> addClass ('foo') | |
-> addClass ('bar') | |
-> addClass ('baz'); | |
$test2 = new TextWidget ($dom); | |
$test2 -> setId ('thatId') | |
-> setName ('thisName') | |
-> addClass ('fred') | |
-> addClass ('wilma') | |
-> addClass ('pebbles'); | |
?> | |
<!-- | |
To change this template, choose Tools | Templates | |
and open the template in the editor. | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<?php | |
echo $test . PHP_EOL | |
. $test . PHP_EOL | |
. $test . PHP_EOL | |
. $test2 . PHP_EOL | |
. $test2 . PHP_EOL | |
. $test2 . PHP_EOL; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment