Posted By

mb2o on 04/15/11


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mb2o


Easily create html elements using PHP


 / Published in: PHP
 

  1. <?php
  2. /**
  3. * Create an element
  4. */
  5. class Element
  6. {
  7. private $type;
  8. private $unaryTagArray = array('input', 'img', 'hr', 'br', 'meta', 'link');
  9. private $attributeArray;
  10. private $innerHtml;
  11.  
  12. /**
  13. * Constructor
  14. *
  15. * @param <type> $type
  16. * @param <type> $attributeArray
  17. * @param <type> $unaryTagArray
  18. */
  19. public function __construct($type, $attributeArray = array())
  20. {
  21. $this->type = strtolower($type);
  22. foreach($attributeArray as $attribute => $value) {
  23. $this->setAttribute($attribute, $value);
  24. }
  25. return $this;
  26. }
  27.  
  28. /**
  29. * Get one of the element's attributes
  30. *
  31. * @param <type> $attribute
  32. * @return <type>
  33. */
  34. public function getAttribute($attribute)
  35. {
  36. return $this->attributeArray[$attribute];
  37. }
  38.  
  39. /**
  40. * Set an array, can pass an array or a key, value combination
  41. *
  42. * @param <type> $attribute
  43. * @param <type> $value
  44. */
  45. function setAttribute($attribute, $value = "")
  46. {
  47. if(!is_array($attribute)) {
  48. $this->attributeArray[$attribute] = $value;
  49. }
  50. else {
  51. $this->attributeArray = array_merge($this->attributeArray, $attribute);
  52. }
  53. return $this;
  54. }
  55.  
  56. /**
  57. * Remove an attribute from an element
  58. *
  59. * @param <type> $attribute
  60. */
  61. function removeAttribute($attribute)
  62. {
  63. if(isset($this->attributeArray[$attribute])) {
  64. unset($this->attributeArray[$attribute]);
  65. }
  66. return $this;
  67. }
  68.  
  69. /**
  70. * Clear all of the element's attributes
  71. */
  72. function clearAttributes() {
  73. $this->attributeArray = array();
  74. return $this;
  75. }
  76.  
  77. /**
  78. * Insert an element into the current element
  79. *
  80. * @param <type> $object
  81. */
  82. function insert($object)
  83. {
  84. if(@get_class($object) == __class__) {
  85. $this->innerHtml .= $object->build();
  86. }
  87. return $this;
  88. }
  89.  
  90. /**
  91. * Set the innerHtml of an element
  92. *
  93. * @param <type> $object
  94. * @return <type>
  95. */
  96. function update($object)
  97. {
  98. $this->innerHtml = $object;
  99. return $this;
  100. }
  101.  
  102. /**
  103. * Builds the element
  104. *
  105. * @return <type>
  106. */
  107. function build()
  108. {
  109. // Start the tag
  110. $element = "<".$this->type;
  111. // Add attributes
  112. if(count($this->attributeArray)) {
  113. foreach($this->attributeArray as $key => $value) {
  114. $element .= " ".$key."=\"".$value."\"";
  115. }
  116. }
  117. // Close the element
  118. if(!in_array($this->type, $this->unaryTagArray)) {
  119. $element.= ">\n".$this->innerHtml."\n</".$this->type.">\n";
  120. }
  121. else {
  122. $element.= " />\n";
  123. }
  124. return $element;
  125. }
  126.  
  127. /**
  128. * Echoes out the element
  129. *
  130. * @return <type>
  131. */
  132. function __toString()
  133. {
  134. return $this->build();
  135. }
  136. }

Report this snippet  

You need to login to post a comment.