## Apache Zeta Components Manual :: File Source for question_dialog_type.php

## Source for file question_dialog_type.php

Documentation is available at question_dialog_type.php

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. */
28. /**
29. * Validator class to validate a certain data type.
30. * Validator class for ezcConsoleQuestionDialog objects that validates a
31. * certain datatype.
32. *
33. * @package ConsoleTools
34. * @version //autogen//
35. *
36. * @property int $type
37. * One of ezcConsoleQuestionDialogTypeValidator::TYPE_*. The type to
38. * check against and convert results to.
39. * @property mixed $default
40. * A default value if no result given.
41. */
42. class ezcConsoleQuestionDialogTypeValidator implements ezcConsoleQuestionDialogValidator
43. {
44. /**
45. * Data type string.
46. */
47. const TYPE_STRING = 0;
48. /**
49. * Data type int.
50. */
51. const TYPE_INT = 1;
52. /**
53. * Data type float.
54. */
55. const TYPE_FLOAT = 2;
56. /**
57. * Data type bool.
58. * The results 1 and "true" will be cased to true, 0 and "false" to false.
59. */
60. const TYPE_BOOL = 3;
62. /**
63. * Properties.
64. *
65. * @var array
66. */
67. protected $properties = array(
68. "type" => self::TYPE_STRING,
69. "default" => null,
70. );
72. /**
73. * Creates a new question dialog type validator.
74. * Creates a new question dialog type validator, which validates the result
75. * specified to be of a certaon type. The $type must be one of the TYPE_*
76. * constants in this class. If no value is provided by the user a possibly
77. * set $default value is used instead.
78. *
79. * @param int $type One of ezcConsoleQuestionDialogTypeValidator::TYPE_*.
80. * @param mixed $default Default value according to $type.
81. * @return void
82. */
83. public function __construct ( $type = self::TYPE_STRING, $default = null )
84. {
85. $this->type = $type;
86. $this->default = $default;
87. }
89. /**
90. * Returns if the given result is valid.
91. * Returns if the result is of the given type.
92. *
93. * @param mixed $result The result to check.
94. * @return bool True if the result is valid. Otherwise false.
95. */
96. public function validate ( $result )
97. {
98. if ( $result === "" )
99. {
100. return $this->default !== null;
101. }
102. switch ( $this->type )
103. {
104. case self::TYPE_INT:
105. return is_int ( $result );
106. case self::TYPE_FLOAT:
107. return is_float ( $result );
108. case self::TYPE_BOOL:
109. return is_bool ( $result );
110. case self::TYPE_STRING:
111. default:
112. return is_string ( $result );
113. }
114. }
116. /**
117. * Returns a fixed version of the result, if possible.
118. * Returns the value casted into the correct type or the default value, if
119. * it exists and the result is empty.
120. *
121. * @param mixed $result The result received.
122. * @return mixed The manipulated result.
123. */
124. public function fixup ( $result )
125. {
126. if ( $result === "" && $this->default !== null )
127. {
128. return $this->default;
129. }
130. switch ( $this->type )
131. {
132. case self::TYPE_INT:
133. return ( preg_match ( "/^[0-9\-]+$/", $result ) !== 0 ) ? (int) $result : $result;
134. case self::TYPE_FLOAT:
135. return ( preg_match ( "/^[0-9.E\-]+$/i", $result ) !== 0 ) ? (float) $result : $result;
136. case self::TYPE_BOOL:
137. switch ( $result )
138. {
139. case "1":
140. case "true":
141. return true;
142. case "0":
143. case "false":
144. return false;
145. }
146. case self::TYPE_STRING:
147. default:
148. return $result;
149. }
150. }
152. /**
153. * Returns a string representing valid results.
154. * Returns the string that can will be displayed with the question to
155. * indicate valid results to the user and a possibly set default, if
156. * available.
157. *
158. * @return string
159. */
160. public function getResultString ()
161. {
162. $res = "(<%s>)" . ( $this->default !== null ? " [{$this->default}]

" : "" );
- switch ( $this->type )
- {
- case self::TYPE_INT:
- return sprintf( $res, "int" );
- case self::TYPE_FLOAT:
- return sprintf( $res, "float" );
- case self::TYPE_BOOL:
- return sprintf( $res, "bool" );
- case self::TYPE_STRING:
- default:
- return sprintf( $res, "string" );
- }
- }
- /**
- * Property read access.
- *
- * @param string $propertyName Name of the property.
- * @return mixed Value of the property or null.
- *
- * @throws ezcBasePropertyNotFoundException
- * If the the desired property is not found.
- * @ignore
- */
- public function __get( $propertyName )
- {
- if ( isset( $this->$propertyName ) )
- {
- return $this-> properties [$propertyName];
- }
- throw new ezcBasePropertyNotFoundException( $propertyName );
- }
- /**
- * Property write access.
- *
- * @param string $propertyName Name of the property.
- * @param mixed $propertyValue The value for the property.
- *
- * @throws ezcBasePropertyNotFoundException
- * If a the value for the property options is not an instance of
- * @throws ezcBaseValueException
- * If a the value for a property is out of range.
- * @ignore
- */
- public function __set( $propertyName, $propertyValue )
- {
- switch ( $propertyName )
- {
- case "type":
- if ( $propertyValue !== self::TYPE_STRING && $propertyValue !== self::TYPE_INT && $propertyValue !== self::TYPE_FLOAT && $propertyValue !== self::TYPE_BOOL )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleQuestionDialogTypeValidator::TYPE_*" );
- }
- break;
- case "default":
- if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
- }
- break;
- default:
- throw new ezcBasePropertyNotFoundException( $propertyName );
- }
- $this-> properties [$propertyName] = $propertyValue;
- }
- /**
- * Property isset access.
- *
- * @param string $propertyName Name of the property.
- * @return bool True is the property is set, otherwise false.
- * @ignore
- */
- public function __isset( $propertyName )
- {
- return array_key_exists( $propertyName, $this-> properties );
- }
- }
Documentation generated by phpDocumentor 1.4.3