htmltag.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class HTMLTag
  27. {
  28. protected $tag;
  29. protected $usesPrettyPrint = true;
  30. protected $isSingleton;
  31. protected $attributes = array ();
  32. protected $children = array ();
  33. public function __construct ($tag = '', array $attributes = array (), $pretty = true, $singleton = false, $spaced = true)
  34. {
  35. if (!is_string ($tag) or !$this->isWord ($tag)) {
  36. $this->throwError ('Tag must have a single word String value.');
  37. return;
  38. }
  39. if (!is_bool ($singleton)) {
  40. $this->throwError ('Singleton parameter must have a Boolean value.');
  41. return;
  42. }
  43. if (!is_bool ($pretty)) {
  44. $this->throwError ('Pretty Print parameter must have a Boolean value.');
  45. return;
  46. }
  47. $this->tag = !empty ($tag) ? $tag : 'span';
  48. $this->createAttributes ($attributes, $spaced);
  49. $this->usesPrettyPrint = $pretty;
  50. $this->isSingleton = $singleton;
  51. }
  52. public function __get ($name)
  53. {
  54. switch ($name) {
  55. case 'innerHTML': {
  56. return $this->getInnerHTML ();
  57. }
  58. }
  59. }
  60. protected function isWord ($string)
  61. {
  62. if (empty ($string)) {
  63. return false;
  64. }
  65. if (!preg_match ('/[a-z0-9:-_.]+/i', $string)) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. protected function throwError ($error)
  71. {
  72. $backtrace = debug_backtrace ();
  73. $line = $backtrace[1]['line'];
  74. throw new \Exception ('Error on line ' . $line . ': ' . $error);
  75. }
  76. protected function getInnerHTML ()
  77. {
  78. $inner_html = array ();
  79. foreach ($this->children as $child) {
  80. if (is_object ($child)) {
  81. $inner_html[] = $child->asHTML ();
  82. continue;
  83. }
  84. $inner_html[] = $child;
  85. }
  86. return implode (PHP_EOL, $inner_html);
  87. }
  88. public function createAttribute ($name = '', $value = '', $spaced = true)
  89. {
  90. if (!is_string ($name) or !$this->isWord ($name)) {
  91. $this->throwError ('Attribute name must have a single word String value.');
  92. return false;
  93. }
  94. if (is_array ($value)) {
  95. $glue = ($spaced !== false) ? ' ' : '';
  96. $this->attributes[$name] = implode ($glue, $value);
  97. return true;
  98. }
  99. $this->attributes[$name] = $value;
  100. return true;
  101. }
  102. public function innerHTML ($html = '')
  103. {
  104. if ($this->isSingleton === true) {
  105. $this->throwError ('Singleton tags do not have innerHTML.');
  106. return false;
  107. }
  108. if (!empty ($html)) {
  109. $this->children = array ($html);
  110. }
  111. return true;
  112. }
  113. public function appendInnerHTML ($html = '')
  114. {
  115. if ($this->isSingleton === true) {
  116. $this->throwError ('Singleton tags do not have innerHTML.');
  117. return false;
  118. }
  119. if (!empty ($html)) {
  120. $this->children[] = $html;
  121. }
  122. return true;
  123. }
  124. public function createAttributes (array $attributes, $spaced = true)
  125. {
  126. if (!is_array ($attributes)) {
  127. $this->throwError ('Attributes parameter must have an Array value.');
  128. return;
  129. }
  130. foreach ($attributes as $key => $value) {
  131. switch ($key) {
  132. case 'children': {
  133. if (is_array ($value)) {
  134. for ($i = 0, $il = count ($value); $i < $il; $i++) {
  135. $this->appendChild ($value[$i]);
  136. }
  137. }
  138. break;
  139. }
  140. case 'innerHTML': {
  141. $this->innerHTML ($value);
  142. break;
  143. }
  144. default: {
  145. $this->createAttribute ($key, $value, $spaced);
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. public function appendAttribute ($name = '', $value = '', $spaced = true)
  152. {
  153. if (!is_string ($name) or !$this->isWord ($name)) {
  154. $this->throwError ('Attribute name must have a single word String value.');
  155. return false;
  156. }
  157. if (!empty ($this->attributes[$name])) {
  158. if ($spaced !== false) {
  159. $this->attributes[$name] .= ' ';
  160. }
  161. } else {
  162. $this->attributes[$name] = '';
  163. }
  164. if (is_array ($value)) {
  165. $glue = ($spaced !== false) ? ' ' : '';
  166. $this->attributes[$name] .= implode ($glue, $value);
  167. return true;
  168. }
  169. $this->attributes[$name] .= $value;
  170. return true;
  171. }
  172. public function appendAttributes (array $attributes, $spaced = true)
  173. {
  174. if (!is_array ($attributes)) {
  175. $this->throwError ('Attributes parameter must have an Array value.');
  176. return;
  177. }
  178. foreach ($attributes as $key => $value) {
  179. if ($key === 'innerHTML') {
  180. $this->appendInnerHTML ($value);
  181. continue;
  182. }
  183. $this->appendAttribute ($key, $value, $spaced);
  184. }
  185. }
  186. public function appendChild (HTMLTag $object)
  187. {
  188. if ($this->isSingleton === true) {
  189. $this->throwError ('Singleton tags do not have children.');
  190. return false;
  191. }
  192. if (!is_object ($object)) {
  193. $given_type = ucwords (gettype ($object));
  194. $this->throwError ($given_type . ' given, when Object is expected.');
  195. return false;
  196. }
  197. $this->children[] = $object;
  198. return true;
  199. }
  200. public function asHTML ()
  201. {
  202. $attributes = '';
  203. foreach ($this->attributes as $attribute => $value) {
  204. $attributes .= ' ' . $attribute . '="' . $value . '"';
  205. }
  206. $tag = '<' . $this->tag . $attributes . '>';
  207. if ($this->isSingleton === false) {
  208. if (!empty ($this->children)) {
  209. $inner_html = $this->getInnerHTML ();
  210. if ($this->usesPrettyPrint !== false) {
  211. $tag .= PHP_EOL . "\t";
  212. $tag .= str_replace (PHP_EOL, PHP_EOL . "\t", $inner_html);
  213. $tag .= PHP_EOL;
  214. } else {
  215. $tag .= $inner_html;
  216. }
  217. }
  218. $tag .= '</' . $this->tag . '>';
  219. }
  220. return $tag;
  221. }
  222. }