Element.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace dokuwiki\Form;
  3. /**
  4. * Class Element
  5. *
  6. * The basic building block of a form
  7. *
  8. * @package dokuwiki\Form
  9. */
  10. abstract class Element
  11. {
  12. /**
  13. * @var array the attributes of this element
  14. */
  15. protected $attributes = array();
  16. /**
  17. * @var string The type of this element
  18. */
  19. protected $type;
  20. /**
  21. * @param string $type The type of this element
  22. * @param array $attributes
  23. */
  24. public function __construct($type, $attributes = array())
  25. {
  26. $this->type = $type;
  27. $this->attributes = $attributes;
  28. }
  29. /**
  30. * Type of this element
  31. *
  32. * @return string
  33. */
  34. public function getType()
  35. {
  36. return $this->type;
  37. }
  38. /**
  39. * Gets or sets an attribute
  40. *
  41. * When no $value is given, the current content of the attribute is returned.
  42. * An empty string is returned for unset attributes.
  43. *
  44. * When a $value is given, the content is set to that value and the Element
  45. * itself is returned for easy chaining
  46. *
  47. * @param string $name Name of the attribute to access
  48. * @param null|string $value New value to set
  49. * @return string|$this
  50. */
  51. public function attr($name, $value = null)
  52. {
  53. // set
  54. if ($value !== null) {
  55. $this->attributes[$name] = $value;
  56. return $this;
  57. }
  58. // get
  59. if (isset($this->attributes[$name])) {
  60. return $this->attributes[$name];
  61. } else {
  62. return '';
  63. }
  64. }
  65. /**
  66. * Removes the given attribute if it exists
  67. *
  68. * @param string $name
  69. * @return $this
  70. */
  71. public function rmattr($name)
  72. {
  73. if (isset($this->attributes[$name])) {
  74. unset($this->attributes[$name]);
  75. }
  76. return $this;
  77. }
  78. /**
  79. * Gets or adds a all given attributes at once
  80. *
  81. * @param array|null $attributes
  82. * @return array|$this
  83. */
  84. public function attrs($attributes = null)
  85. {
  86. // set
  87. if ($attributes) {
  88. foreach ((array) $attributes as $key => $val) {
  89. $this->attr($key, $val);
  90. }
  91. return $this;
  92. }
  93. // get
  94. return $this->attributes;
  95. }
  96. /**
  97. * Adds a class to the class attribute
  98. *
  99. * This is the preferred method of setting the element's class
  100. *
  101. * @param string $class the new class to add
  102. * @return $this
  103. */
  104. public function addClass($class)
  105. {
  106. $classes = explode(' ', $this->attr('class'));
  107. $classes[] = $class;
  108. $classes = array_unique($classes);
  109. $classes = array_filter($classes);
  110. $this->attr('class', join(' ', $classes));
  111. return $this;
  112. }
  113. /**
  114. * Get or set the element's ID
  115. *
  116. * This is the preferred way of setting the element's ID
  117. *
  118. * @param null|string $id
  119. * @return string|$this
  120. */
  121. public function id($id = null)
  122. {
  123. if (strpos($id, '__') === false) {
  124. throw new \InvalidArgumentException('IDs in DokuWiki have to contain two subsequent underscores');
  125. }
  126. return $this->attr('id', $id);
  127. }
  128. /**
  129. * Get or set the element's value
  130. *
  131. * This is the preferred way of setting the element's value
  132. *
  133. * @param null|string $value
  134. * @return string|$this
  135. */
  136. public function val($value = null)
  137. {
  138. return $this->attr('value', $value);
  139. }
  140. /**
  141. * The HTML representation of this element
  142. *
  143. * @return string
  144. */
  145. abstract public function toHTML();
  146. }