FormOptions.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Helper class to keep track of options when mixing links and form elements.
  4. *
  5. * @author Niklas Laxström
  6. * @copyright Copyright © 2008, Niklas Laxström
  7. */
  8. class FormOptions implements ArrayAccess {
  9. const AUTO = -1; //! Automatically detects simple data types
  10. const STRING = 0;
  11. const INT = 1;
  12. const BOOL = 2;
  13. const INTNULL = 3; //! Useful for namespace selector
  14. protected $options = array();
  15. # Setting up
  16. public function add( $name, $default, $type = self::AUTO ) {
  17. $option = array();
  18. $option['default'] = $default;
  19. $option['value'] = null;
  20. $option['consumed'] = false;
  21. if ( $type !== self::AUTO ) {
  22. $option['type'] = $type;
  23. } else {
  24. $option['type'] = self::guessType( $default );
  25. }
  26. $this->options[$name] = $option;
  27. }
  28. public function delete( $name ) {
  29. $this->validateName( $name, true );
  30. unset($this->options[$name]);
  31. }
  32. public static function guessType( $data ) {
  33. if ( is_bool($data) ) {
  34. return self::BOOL;
  35. } elseif( is_int($data) ) {
  36. return self::INT;
  37. } elseif( is_string($data) ) {
  38. return self::STRING;
  39. } else {
  40. throw new MWException( 'Unsupported datatype' );
  41. }
  42. }
  43. # Handling values
  44. public function validateName( $name, $strict = false ) {
  45. if ( !isset($this->options[$name]) ) {
  46. if ( $strict ) {
  47. throw new MWException( "Invalid option $name" );
  48. } else {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. public function setValue( $name, $value, $force = false ) {
  55. $this->validateName( $name, true );
  56. if ( !$force && $value === $this->options[$name]['default'] ) {
  57. // null default values as unchanged
  58. $this->options[$name]['value'] = null;
  59. } else {
  60. $this->options[$name]['value'] = $value;
  61. }
  62. }
  63. public function getValue( $name ) {
  64. $this->validateName( $name, true );
  65. return $this->getValueReal( $this->options[$name] );
  66. }
  67. protected function getValueReal( $option ) {
  68. if ( $option['value'] !== null ) {
  69. return $option['value'];
  70. } else {
  71. return $option['default'];
  72. }
  73. }
  74. public function reset( $name ) {
  75. $this->validateName( $name, true );
  76. $this->options[$name]['value'] = null;
  77. }
  78. public function consumeValue( $name ) {
  79. $this->validateName( $name, true );
  80. $this->options[$name]['consumed'] = true;
  81. return $this->getValueReal( $this->options[$name] );
  82. }
  83. public function consumeValues( /*Array*/ $names ) {
  84. $out = array();
  85. foreach ( $names as $name ) {
  86. $this->validateName( $name, true );
  87. $this->options[$name]['consumed'] = true;
  88. $out[] = $this->getValueReal( $this->options[$name] );
  89. }
  90. return $out;
  91. }
  92. # Validating values
  93. public function validateIntBounds( $name, $min, $max ) {
  94. $this->validateName( $name, true );
  95. if ( $this->options[$name]['type'] !== self::INT )
  96. throw new MWException( "Option $name is not of type int" );
  97. $value = $this->getValueReal( $this->options[$name] );
  98. $value = max( $min, min( $max, $value ) );
  99. $this->setValue( $name, $value );
  100. }
  101. # Getting the data out for use
  102. public function getUnconsumedValues( $all = false ) {
  103. $values = array();
  104. foreach ( $this->options as $name => $data ) {
  105. if ( !$data['consumed'] ) {
  106. if ( $all || $data['value'] !== null ) {
  107. $values[$name] = $this->getValueReal( $data );
  108. }
  109. }
  110. }
  111. return $values;
  112. }
  113. public function getChangedValues() {
  114. $values = array();
  115. foreach ( $this->options as $name => $data ) {
  116. if ( $data['value'] !== null ) {
  117. $values[$name] = $data['value'];
  118. }
  119. }
  120. return $values;
  121. }
  122. public function getAllValues() {
  123. $values = array();
  124. foreach ( $this->options as $name => $data ) {
  125. $values[$name] = $this->getValueReal( $data );
  126. }
  127. return $values;
  128. }
  129. # Reading values
  130. public function fetchValuesFromRequest( WebRequest $r, $values = false ) {
  131. if ( !$values ) {
  132. $values = array_keys($this->options);
  133. }
  134. foreach ( $values as $name ) {
  135. $default = $this->options[$name]['default'];
  136. $type = $this->options[$name]['type'];
  137. switch( $type ) {
  138. case self::BOOL:
  139. $value = $r->getBool( $name, $default ); break;
  140. case self::INT:
  141. $value = $r->getInt( $name, $default ); break;
  142. case self::STRING:
  143. $value = $r->getText( $name, $default ); break;
  144. case self::INTNULL:
  145. $value = $r->getIntOrNull( $name ); break;
  146. default:
  147. throw new MWException( 'Unsupported datatype' );
  148. }
  149. if ( $value !== null ) {
  150. $this->options[$name]['value'] = $value === $default ? null : $value;
  151. }
  152. }
  153. }
  154. /* ArrayAccess methods */
  155. public function offsetExists( $name ) {
  156. return isset($this->options[$name]);
  157. }
  158. public function offsetGet( $name ) {
  159. return $this->getValue( $name );
  160. }
  161. public function offsetSet( $name, $value ) {
  162. return $this->setValue( $name, $value );
  163. }
  164. public function offsetUnset( $name ) {
  165. return $this->delete( $name );
  166. }
  167. }