/ConsoleTools/src/dialog/validators/question_dialog_type.php

https://github.com/F5/zetacomponents · PHP · 243 lines · 113 code · 10 blank · 120 comment · 15 complexity · 3b890cc452629f64150034e0c6eb7665 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcConsoleQuestionDialogTypeValidator class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package ConsoleTools
  23. * @version //autogentag//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. * @filesource
  26. */
  27. /**
  28. * Validator class to validate a certain data type.
  29. * Validator class for ezcConsoleQuestionDialog objects that validates a
  30. * certain datatype.
  31. *
  32. * @package ConsoleTools
  33. * @version //autogen//
  34. *
  35. * @property int $type
  36. * One of ezcConsoleQuestionDialogTypeValidator::TYPE_*. The type to
  37. * check against and convert results to.
  38. * @property mixed $default
  39. * A default value if no result given.
  40. */
  41. class ezcConsoleQuestionDialogTypeValidator implements ezcConsoleQuestionDialogValidator
  42. {
  43. /**
  44. * Data type string.
  45. */
  46. const TYPE_STRING = 0;
  47. /**
  48. * Data type int.
  49. */
  50. const TYPE_INT = 1;
  51. /**
  52. * Data type float.
  53. */
  54. const TYPE_FLOAT = 2;
  55. /**
  56. * Data type bool.
  57. * The results 1 and "true" will be cased to true, 0 and "false" to false.
  58. */
  59. const TYPE_BOOL = 3;
  60. /**
  61. * Properties.
  62. *
  63. * @var array
  64. */
  65. protected $properties = array(
  66. "type" => self::TYPE_STRING,
  67. "default" => null,
  68. );
  69. /**
  70. * Creates a new question dialog type validator.
  71. * Creates a new question dialog type validator, which validates the result
  72. * specified to be of a certaon type. The $type must be one of the TYPE_*
  73. * constants in this class. If no value is provided by the user a possibly
  74. * set $default value is used instead.
  75. *
  76. * @param int $type One of ezcConsoleQuestionDialogTypeValidator::TYPE_*.
  77. * @param mixed $default Default value according to $type.
  78. * @return void
  79. */
  80. public function __construct( $type = self::TYPE_STRING, $default = null )
  81. {
  82. $this->type = $type;
  83. $this->default = $default;
  84. }
  85. /**
  86. * Returns if the given result is valid.
  87. * Returns if the result is of the given type.
  88. *
  89. * @param mixed $result The result to check.
  90. * @return bool True if the result is valid. Otherwise false.
  91. */
  92. public function validate( $result )
  93. {
  94. if ( $result === "" )
  95. {
  96. return $this->default !== null;
  97. }
  98. switch ( $this->type )
  99. {
  100. case self::TYPE_INT:
  101. return is_int( $result );
  102. case self::TYPE_FLOAT:
  103. return is_float( $result );
  104. case self::TYPE_BOOL:
  105. return is_bool( $result );
  106. case self::TYPE_STRING:
  107. default:
  108. return is_string( $result );
  109. }
  110. }
  111. /**
  112. * Returns a fixed version of the result, if possible.
  113. * Returns the value casted into the correct type or the default value, if
  114. * it exists and the result is empty.
  115. *
  116. * @param mixed $result The result received.
  117. * @return mixed The manipulated result.
  118. */
  119. public function fixup( $result )
  120. {
  121. if ( $result === "" && $this->default !== null )
  122. {
  123. return $this->default;
  124. }
  125. switch ( $this->type )
  126. {
  127. case self::TYPE_INT:
  128. return ( preg_match( "/^[0-9\-]+$/", $result ) !== 0 ) ? (int) $result : $result;
  129. case self::TYPE_FLOAT:
  130. return ( preg_match( "/^[0-9.E\-]+$/i", $result ) !== 0 ) ? (float) $result : $result;
  131. case self::TYPE_BOOL:
  132. switch ( $result )
  133. {
  134. case "1":
  135. case "true":
  136. return true;
  137. case "0":
  138. case "false":
  139. return false;
  140. }
  141. case self::TYPE_STRING:
  142. default:
  143. return $result;
  144. }
  145. }
  146. /**
  147. * Returns a string representing valid results.
  148. * Returns the string that can will be displayed with the question to
  149. * indicate valid results to the user and a possibly set default, if
  150. * available.
  151. *
  152. * @return string
  153. */
  154. public function getResultString()
  155. {
  156. $res = "(<%s>)" . ( $this->default !== null ? " [{$this->default}]" : "" );
  157. switch ( $this->type )
  158. {
  159. case self::TYPE_INT:
  160. return sprintf( $res, "int" );
  161. case self::TYPE_FLOAT:
  162. return sprintf( $res, "float" );
  163. case self::TYPE_BOOL:
  164. return sprintf( $res, "bool" );
  165. case self::TYPE_STRING:
  166. default:
  167. return sprintf( $res, "string" );
  168. }
  169. }
  170. /**
  171. * Property read access.
  172. *
  173. * @param string $propertyName Name of the property.
  174. * @return mixed Value of the property or null.
  175. *
  176. * @throws ezcBasePropertyNotFoundException
  177. * If the the desired property is not found.
  178. * @ignore
  179. */
  180. public function __get( $propertyName )
  181. {
  182. if ( isset( $this->$propertyName ) )
  183. {
  184. return $this->properties[$propertyName];
  185. }
  186. throw new ezcBasePropertyNotFoundException( $propertyName );
  187. }
  188. /**
  189. * Property write access.
  190. *
  191. * @param string $propertyName Name of the property.
  192. * @param mixed $propertyValue The value for the property.
  193. *
  194. * @throws ezcBasePropertyNotFoundException
  195. * If a the value for the property options is not an instance of
  196. * @throws ezcBaseValueException
  197. * If a the value for a property is out of range.
  198. * @ignore
  199. */
  200. public function __set( $propertyName, $propertyValue )
  201. {
  202. switch ( $propertyName )
  203. {
  204. case "type":
  205. if ( $propertyValue !== self::TYPE_STRING && $propertyValue !== self::TYPE_INT && $propertyValue !== self::TYPE_FLOAT && $propertyValue !== self::TYPE_BOOL )
  206. {
  207. throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleQuestionDialogTypeValidator::TYPE_*" );
  208. }
  209. break;
  210. case "default":
  211. if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
  212. {
  213. throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
  214. }
  215. break;
  216. default:
  217. throw new ezcBasePropertyNotFoundException( $propertyName );
  218. }
  219. $this->properties[$propertyName] = $propertyValue;
  220. }
  221. /**
  222. * Property isset access.
  223. *
  224. * @param string $propertyName Name of the property.
  225. * @return bool True is the property is set, otherwise false.
  226. * @ignore
  227. */
  228. public function __isset( $propertyName )
  229. {
  230. return array_key_exists( $propertyName, $this->properties );
  231. }
  232. }
  233. ?>