PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/Form/Element.php

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