Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Disabled trait
- *
- * This trait adds the disabled attribute, along with its getter and setter.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- trait Disabled {
- /**
- * @var boolean $disabled
- */
- protected $disabled = false;
- /**
- * @return boolean True if disabled, false if not.
- */
- public function getDisabled() {
- return $this->disabled;
- }
- /**
- * @param boolean $disabled
- * @return self
- */
- public function setDisabled( $disabled ) {
- $this->disabled = $disabled;
- return $this;
- }
- }
- /**
- * Required trait
- *
- * This trait adds the required attribute, along with its getter and setter.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- trait Required {
- /**
- * @var boolean $disabled
- */
- protected $required = false;
- /**
- * @return boolean True if required, false if not.
- */
- public function getRequired() {
- return $this->required;
- }
- /**
- * @param boolean required
- * @return self
- */
- public function setRequired( $required ) {
- $this->required = $required;
- return $this;
- }
- }
- /**
- * Placeholder trait
- *
- * This trait adds the placeholder attribute, along with its getter and setter.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- trait Placeholder {
- /**
- * @var string $placeholder
- */
- protected $placeholder;
- /**
- * @return string The placeholder attribute.
- */
- public function getPlaceholder() {
- return $this->placeholder;
- }
- /**
- * @param string $placeholder
- * @return self
- */
- public function setPlaceholder( $placeholder ) {
- $this->placeholder = $placeholder;
- return $this;
- }
- }
- /**
- * Value trait
- *
- * This trait adds the value attribute, along with its getter and setter.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- trait Value {
- /**
- * @var string $value The value attribute.
- */
- protected $value;
- /**
- * @return string The value attribute.
- */
- public function getValue() {
- return $this->value;
- }
- /**
- * @param string $value
- * @return self
- */
- private function setValue( $value ) {
- $this->value = $value;
- return $this;
- }
- }
- /**
- * HTML class
- *
- * HTML class is used to create objects representing HTML elements
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML {
- // define constants
- const AS_ARRAY = 0;
- const AS_STRING = 1;
- const CONTENT_BEFORE = 0;
- const CONTENT_AFTER = 1;
- const DO_NOT_INDEX = 0;
- const INDEX_BY_ID = 1;
- const FOR_ATTR = 'for';
- const GET_OPTION = 0;
- const GET_VALUE = 1;
- const BY_VALUE = 0;
- const BY_TEXT = 1;
- const ASC = 0;
- const DESC = 1;
- /**
- * @param string $element_type The HTML element type.
- * @param string $id (optional) The id attribute.
- * return self
- */
- public function __construct( $element_type, $id = NULL ) {
- $this->setElementType( $element_type );
- $this->setId( $id );
- return $this;
- }
- /**
- * @var array $attributes An array of attributes.
- */
- protected $attributes = array( 'id', 'name', 'class', 'data', 'style' );
- /**
- * @param array|string $attribute An array of attributes to add (or a space-separated string).
- * @return self
- */
- protected function addAttribute( $attribute ) {
- if ( is_string( $attribute ) ) $attribute = explode( ' ', trim( $attribute ) );
- $this->attributes = array_unique( array_merge( $this->attributes, $attribute ), SORT_REGULAR );
- return $this;
- }
- /**
- * @var string $element_type The type of HTML element.
- */
- protected $element_type;
- /**
- * @return string The element type.
- */
- public function getElementType() {
- return $this->element_type;
- }
- /**
- * @param string $element_type
- * @return self
- */
- private function setElementType( $element_type ) {
- $this->element_type = $element_type;
- return $this;
- }
- // void element flag
- protected $void;
- private function isVoid() {
- return $this->void;
- }
- /**
- * Sets void flag (whether the element has contents or not) based on element type.
- */
- protected function setVoid() {
- // list from: https://www.w3.org/TR/html5/syntax.html#void-elements
- switch ( $this->element_type ) {
- case 'area':
- case 'base':
- case 'br':
- case 'col':
- case 'embed':
- case 'hr':
- case 'img':
- case 'input':
- case 'keygen':
- case 'link':
- case 'meta':
- case 'param':
- case 'source':
- case 'track':
- case 'wbr':
- $this->void = true;
- break;
- default:
- $this->void = false;
- break;
- }
- return $this;
- }
- /**
- * @var string $id
- */
- protected $id;
- /**
- * @return string|NULL The id.
- */
- public function getId() {
- return $this->id;
- }
- /**
- * @param string|NULL $id
- * @return self
- */
- private function setId( $id ) {
- $this->id = $id;
- return $this;
- }
- /**
- * @var string $name
- */
- protected $name;
- /**
- * @return string Name
- */
- public function getName() {
- return $this->name;
- }
- /**
- * @param string $name Name
- * @return self
- */
- public function setName( $name ) {
- $this->name = $name;
- return $this;
- }
- /**
- * @var array $classes
- */
- protected $classes = array();
- /**
- * @return array Array of classes
- */
- public function getClasses() {
- return $this->classes;
- }
- /**
- * Clears current class array and adds provided classes
- *
- * @param string|array $classes A string of space-separated classes or an array of classes
- * @return self
- */
- private function setClasses( $classes ) {
- $this->classes = array();
- $this->addClass( $classes );
- return $this;
- }
- /**
- * Adds new class(es)
- *
- * @param string|array $classes A string of space-separated classes or an array of classes
- * @return self
- */
- public function addClass( $class ) {
- if ( is_string( $class ) ) $class = explode( ' ', trim( $class ) );
- $this->classes = array_unique( array_merge( $this->classes, $class ), SORT_REGULAR );
- return $this;
- }
- /**
- * @return string The class attribute string.
- */
- private function getClassAttribute() {
- return empty( $this->classes ) ? '' : 'class="'.implode( ' ', $this->classes ).'"';
- }
- /**
- * @var array $style An associative array of CSS style properties mapped to their values.
- */
- protected $style = array();
- /**
- * @return array An associative array of CSS style properties mapped to their values.
- */
- public function getStyle() {
- return $this->style;
- }
- /**
- * @param string $property A CSS property.
- * @param string $value The value of the given CSS property.
- * @return self
- */
- public function setStyleProperty( $property, $value ) {
- $this->style[ $property ] = $value;
- return $this;
- }
- /**
- * Adds multiple CSS properties.
- *
- * @param array $properties An associative array of CSS style properties mapped to their values.
- * @return self
- */
- public function setStyleProperties( $properties ) {
- foreach ( $properties AS $property => $value ) $this->setStyleProperty( $property, $value );
- return $this;
- }
- /**
- * Clears current style and adds the provided style.
- *
- * @param array|string $style An associative array of properties => values or a valid CSS string.
- * @return self|boolean Returns false if invalid style provided.
- */
- public function setStyle( $style ) {
- if ( is_array( $style ) ) $this->style = $style;
- elseif ( is_string( $style ) && preg_match( '/^(?>\s*[^:]+\s*:\s*[^;]+\s*;\s*)+$/', $style ) === 1 ) {
- $properties = explode( ';', trim( $style, ' ;' ) );
- $this->style = array();
- foreach ( $properties AS $property ) {
- $property = explode( ':', $property );
- $this->setStyleProperty( trim( $property[ 0 ] ), trim( preg_replace( '/\s{2,}/', ' ', $property[ 1 ] ) ) );
- }
- }
- else return false; // invalid style provided
- return $this;
- }
- /**
- * @return string The inline style attribute.
- */
- private function getStyleAttribute() {
- if ( empty( $this->style ) ) return '';
- $style = 'style="';
- foreach ( $this->style AS $property => $value ) $style .= $property.':'.$value.';';
- return $style.'"';
- }
- /**
- * @var array $data An associative array of data-* attributes mapped to their values.
- */
- protected $data = array();
- /**
- * @return array An associative array of data-* attributes mapped to their values.
- */
- public function getData() {
- return $this->data;
- }
- /**
- * Clears current data attributes and adds the new ones.
- *
- * @param array An associative array of data-* attributes mapped to their values.
- * @return self;
- */
- private function setData( $data ) {
- $this->data = array();
- $this->addData( $data );
- return $this;
- }
- /**
- * @param array An associative array of data-* attributes mapped to their values.
- * @return self
- */
- public function addData( $data ) {
- foreach ( $data AS $key => $value ) $this->data[ $key ] = $value;
- return $this;
- }
- /**
- * @return string All data-* attributes condensed into a single string.
- */
- private function getDataAttribute() {
- $data_attributes = '';
- foreach ( $this->data AS $key => $value ) $data_attributes .= ' data-'.$key.'="'.$value.'"';
- return ltrim( $data_attributes );
- }
- /**
- * @return string All attributes combined into a string.
- */
- protected function getAttributes() {
- $attributes = '';
- foreach ( $this->attributes AS $attribute ) {
- // check if this attribute requires special processing
- // to require custom processing, create a function named 'get<capitalized attribute name>Attribute'
- $get_method = 'get'.ucwords( $attribute ).'Attribute';
- if ( method_exists( $this, $get_method ) ) $attributes .= ' '.$this->{$get_method}();
- else {
- if ( is_bool( $this->{$attribute} ) ) {
- if ( !$this->{$attribute} ) continue;
- $attributes .= ' '.$attribute.'="'.$attribute.'"';
- }
- elseif ( isset( $this->{$attribute} ) ) $attributes .= ' '.$attribute.'="'.$this->{$attribute}.'"';
- }
- }
- return $attributes;
- }
- /**
- * @return string The HTML representation of the element.
- */
- public function getElement() {
- return '<'.$this->getElementType().$this->getAttributes().'>';
- }
- /**
- * Print the element HTML.
- */
- public function output() {
- echo $this->getElement();
- }
- }
- /**
- * HTML_Element class
- *
- * HTML_Element class is used to create objects representing non-void HTML elements.
- * Non-void elements are HTML elements that may contain content. These elements are
- * formatted as '<tag>content</tag>'.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Element extends HTML {
- /**
- * @var string $text The text content of the element.
- */
- protected $text = '';
- /**
- * @return string The element's text.
- */
- public function getText() {
- return $this->text;
- }
- /**
- * @param string $text
- * @return self
- */
- public function setText( $text ) {
- $this->text = $text;
- return $this;
- }
- /**
- * @param string $contents (optional) If provided, replace the element text with $contents.
- * @return string The element's HTML string.
- */
- public function getElement( $contents=NULL ) {
- return parent::getElement().( isset( $contents ) ? $contents : $this->getText() ).'</'.$this->getElementType().'>';
- }
- }
- /**
- * HTML_Container_Element class
- *
- * HTML_Container_Element class is used to create objects representing HTML elements
- * which may contain child HTML elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Container_Element extends HTML_Element {
- /**
- * @var array $children An array of child elements.
- */
- protected $children = array();
- /**
- * @param HTML::AS_ARRAY|HTML::AS_STRING $format The return format.
- * @return array|string An array of HTML based objects or an HTML string of all child elements.
- */
- public function getChildren( $format = HTML::AS_ARRAY ) {
- if ( $format === HTML::AS_ARRAY ) return $this->children;
- elseif ( $format === HTML::AS_STRING ) {
- $children = '';
- foreach ( $this->children AS $child ) $children .= $child->getElement();
- return $children;
- }
- return false;
- }
- /**
- * Add child element(s)
- *
- * @param HTML instance|array $child An HTML instance or an array of HTML instances
- * return self
- */
- public function addChild( $child, $key=NULL ) {
- if ( isset( $key ) ) $this->children[ $key ] = $child;
- else $this->children[] = $child;
- return $this;
- }
- /**
- * Add multiple children at once.
- *
- * @param HTML array $children An array of HTML instances
- * return self
- */
- public function addChildren( Array $children ) {
- foreach ( $children AS $key => $child ) {
- if ( is_string( $key ) ) $this->addChild( $child, $key );
- else $this->addChild( $child );
- }
- return $this;
- }
- /**
- * Clears current children and adds new child(ren)
- *
- * @param HTML instance|array $child An HTML instance or an array of HTML instances
- * return self
- */
- protected function setChildren( $children ) {
- $this->children = array();
- $this->addChild( $children );
- return $this;
- }
- /**
- * @var HTML::CONTENT_BEFORE|HTML::CONTENT_AFTER $format Order of text content and child elements.
- */
- protected $format = HTML::CONTENT_BEFORE;
- /**
- * @param HTML::CONTENT_BEFORE|HTML::CONTENT_AFTER $format Order of text content and child elements.
- * @return self
- */
- public function setFormat( $format ) {
- if ( $format !== HTML::CONTENT_BEFORE && $format !== HTML::CONTENT_AFTER ) return false;
- $this->format = $format;
- return $this;
- }
- /**
- * @return string The element's HTML string.
- */
- public function getElement( $contents=NULL ) {
- return parent::getElement( $this->getElementContents() );
- }
- /**
- * @return string The element's children and text. Order determined by set format.
- * @see setFormat
- */
- protected function getElementContents() {
- if ( $this->format === HTML::CONTENT_BEFORE ) return $this->getText().$this->getChildren( HTML::AS_STRING );
- return $this->getChildren( HTML::AS_STRING ).$this->getText();
- }
- }
- /**
- * HTML_Input class
- *
- * HTML_Input class is used to create objects representing HTML input elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Input extends HTML {
- use Disabled, Value;
- /**
- * @param string $id The id attribute.
- * @param string $value The value attribute.
- * @param string $type The type attribute.
- * return self
- */
- function __construct( string $id, string $value, string $type ) {
- parent::__construct( 'input', $id );
- $this->addAttribute( array( 'disabled', 'type', 'value' ) )->setType( $type )->setValue( $value );
- return $this;
- }
- /**
- * @var string $type The type attribute.
- */
- protected $type;
- /**
- * @return string The type attribute.
- */
- public function getType() {
- return $this->type;
- }
- /**
- * @param string $type
- * @return self
- */
- private function setType( string $type ) {
- $this->type = $type;
- return $this;
- }
- }
- /**
- * HTML_Checkbox class
- *
- * HTML_Checkbox class is used to create objects representing HTML checkbox elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Checkbox extends HTML_Input {
- /**
- * @param string $id The id attribute.
- * @param string $value The value attribute.
- * @param string $checked (optional) Defaults to false.
- * return self
- */
- function __construct( string $id, string $value, boolean $checked ) {
- parent::__construct( $id, $value, 'checkbox' );
- $this->addAttribute( array( 'checked' ) )->setChecked( isset( $checked ) ? $checked : false );
- return $this;
- }
- /**
- * @var boolean $checked
- */
- protected $checked;
- /**
- * @return boolean True if checked, false if not.
- */
- public function getChecked() {
- return $this->checked;
- }
- /**
- * @param boolean $checked
- * @return self
- */
- public function setChecked( boolean $checked ) {
- $this->checked = $checked;
- return $this;
- }
- }
- /**
- * HTML_Textarea class
- *
- * HTML_Textarea class is used to create objects representing HTML textarea elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Textarea extends HTML_Element {
- use Disabled, Required, Placeholder;
- /**
- * @param string $id The id attribute.
- * @param string $text The text.
- * return self
- */
- function __construct( string $id, $text='' ) {
- parent::__construct( 'textarea', $id );
- $this->addAttribute( array( 'required', 'disabled' ) )->setText( $text );
- return $this;
- }
- }
- /**
- * HTML_Textbox class
- *
- * HTML_Textbox class is used to create objects representing HTML text input elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Textbox extends HTML_Input {
- use Required, Placeholder;
- /**
- * @param string $id The id attribute.
- * @param string $value (optional) The value attribute. Defaults to empty string.
- * return self
- */
- function __construct( string $id, $value='' ) {
- parent::__construct( $id, $value, 'text' );
- $this->addAttribute( array( 'required' ) );
- return $this;
- }
- }
- /**
- * HTML_Form class
- *
- * HTML_Form class is used to create objects representing HTML form elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Form extends HTML_Container_Element {
- /**
- * @param string $id The id attribute.
- * @param string $action (optional) The action attribute. Defaults to 'javascript:void(0);'.
- * return self
- */
- function __construct( string $id, $action='javascript:void(0);' ) {
- parent::__construct( 'form', $id );
- $this->addAttribute( array( 'action' ) )->setAction( $action );
- return $this;
- }
- /**
- * @var string $action The action attribute.
- */
- protected $action;
- /**
- * @return string The action attribute.
- */
- public function getAction() {
- return $this->action;
- }
- /**
- * @param string $action The action attribute.
- * @return self
- */
- public function setAction( string $action ) {
- $this->action = $action;
- return $this;
- }
- }
- /**
- * HTML_Label class
- *
- * HTML_Label class is used to create objects representing HTML label elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Label extends HTML_Container_Element {
- /**
- * @param string $id The id attribute.
- * @param string $for (optional) The for attribute.
- * return self
- */
- function __construct( $id=NULL, $for=NULL ) {
- parent::__construct( 'label', $id );
- $this->addAttribute( array( 'for' ) )->setFor( $for );
- return $this;
- }
- /**
- * @var string The for attribute.
- */
- protected $for;
- /**
- * @return string The for attribute.
- */
- public function getFor() {
- return $this->{HTML::FOR_ATTR};
- }
- /**
- * @param string $for The for attribute.
- * @return self
- */
- public function setFor( string $for ) {
- $this->{HTML::FOR_ATTR} = $for;
- return $this;
- }
- }
- /**
- * HTML_Select class
- *
- * HTML_Select class is used to create objects representing HTML select elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Select extends HTML_Container_Element {
- use Disabled;
- /**
- * @param string $id The id attribute.
- * @return self
- */
- function __construct( string $id, array $options=array() ) {
- parent::__construct( 'select', $id );
- $this->addOptions( $options );
- return $this;
- }
- /**
- * @param string $value
- * @param string $text
- * @return self
- */
- public function addOption( string $value, string $text ) {
- $this->addChild( ( new HTML_Option( $value, $text ) ) );
- return $this;
- }
- /**
- * @param array $options Associative array of option value => text pairs
- * @return self
- */
- public function addOptions( array $options ) {
- foreach ( $options AS $value => $text ) $this->addOption( $value, $text );
- return $this;
- }
- /**
- * @param HTML::GET_OPTION|HTML::GET_VALUE $return (optional) Default HTML::GET_OPTION
- * @return HTML_Option|string Depends on $return. The selected HTML_Option object, or said object's value attribute.
- */
- public function getSelected( $return=HTML::GET_OPTION ) {
- $default = false;
- foreach ( $this->children AS &$option ) {
- if ( $default === false ) $default = $option;
- if ( $option->getSelected() ) return ( $return === HTML::GET_OPTION ) ? $option : $option->getValue();
- }
- return ( $return === HTML::GET_OPTION ) ? $default : $default->getValue();
- }
- /**
- * @param string $value The value attribute of the option to select
- * @return self
- */
- public function setSelected( string $value ) {
- foreach ( $this->children AS &$option ) $option->setSelected( ( $option->getValue() == $value ) );
- return $this;
- }
- /**
- * @param HTML::BY_TEXT|HTML::BY_VALUE $sort_by
- * @param HTML::ASC|HTML::DESC $order
- * @return self
- */
- public function sortOptions( $sort_by = HTML::BY_TEXT, $order = HTML::ASC ) {
- if ( $sort_by === HTML::BY_TEXT ) {
- if ( $order === HTML::ASC ) usort( $this->children, function( $a, $b ) { return strcmp( $a->getText(), $b->getText() ); } );
- elseif ( $order === HTML::DESC ) usort( $this->children, function( $a, $b ) { return strcmp( $b->getText(), $a->getText() ); } );
- }
- elseif ( $sort_by === HTML::BY_VALUE ) {
- if ( $order === HTML::ASC ) usort( $this->children, function( $a, $b ) { return strcmp( $a->getValue(), $b->getValue() ); } );
- elseif ( $order === HTML::DESC ) usort( $this->children, function( $a, $b ) { return strcmp( $b->getValue(), $a->getValue() ); } );
- }
- return $this;
- }
- }
- /**
- * HTML_Option class
- *
- * HTML_Option class is used to create objects representing HTML option elements.
- *
- * @package -
- * @subpackage HTML
- * @author JBHUTT09
- */
- class HTML_Option extends HTML_Container_Element {
- use Value;
- /**
- * @param string $value
- * @param string $text
- * @return self
- */
- function __construct( string $value, string $text ) {
- parent::__construct( 'option' );
- $this->addAttribute( array( 'value', 'selected' ) )->setValue( $value )->setText( $text );
- return $this;
- }
- /**
- * @var boolean $selected
- */
- protected $selected = false;
- /**
- * @return boolean True if selected, false if not.
- */
- public function getSelected() {
- return $this->selected;
- }
- /**
- * @param boolean $selected
- * @return self
- */
- public function setSelected( bool $selected ) {
- $this->selected = $selected;
- return $this;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment