| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file contains Html Attribute Builder. |
| 4 |
* |
| 5 |
* @author Alex Kovalev <alex.kovalevv@gmail.com> |
| 6 |
* @copyright (c) 2018, Webcraftic Ltd |
| 7 |
* |
| 8 |
* @package factory-forms |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if( !defined('ABSPATH') ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if( !class_exists('Wbcr_FactoryForms460_HtmlAttributeBuilder') ) { |
| 18 |
/** |
| 19 |
* Html Attribute Builder |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Wbcr_FactoryForms460_HtmlAttributeBuilder { |
| 24 |
|
| 25 |
/** |
| 26 |
* An array to store css classes. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @var string[] |
| 30 |
*/ |
| 31 |
protected $css_classes = array(); |
| 32 |
|
| 33 |
/** |
| 34 |
* An array to store html attributes. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @var string[] |
| 38 |
*/ |
| 39 |
protected $html_attrs = array(); |
| 40 |
|
| 41 |
/** |
| 42 |
* An array to store html data. |
| 43 |
* |
| 44 |
* @since 1.0.0 |
| 45 |
* @var string[] |
| 46 |
*/ |
| 47 |
protected $html_data = array(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Adds a new CSS class. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function addCssClass($class) |
| 56 |
{ |
| 57 |
if( !is_array($class) ) { |
| 58 |
$this->css_classes[] = $class; |
| 59 |
} else { |
| 60 |
$this->css_classes = array_merge($this->css_classes, $class); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Prints CSS classes. |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function printCssClass() |
| 71 |
{ |
| 72 |
echo implode(' ', $this->css_classes); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Adds a new html data item. |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* @param string $dataKey |
| 80 |
* @param string $dataValue |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function addHtmlData($dataKey, $dataValue) |
| 84 |
{ |
| 85 |
$this->html_data[$dataKey] = $dataValue; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Prints html data items. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function printHtmlData() |
| 95 |
{ |
| 96 |
foreach($this->html_data as $key => $value) { |
| 97 |
echo 'data-' . $key . '="' . $value . '" '; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Adds a new html attribute. |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* @param string $attr_name |
| 106 |
* @param string $attr_value |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function addHtmlAttr($attr_name, $attr_value) |
| 110 |
{ |
| 111 |
$this->html_attrs[$attr_name] = $attr_value; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Prints all html attributes, including css classes and data. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function printAttrs() |
| 121 |
{ |
| 122 |
$attrs = $this->html_attrs; |
| 123 |
|
| 124 |
if( !empty($this->css_classes) ) { |
| 125 |
$attrs['class'] = implode(' ', $this->css_classes); |
| 126 |
} |
| 127 |
|
| 128 |
foreach($this->html_data as $data_key => $data_value) { |
| 129 |
$attrs['data-' . $data_key] = $data_value; |
| 130 |
} |
| 131 |
|
| 132 |
foreach($attrs as $key => $value) { |
| 133 |
echo $key . '="' . $value . '" '; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |