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

## Source for file question_dialog_mapping.php

Documentation is available at question_dialog_mapping.php

1. <?php
2. /**
3. * File containing the ezcConsoleQuestionDialogMappingValidator 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 map certain results to others.
30. * This validator class, for {@link ezcConsoleQuestionDialog} objects,
31. * validates a given result against a set of predefined values, exactly like
32. * {@link ezcConsoleQuestionDialogCollectionValidator} does, but allows in
33. * addition to map certain results to other results. The $map property contains
34. * an array of mappings that are checked before a received result is validated.
35. * If a mapping matches, the received result is converted to the mapping target
36. * before being validated.
37. *
38. * A valid $map looks like this:
39. * <code>
40. * array(
41. * 'yes' => 'y',
42. * 'no' => 'n',
43. * '1' => 'y',
44. * '0' => 'n'
45. * )
46. * </code>
47. * While the corresponding collection of valid answers would look like
48. * this:
49. * <code>
50. * array(
51. * 'y', 'n'
52. * )
53. * </code>
54. * If the answer 'yes' is received by the validator, it is mapped to 'y',
55. * therefore considered valid and 'y' is returned as the received value.
56. *
57. * @package ConsoleTools
58. * @version //autogen//
59. *
60. * @property array(string) $collection
61. * The collection of valid answers.
62. * @property mixed $default
63. * Default value.
64. * @property int $conversion
65. * ezcConsoleDialogValidator::CONVERT_NONE (default) or
66. * ezcConsoleDialogValidator::CONVERT_LOWER or
67. * ezcConsoleDialogValidator::CONVERT_UPPER.
68. * @property array(string=>string) $map
69. * Mapping of answers to valid answers (e.g. array('yes' => 'y') to
70. * map 'yes' to 'y' while 'y' must be in $collection).
71. */
72. class ezcConsoleQuestionDialogMappingValidator extends ezcConsoleQuestionDialogCollectionValidator
73. {
74. /**
75. * Creates a new question dialog mapping validator.
76. * Creates a new question dialog mapping validator, which validates the
77. * result specified by the user against an array of valid results
78. * ($collection). If not value is provided by the user a possibly set
79. * $default value is used instead. The $conversion parameter can optionally
80. * define a conversion to be performed on the result before validating it.
81. * Valid conversions are defined by the CONVERT_* constants in this class.
82. *
83. * While this functionality is already provided by {@link }
84. * ezcConsoleQuestionDialogCollectionValidator}, the additional $map
85. * paramater allows the sepcification of a map of result values. These
86. * mapping is then checked for matches, before a received answer is
87. * validated against the collection.
88. *
89. * @param array(string) $collection The collection of valid results.
90. * @param mixed $default Optional default value.
91. * @param int $conversion CONVERT_* constant.
92. * @param array(string=>string) $map
93. * @return void
94. */
95. public function __construct ( array $collection, $default = null, $conversion = self::CONVERT_NONE, array $map = array() )
96. {
97. // Initialize additional property
98. $this-> properties ['map'] = $map;
100. parent:: __construct ( $collection, $default, $conversion );
101. }
103. /**
104. * Returns a fixed version of the result, if possible.
105. * Converts the given result according to the conversion defined in the
106. * $conversion property.
107. *
108. * @param mixed $result The received result.
109. * @return mixed The manipulated result.
110. */
111. public function fixup ( $result )
112. {
113. if ( $result === "" && $this->default !== null )
114. {
115. return $this->default;
116. }
117. switch ( $this->conversion )
118. {
119. case self::CONVERT_UPPER:
120. $result = strtoupper ( $result );
121. break;
122. case self::CONVERT_LOWER:
123. $result = strtolower ( $result );
124. break;
125. }
126. return ( isset( $this->map[$result] ) ? $this->map[$result] : $result );
127. }
129. /**
130. * Property write access.
131. *
132. * @param string $propertyName Name of the property.
133. * @param mixed $propertyValue The value for the property.
134. *
135. * @throws ezcBasePropertyNotFoundException
136. * If a the value for the property options is not an instance of
137. * @throws ezcBaseValueException
138. * If a the value for a property is out of range.
139. * @ignore
140. */
141. public function __set( $propertyName, $propertyValue )
142. {
143. switch ( $propertyName )
144. {
145. case "map":
146. if ( is_array ( $propertyValue ) === false )
147. {
148. throw new ezcBaseValueException ( $propertyName, $propertyValue, "array" );
149. }
150. break;
151. default:
152. return parent::__set( $propertyName, $propertyValue );
153. }
154. $this-> properties [$propertyName] = $propertyValue;
155. }
156. }
158. ?>

Documentation generated by phpDocumentor 1.4.3