Last active
August 29, 2015 14:23
-
-
Save WillSquire/44f1692412d11599c577 to your computer and use it in GitHub Desktop.
OOHTML - Object orientated html. Build html objects using php classes and parse the object to html using the toHtml() function.
This file contains 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 | |
/** | |
* Base interface for PHP HTML elements | |
* | |
* Interface HtmlElement | |
*/ | |
interface HtmlElement { | |
/** | |
* Parses the html element to a html formatted string. | |
* | |
* @return string | |
*/ | |
public function toHtml(); | |
} |
This file contains 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 | |
require_once("HTMLElement.class.php"); | |
/** | |
* Class for the input html element. | |
* | |
* Class Input | |
*/ | |
class Input implements HtmlElement { | |
public $type; | |
public $name; | |
public $placeholder; | |
public $value; | |
/** | |
* Constructor/initialisation | |
* | |
* @param string $type | |
* @param string $name | |
* @param string $placeholder | |
* @param string $value | |
*/ | |
public function __construct($type = '', $name = '', $placeholder = '', $value = '') | |
{ | |
$this->type = $type; | |
$this->name = $name; | |
$this->placeholder = $placeholder; | |
$this->value = $value; | |
} | |
/** | |
* Parses this html element to a html formatted string. | |
* @return string | |
*/ | |
public function toHtml() | |
{ | |
$html = '<input'; | |
$html .= ' type="' . htmlspecialchars($this->type) . '"'; | |
$html .= ' name="' . htmlspecialchars($this->name) . '"'; | |
$html .= ' placeholder="' . htmlspecialchars($this->placeholder) . '"'; | |
$html .= ' value="' . htmlspecialchars($this->value) . '"'; | |
$html .= '>'; | |
return $html; | |
} | |
} |
This file contains 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 | |
require_once("HTMLElement.class.php"); | |
/** | |
* Class for the input's html element option. | |
* | |
* Class Option | |
*/ | |
class Option implements HtmlElement { | |
public $label; | |
public $value; | |
public $selected; | |
public $disabled; | |
/** | |
* Constructor/initialisation | |
* | |
* @param string $label | |
* @param string $value | |
* @param bool|false $selected | |
* @param bool|false $disabled | |
*/ | |
public function __construct($label = '', $value = '', $selected = false, $disabled = false) | |
{ | |
$this->label = $label; | |
$this->value = $value; | |
$this->selected = $selected; | |
$this->disabled = $disabled; | |
} | |
/** | |
* Sets if the option is selected and returns the option. | |
* | |
* @param $selected | |
* @return $this | |
*/ | |
public function isSelected($selected) | |
{ | |
$this->selected = $selected; | |
return $this; | |
} | |
/** | |
* Parses this html element to a html formatted string. | |
* @return string | |
*/ | |
public function toHtml() | |
{ | |
$html = '<option'; | |
$html .= ' value="' . htmlspecialchars($this->value) . '"'; | |
if ($this->selected) | |
$html .= ' selected'; | |
if ($this->disabled) | |
$html .= ' disabled'; | |
$html .= '>'; | |
$html .= $this->label; | |
$html .= '</option>'; | |
return $html; | |
} | |
/** | |
* Sets an option to selected if the option's label | |
* matches the given selected value. | |
* | |
* @param array $options | |
* @param $selected_value | |
* @return bool | |
*/ | |
static public function selectOptionWithLabel(array $options, $selected_value) | |
{ | |
$option_selected = false; | |
foreach ($options as $option) | |
{ | |
if ($option->label == $selected_value) | |
{ | |
$option->selected = true; | |
$option_selected = true; | |
break; | |
} | |
} | |
return $option_selected; | |
} | |
/** | |
* Sets an option to selected if the option's value | |
* matches the given selected value. | |
* | |
* @param array $options | |
* @param $selected_value | |
* @return bool | |
*/ | |
static public function selectOptionWithValue(array $options, $selected_value) | |
{ | |
$option_selected = false; | |
foreach ($options as $option) | |
{ | |
if ($option->value == $selected_value) | |
{ | |
$option->selected = true; | |
$option_selected = true; | |
break; | |
} | |
} | |
return $option_selected; | |
} | |
/** | |
* Sets options to selected if the option's value | |
* matches one of the given values in the selected values array. | |
* | |
* @param array $options | |
* @param array $selected_values | |
* @return bool | |
*/ | |
static public function selectOptionWithValues(array $options, array $selected_values) | |
{ | |
$option_selected = false; | |
foreach ($options as $option) | |
{ | |
foreach ($selected_values as $selected_value) | |
{ | |
if ($option->value == $selected_value) | |
{ | |
$option->selected = true; | |
$option_selected = true; | |
} | |
} | |
} | |
return $option_selected; | |
} | |
/** | |
* Output options to html and concantinate to | |
* build a string of html options. | |
* | |
* @param array $options | |
* @return string | |
*/ | |
static public function optionsToHtml(array $options) | |
{ | |
$options_html = ''; | |
foreach ($options as $option) | |
$options_html .= $option->toHtml(); | |
return $options_html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment