JBHUTT09

HTML Object (practice)

Aug 11th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.26 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.     * Disabled trait
  5.     *
  6.     * This trait adds the disabled attribute, along with its getter and setter.
  7.     *
  8.     * @package -
  9.     * @subpackage HTML
  10.     * @author JBHUTT09
  11.     */
  12.     trait Disabled {
  13.         /**
  14.          * @var boolean $disabled
  15.          */
  16.         protected $disabled = false;
  17.        
  18.         /**
  19.          * @return boolean True if disabled, false if not.
  20.          */
  21.         public function getDisabled() {
  22.             return $this->disabled;
  23.         }
  24.        
  25.         /**
  26.          * @param boolean $disabled
  27.          * @return self
  28.          */
  29.         public function setDisabled( $disabled ) {
  30.             $this->disabled = $disabled;
  31.             return $this;
  32.         }
  33.     }
  34.  
  35.     /**
  36.     * Required trait
  37.     *
  38.     * This trait adds the required attribute, along with its getter and setter.
  39.     *
  40.     * @package -
  41.     * @subpackage HTML
  42.     * @author JBHUTT09
  43.     */
  44.     trait Required {
  45.         /**
  46.          * @var boolean $disabled
  47.          */
  48.         protected $required = false;
  49.        
  50.         /**
  51.          * @return boolean True if required, false if not.
  52.          */
  53.         public function getRequired() {
  54.             return $this->required;
  55.         }
  56.        
  57.         /**
  58.          * @param boolean required
  59.          * @return self
  60.          */
  61.         public function setRequired( $required ) {
  62.             $this->required = $required;
  63.             return $this;
  64.         }
  65.     }
  66.    
  67.     /**
  68.     * Placeholder trait
  69.     *
  70.     * This trait adds the placeholder attribute, along with its getter and setter.
  71.     *
  72.     * @package -
  73.     * @subpackage HTML
  74.     * @author JBHUTT09
  75.     */
  76.     trait Placeholder {
  77.         /**
  78.          * @var string $placeholder
  79.          */
  80.         protected $placeholder;
  81.        
  82.         /**
  83.          * @return string The placeholder attribute.
  84.          */
  85.         public function getPlaceholder() {
  86.             return $this->placeholder;
  87.         }
  88.        
  89.         /**
  90.          * @param string $placeholder
  91.          * @return self
  92.          */
  93.         public function setPlaceholder( $placeholder ) {
  94.             $this->placeholder = $placeholder;
  95.             return $this;
  96.         }
  97.     }
  98.    
  99.     /**
  100.     * Value trait
  101.     *
  102.     * This trait adds the value attribute, along with its getter and setter.
  103.     *
  104.     * @package -
  105.     * @subpackage HTML
  106.     * @author JBHUTT09
  107.     */
  108.     trait Value {
  109.         /**
  110.          * @var string $value The value attribute.
  111.          */
  112.         protected $value;
  113.        
  114.         /**
  115.          * @return string The value attribute.
  116.          */
  117.         public function getValue() {
  118.             return $this->value;
  119.         }
  120.        
  121.         /**
  122.          * @param string $value
  123.          * @return self
  124.          */
  125.         private function setValue( $value ) {
  126.             $this->value = $value;
  127.             return $this;
  128.         }
  129.     }
  130.    
  131.     /**
  132.     * HTML class
  133.     *
  134.     * HTML class is used to create objects representing HTML elements
  135.     *
  136.     * @package -
  137.     * @subpackage HTML
  138.     * @author JBHUTT09
  139.     */
  140.     class HTML {
  141.         // define constants
  142.         const AS_ARRAY = 0;
  143.         const AS_STRING = 1;
  144.         const CONTENT_BEFORE = 0;
  145.         const CONTENT_AFTER = 1;
  146.         const DO_NOT_INDEX = 0;
  147.         const INDEX_BY_ID = 1;
  148.         const FOR_ATTR = 'for';
  149.         const GET_OPTION = 0;
  150.         const GET_VALUE = 1;
  151.         const BY_VALUE = 0;
  152.         const BY_TEXT = 1;
  153.         const ASC = 0;
  154.         const DESC = 1;
  155.        
  156.         /**
  157.          * @param string $element_type The HTML element type.
  158.          * @param string $id (optional) The id attribute.
  159.          * return self
  160.          */
  161.         public function __construct( $element_type, $id = NULL ) {
  162.             $this->setElementType( $element_type );
  163.             $this->setId( $id );
  164.             return $this;
  165.         }
  166.        
  167.         /**
  168.          * @var array $attributes An array of attributes.
  169.          */
  170.         protected $attributes = array( 'id', 'name', 'class', 'data', 'style' );
  171.        
  172.         /**
  173.          * @param array|string $attribute An array of attributes to add (or a space-separated string).
  174.          * @return self
  175.          */
  176.         protected function addAttribute( $attribute ) {
  177.             if ( is_string( $attribute ) ) $attribute = explode( ' ', trim( $attribute ) );
  178.             $this->attributes = array_unique( array_merge( $this->attributes, $attribute ), SORT_REGULAR );
  179.             return $this;
  180.         }
  181.        
  182.         /**
  183.          * @var string $element_type The type of HTML element.
  184.          */
  185.         protected $element_type;
  186.        
  187.         /**
  188.          * @return string The element type.
  189.          */
  190.         public function getElementType() {
  191.             return $this->element_type;
  192.         }
  193.        
  194.         /**
  195.          * @param string $element_type
  196.          * @return self
  197.          */
  198.         private function setElementType( $element_type ) {
  199.             $this->element_type = $element_type;
  200.             return $this;
  201.         }
  202.        
  203.         // void element flag
  204.         protected $void;
  205.        
  206.         private function isVoid() {
  207.             return $this->void;
  208.         }
  209.        
  210.         /**
  211.          * Sets void flag (whether the element has contents or not) based on element type.
  212.          */
  213.         protected function setVoid() {
  214.             // list from: https://www.w3.org/TR/html5/syntax.html#void-elements
  215.             switch ( $this->element_type ) {
  216.                 case 'area':
  217.                 case 'base':
  218.                 case 'br':
  219.                 case 'col':
  220.                 case 'embed':
  221.                 case 'hr':
  222.                 case 'img':
  223.                 case 'input':
  224.                 case 'keygen':
  225.                 case 'link':
  226.                 case 'meta':
  227.                 case 'param':
  228.                 case 'source':
  229.                 case 'track':
  230.                 case 'wbr':
  231.                     $this->void = true;
  232.                     break;
  233.                 default:
  234.                     $this->void = false;
  235.                     break;
  236.             }
  237.             return $this;
  238.         }
  239.        
  240.         /**
  241.          * @var string $id
  242.          */
  243.         protected $id;
  244.        
  245.         /**
  246.          * @return string|NULL The id.
  247.          */
  248.         public function getId() {
  249.             return $this->id;
  250.         }
  251.        
  252.         /**
  253.          * @param string|NULL $id
  254.          * @return self
  255.          */
  256.         private function setId( $id ) {
  257.             $this->id = $id;
  258.             return $this;
  259.         }
  260.        
  261.         /**
  262.          * @var string $name
  263.          */
  264.         protected $name;
  265.        
  266.         /**
  267.          * @return string Name
  268.          */
  269.         public function getName() {
  270.             return $this->name;
  271.         }
  272.        
  273.         /**
  274.          * @param string $name Name
  275.          * @return self
  276.          */
  277.         public function setName( $name ) {
  278.             $this->name = $name;
  279.             return $this;
  280.         }
  281.        
  282.         /**
  283.          * @var array $classes
  284.          */
  285.         protected $classes = array();
  286.        
  287.         /**
  288.          * @return array Array of classes
  289.          */
  290.         public function getClasses() {
  291.             return $this->classes;
  292.         }
  293.        
  294.         /**
  295.          * Clears current class array and adds provided classes
  296.          *
  297.          * @param string|array $classes A string of space-separated classes or an array of classes
  298.          * @return self
  299.          */
  300.         private function setClasses( $classes ) {
  301.             $this->classes = array();
  302.             $this->addClass( $classes );
  303.             return $this;
  304.         }
  305.        
  306.         /**
  307.          * Adds new class(es)
  308.          *
  309.          * @param string|array $classes A string of space-separated classes or an array of classes
  310.          * @return self
  311.          */
  312.         public function addClass( $class ) {
  313.             if ( is_string( $class ) ) $class = explode( ' ', trim( $class ) );
  314.             $this->classes = array_unique( array_merge( $this->classes, $class ), SORT_REGULAR );
  315.             return $this;
  316.         }
  317.        
  318.         /**
  319.          * @return string The class attribute string.
  320.          */
  321.         private function getClassAttribute() {
  322.             return empty( $this->classes ) ? '' : 'class="'.implode( ' ', $this->classes ).'"';
  323.         }
  324.        
  325.         /**
  326.          * @var array $style An associative array of CSS style properties mapped to their values.
  327.          */
  328.         protected $style = array();
  329.        
  330.         /**
  331.          * @return array An associative array of CSS style properties mapped to their values.
  332.          */
  333.         public function getStyle() {
  334.             return $this->style;
  335.         }
  336.        
  337.         /**
  338.          * @param string $property A CSS property.
  339.          * @param string $value The value of the given CSS property.
  340.          * @return self
  341.          */
  342.         public function setStyleProperty( $property, $value ) {
  343.             $this->style[ $property ] = $value;
  344.             return $this;
  345.         }
  346.        
  347.         /**
  348.          * Adds multiple CSS properties.
  349.          *
  350.          * @param array $properties An associative array of CSS style properties mapped to their values.
  351.          * @return self
  352.          */
  353.         public function setStyleProperties( $properties ) {
  354.             foreach ( $properties AS $property => $value ) $this->setStyleProperty( $property, $value );
  355.             return $this;
  356.         }
  357.        
  358.         /**
  359.          * Clears current style and adds the provided style.
  360.          *
  361.          * @param array|string $style An associative array of properties => values or a valid CSS string.
  362.          * @return self|boolean Returns false if invalid style provided.
  363.          */
  364.         public function setStyle( $style ) {
  365.             if ( is_array( $style ) ) $this->style = $style;
  366.             elseif ( is_string( $style ) && preg_match( '/^(?>\s*[^:]+\s*:\s*[^;]+\s*;\s*)+$/', $style ) === 1 ) {
  367.                 $properties = explode( ';', trim( $style, ' ;' ) );
  368.                 $this->style = array();
  369.                 foreach ( $properties AS $property ) {
  370.                     $property = explode( ':', $property );
  371.                     $this->setStyleProperty( trim( $property[ 0 ] ), trim( preg_replace( '/\s{2,}/', ' ', $property[ 1 ] ) ) );
  372.                 }
  373.             }
  374.             else return false; // invalid style provided
  375.             return $this;
  376.         }
  377.        
  378.         /**
  379.          * @return string The inline style attribute.
  380.          */
  381.         private function getStyleAttribute() {
  382.             if ( empty( $this->style ) ) return '';
  383.             $style = 'style="';
  384.             foreach ( $this->style AS $property => $value ) $style .= $property.':'.$value.';';
  385.             return $style.'"';
  386.         }
  387.        
  388.         /**
  389.          * @var array $data An associative array of data-* attributes mapped to their values.
  390.          */
  391.         protected $data = array();
  392.        
  393.         /**
  394.          * @return array An associative array of data-* attributes mapped to their values.
  395.          */
  396.         public function getData() {
  397.             return $this->data;
  398.         }
  399.        
  400.         /**
  401.          * Clears current data attributes and adds the new ones.
  402.          *
  403.          * @param array An associative array of data-* attributes mapped to their values.
  404.          * @return self;
  405.          */
  406.         private function setData( $data ) {
  407.             $this->data = array();
  408.             $this->addData( $data );
  409.             return $this;
  410.         }
  411.        
  412.         /**
  413.          * @param array An associative array of data-* attributes mapped to their values.
  414.          * @return self
  415.          */
  416.         public function addData( $data ) {
  417.             foreach ( $data AS $key => $value ) $this->data[ $key ] = $value;
  418.             return $this;
  419.         }
  420.        
  421.         /**
  422.          * @return string All data-* attributes condensed into a single string.
  423.          */
  424.         private function getDataAttribute() {
  425.             $data_attributes = '';
  426.             foreach ( $this->data AS $key => $value ) $data_attributes .= ' data-'.$key.'="'.$value.'"';
  427.             return ltrim( $data_attributes );
  428.         }
  429.        
  430.         /**
  431.          * @return string All attributes combined into a string.
  432.          */
  433.         protected function getAttributes() {
  434.             $attributes = '';
  435.             foreach ( $this->attributes AS $attribute ) {
  436.                 // check if this attribute requires special processing
  437.                 // to require custom processing, create a function named 'get<capitalized attribute name>Attribute'
  438.                 $get_method = 'get'.ucwords( $attribute ).'Attribute';
  439.                 if ( method_exists( $this, $get_method ) ) $attributes .= ' '.$this->{$get_method}();
  440.                 else {
  441.                     if ( is_bool( $this->{$attribute} ) ) {
  442.                         if ( !$this->{$attribute} ) continue;
  443.                         $attributes .= ' '.$attribute.'="'.$attribute.'"';
  444.                     }
  445.                     elseif ( isset( $this->{$attribute} ) ) $attributes .= ' '.$attribute.'="'.$this->{$attribute}.'"';
  446.                 }
  447.             }
  448.             return $attributes;
  449.         }
  450.        
  451.         /**
  452.          * @return string The HTML representation of the element.
  453.          */
  454.         public function getElement() {
  455.             return '<'.$this->getElementType().$this->getAttributes().'>';
  456.         }
  457.        
  458.         /**
  459.          * Print the element HTML.
  460.          */
  461.         public function output() {
  462.             echo $this->getElement();
  463.         }
  464.     }
  465.  
  466.     /**
  467.     * HTML_Element class
  468.     *
  469.     * HTML_Element class is used to create objects representing non-void HTML elements.
  470.     * Non-void elements are HTML elements that may contain content. These elements are
  471.     * formatted as '<tag>content</tag>'.
  472.     *
  473.     * @package -
  474.     * @subpackage HTML
  475.     * @author JBHUTT09
  476.     */
  477.     class HTML_Element extends HTML {
  478.        
  479.         /**
  480.          * @var string $text The text content of the element.
  481.          */
  482.         protected $text = '';
  483.        
  484.         /**
  485.          * @return string The element's text.
  486.          */
  487.         public function getText() {
  488.             return $this->text;
  489.         }
  490.        
  491.         /**
  492.          * @param string $text
  493.          * @return self
  494.          */
  495.         public function setText( $text ) {
  496.             $this->text = $text;
  497.             return $this;
  498.         }
  499.        
  500.         /**
  501.          * @param string $contents (optional) If provided, replace the element text with $contents.
  502.          * @return string The element's HTML string.
  503.          */
  504.         public function getElement( $contents=NULL ) {
  505.             return parent::getElement().( isset( $contents ) ? $contents : $this->getText() ).'</'.$this->getElementType().'>';
  506.         }
  507.        
  508.     }
  509.  
  510.     /**
  511.     * HTML_Container_Element class
  512.     *
  513.     * HTML_Container_Element class is used to create objects representing HTML elements
  514.     * which may contain child HTML elements.
  515.     *
  516.     * @package -
  517.     * @subpackage HTML
  518.     * @author JBHUTT09
  519.     */
  520.     class HTML_Container_Element extends HTML_Element {
  521.         /**
  522.          * @var array $children An array of child elements.
  523.          */
  524.         protected $children = array();
  525.        
  526.         /**
  527.          * @param HTML::AS_ARRAY|HTML::AS_STRING $format The return format.
  528.          * @return array|string An array of HTML based objects or an HTML string of all child elements.
  529.          */
  530.         public function getChildren( $format = HTML::AS_ARRAY ) {
  531.             if ( $format === HTML::AS_ARRAY ) return $this->children;
  532.             elseif ( $format === HTML::AS_STRING ) {
  533.                 $children = '';
  534.                 foreach ( $this->children AS $child ) $children .= $child->getElement();
  535.                 return $children;
  536.             }
  537.             return false;
  538.         }
  539.        
  540.         /**
  541.          * Add child element(s)
  542.          *
  543.          * @param HTML instance|array $child An HTML instance or an array of HTML instances
  544.          * return self
  545.          */
  546.         public function addChild( $child, $key=NULL ) {
  547.             if ( isset( $key ) ) $this->children[ $key ] = $child;
  548.             else $this->children[] = $child;
  549.             return $this;
  550.         }
  551.        
  552.         /**
  553.          * Add multiple children at once.
  554.          *
  555.          * @param HTML array $children An array of HTML instances
  556.          * return self
  557.          */
  558.         public function addChildren( Array $children ) {
  559.             foreach ( $children AS $key => $child ) {
  560.                 if ( is_string( $key ) ) $this->addChild( $child, $key );
  561.                 else $this->addChild( $child );
  562.             }
  563.             return $this;
  564.         }
  565.        
  566.         /**
  567.          * Clears current children and adds new child(ren)
  568.          *
  569.          * @param HTML instance|array $child An HTML instance or an array of HTML instances
  570.          * return self
  571.          */
  572.         protected function setChildren( $children ) {
  573.             $this->children = array();
  574.             $this->addChild( $children );
  575.             return $this;
  576.         }
  577.        
  578.         /**
  579.          * @var HTML::CONTENT_BEFORE|HTML::CONTENT_AFTER $format Order of text content and child elements.
  580.          */
  581.         protected $format = HTML::CONTENT_BEFORE;
  582.        
  583.         /**
  584.          * @param HTML::CONTENT_BEFORE|HTML::CONTENT_AFTER $format Order of text content and child elements.
  585.          * @return self
  586.          */
  587.         public function setFormat( $format ) {
  588.             if ( $format !== HTML::CONTENT_BEFORE && $format !== HTML::CONTENT_AFTER ) return false;
  589.             $this->format = $format;
  590.             return $this;
  591.         }
  592.        
  593.         /**
  594.          * @return string The element's HTML string.
  595.          */
  596.         public function getElement( $contents=NULL ) {
  597.             return parent::getElement( $this->getElementContents() );
  598.         }
  599.        
  600.         /**
  601.          * @return string The element's children and text. Order determined by set format.
  602.          * @see setFormat
  603.          */
  604.         protected function getElementContents() {
  605.             if ( $this->format === HTML::CONTENT_BEFORE ) return $this->getText().$this->getChildren( HTML::AS_STRING );
  606.             return $this->getChildren( HTML::AS_STRING ).$this->getText();
  607.         }
  608.     }
  609.    
  610.     /**
  611.     * HTML_Input class
  612.     *
  613.     * HTML_Input class is used to create objects representing HTML input elements.
  614.     *
  615.     * @package -
  616.     * @subpackage HTML
  617.     * @author JBHUTT09
  618.     */
  619.     class HTML_Input extends HTML {
  620.         use Disabled, Value;
  621.         /**
  622.          * @param string $id The id attribute.
  623.          * @param string $value The value attribute.
  624.          * @param string $type The type attribute.
  625.          * return self
  626.          */
  627.         function __construct( string $id, string $value, string $type ) {
  628.             parent::__construct( 'input', $id );
  629.             $this->addAttribute( array( 'disabled', 'type', 'value' ) )->setType( $type )->setValue( $value );
  630.             return $this;
  631.         }
  632.        
  633.         /**
  634.          * @var string $type The type attribute.
  635.          */
  636.         protected $type;
  637.        
  638.         /**
  639.          * @return string The type attribute.
  640.          */
  641.         public function getType() {
  642.             return $this->type;
  643.         }
  644.        
  645.         /**
  646.          * @param string $type
  647.          * @return self
  648.          */
  649.         private function setType( string $type ) {
  650.             $this->type = $type;
  651.             return $this;
  652.         }
  653.        
  654.     }
  655.    
  656.     /**
  657.     * HTML_Checkbox class
  658.     *
  659.     * HTML_Checkbox class is used to create objects representing HTML checkbox elements.
  660.     *
  661.     * @package -
  662.     * @subpackage HTML
  663.     * @author JBHUTT09
  664.     */
  665.     class HTML_Checkbox extends HTML_Input {
  666.         /**
  667.          * @param string $id The id attribute.
  668.          * @param string $value The value attribute.
  669.          * @param string $checked (optional) Defaults to false.
  670.          * return self
  671.          */
  672.         function __construct( string $id, string $value, boolean $checked ) {
  673.             parent::__construct( $id, $value, 'checkbox' );
  674.             $this->addAttribute( array( 'checked' ) )->setChecked( isset( $checked ) ? $checked : false );
  675.             return $this;
  676.         }
  677.        
  678.         /**
  679.          * @var boolean $checked
  680.          */
  681.         protected $checked;
  682.        
  683.         /**
  684.          * @return boolean True if checked, false if not.
  685.          */
  686.         public function getChecked() {
  687.             return $this->checked;
  688.         }
  689.        
  690.         /**
  691.          * @param boolean $checked
  692.          * @return self
  693.          */
  694.         public function setChecked( boolean $checked ) {
  695.             $this->checked = $checked;
  696.             return $this;
  697.         }
  698.     }
  699.    
  700.     /**
  701.     * HTML_Textarea class
  702.     *
  703.     * HTML_Textarea class is used to create objects representing HTML textarea elements.
  704.     *
  705.     * @package -
  706.     * @subpackage HTML
  707.     * @author JBHUTT09
  708.     */
  709.     class HTML_Textarea extends HTML_Element {
  710.         use Disabled, Required, Placeholder;
  711.         /**
  712.          * @param string $id The id attribute.
  713.          * @param string $text The text.
  714.          * return self
  715.          */
  716.         function __construct( string $id, $text='' ) {
  717.             parent::__construct( 'textarea', $id );
  718.             $this->addAttribute( array( 'required', 'disabled' ) )->setText( $text );
  719.             return $this;
  720.         }
  721.     }
  722.    
  723.     /**
  724.     * HTML_Textbox class
  725.     *
  726.     * HTML_Textbox class is used to create objects representing HTML text input elements.
  727.     *
  728.     * @package -
  729.     * @subpackage HTML
  730.     * @author JBHUTT09
  731.     */
  732.     class HTML_Textbox extends HTML_Input {
  733.         use Required, Placeholder;
  734.         /**
  735.          * @param string $id The id attribute.
  736.          * @param string $value (optional) The value attribute. Defaults to empty string.
  737.          * return self
  738.          */
  739.         function __construct( string $id, $value='' ) {
  740.             parent::__construct( $id, $value, 'text' );
  741.             $this->addAttribute( array( 'required' ) );
  742.             return $this;
  743.         }
  744.     }
  745.    
  746.     /**
  747.     * HTML_Form class
  748.     *
  749.     * HTML_Form class is used to create objects representing HTML form elements.
  750.     *
  751.     * @package -
  752.     * @subpackage HTML
  753.     * @author JBHUTT09
  754.     */
  755.     class HTML_Form extends HTML_Container_Element {
  756.         /**
  757.          * @param string $id The id attribute.
  758.          * @param string $action (optional) The action attribute. Defaults to 'javascript:void(0);'.
  759.          * return self
  760.          */
  761.         function __construct( string $id, $action='javascript:void(0);' ) {
  762.             parent::__construct( 'form', $id );
  763.             $this->addAttribute( array( 'action' ) )->setAction( $action );
  764.             return $this;
  765.         }
  766.        
  767.         /**
  768.          * @var string $action The action attribute.
  769.          */
  770.         protected $action;
  771.        
  772.         /**
  773.          * @return string The action attribute.
  774.          */
  775.         public function getAction() {
  776.             return $this->action;
  777.         }
  778.        
  779.         /**
  780.          * @param string $action The action attribute.
  781.          * @return self
  782.          */
  783.         public function setAction( string $action ) {
  784.             $this->action = $action;
  785.             return $this;
  786.         }
  787.     }
  788.    
  789.     /**
  790.     * HTML_Label class
  791.     *
  792.     * HTML_Label class is used to create objects representing HTML label elements.
  793.     *
  794.     * @package -
  795.     * @subpackage HTML
  796.     * @author JBHUTT09
  797.     */
  798.     class HTML_Label extends HTML_Container_Element {
  799.         /**
  800.          * @param string $id The id attribute.
  801.          * @param string $for (optional) The for attribute.
  802.          * return self
  803.          */
  804.         function __construct( $id=NULL, $for=NULL ) {
  805.             parent::__construct( 'label', $id );
  806.             $this->addAttribute( array( 'for' ) )->setFor( $for );
  807.             return $this;
  808.         }
  809.        
  810.         /**
  811.          * @var string The for attribute.
  812.          */
  813.         protected $for;
  814.        
  815.         /**
  816.          * @return string The for attribute.
  817.          */
  818.         public function getFor() {
  819.             return $this->{HTML::FOR_ATTR};
  820.         }
  821.        
  822.         /**
  823.          * @param string $for The for attribute.
  824.          * @return self
  825.          */
  826.         public function setFor( string $for ) {
  827.             $this->{HTML::FOR_ATTR} = $for;
  828.             return $this;
  829.         }
  830.     }
  831.    
  832.     /**
  833.     * HTML_Select class
  834.     *
  835.     * HTML_Select class is used to create objects representing HTML select elements.
  836.     *
  837.     * @package -
  838.     * @subpackage HTML
  839.     * @author JBHUTT09
  840.     */
  841.     class HTML_Select extends HTML_Container_Element {
  842.         use Disabled;
  843.         /**
  844.          * @param string $id The id attribute.
  845.          * @return self
  846.          */
  847.         function __construct( string $id, array $options=array() ) {
  848.             parent::__construct( 'select', $id );
  849.             $this->addOptions( $options );
  850.             return $this;
  851.         }
  852.        
  853.         /**
  854.          * @param string $value
  855.          * @param string $text
  856.          * @return self
  857.          */
  858.         public function addOption( string $value, string $text ) {
  859.             $this->addChild( ( new HTML_Option( $value, $text ) ) );
  860.             return $this;
  861.         }
  862.        
  863.         /**
  864.          * @param array $options Associative array of option value => text pairs
  865.          * @return self
  866.          */
  867.         public function addOptions( array $options ) {
  868.             foreach ( $options AS $value => $text ) $this->addOption( $value, $text );
  869.             return $this;
  870.         }
  871.        
  872.         /**
  873.          * @param HTML::GET_OPTION|HTML::GET_VALUE $return (optional) Default HTML::GET_OPTION
  874.          * @return HTML_Option|string Depends on $return. The selected HTML_Option object, or said object's value attribute.
  875.          */
  876.         public function getSelected( $return=HTML::GET_OPTION ) {
  877.             $default = false;
  878.             foreach ( $this->children AS &$option ) {
  879.                 if ( $default === false ) $default = $option;
  880.                 if ( $option->getSelected() ) return ( $return === HTML::GET_OPTION ) ? $option : $option->getValue();
  881.             }
  882.             return ( $return === HTML::GET_OPTION ) ? $default : $default->getValue();
  883.         }
  884.        
  885.         /**
  886.          * @param string $value The value attribute of the option to select
  887.          * @return self
  888.          */
  889.         public function setSelected( string $value ) {
  890.             foreach ( $this->children AS &$option ) $option->setSelected( ( $option->getValue() == $value ) );
  891.             return $this;
  892.         }
  893.        
  894.         /**
  895.          * @param HTML::BY_TEXT|HTML::BY_VALUE $sort_by
  896.          * @param HTML::ASC|HTML::DESC $order
  897.          * @return self
  898.          */
  899.         public function sortOptions( $sort_by = HTML::BY_TEXT, $order = HTML::ASC ) {
  900.             if ( $sort_by === HTML::BY_TEXT ) {
  901.                 if ( $order === HTML::ASC ) usort( $this->children, function( $a, $b ) { return strcmp( $a->getText(), $b->getText() ); } );
  902.                 elseif ( $order === HTML::DESC ) usort( $this->children, function( $a, $b ) { return strcmp( $b->getText(), $a->getText() ); } );
  903.             }
  904.             elseif ( $sort_by === HTML::BY_VALUE ) {
  905.                 if ( $order === HTML::ASC ) usort( $this->children, function( $a, $b ) { return strcmp( $a->getValue(), $b->getValue() ); } );
  906.                 elseif ( $order === HTML::DESC ) usort( $this->children, function( $a, $b ) { return strcmp( $b->getValue(), $a->getValue() ); } );
  907.             }
  908.             return $this;
  909.         }
  910.     }
  911.    
  912.     /**
  913.      * HTML_Option class
  914.      *
  915.      * HTML_Option class is used to create objects representing HTML option elements.
  916.      *
  917.      * @package -
  918.      * @subpackage HTML
  919.      * @author JBHUTT09
  920.      */
  921.     class HTML_Option extends HTML_Container_Element {
  922.         use Value;
  923.         /**
  924.          * @param string $value
  925.          * @param string $text
  926.          * @return self
  927.          */
  928.         function __construct( string $value, string $text ) {
  929.             parent::__construct( 'option' );
  930.             $this->addAttribute( array( 'value', 'selected' ) )->setValue( $value )->setText( $text );
  931.             return $this;
  932.         }
  933.        
  934.         /**
  935.          * @var boolean $selected
  936.          */
  937.         protected $selected = false;
  938.        
  939.         /**
  940.          * @return boolean True if selected, false if not.
  941.          */
  942.         public function getSelected() {
  943.             return $this->selected;
  944.         }
  945.        
  946.         /**
  947.          * @param boolean $selected
  948.          * @return self
  949.          */
  950.         public function setSelected( bool $selected ) {
  951.             $this->selected = $selected;
  952.             return $this;
  953.         }
  954.     }
  955. ?>
Advertisement
Add Comment
Please, Sign In to add comment