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

## Source for file question_dialog_collection.php

Documentation is available at question_dialog_collection.php

1. <?php
2. /**
3. * File containing the ezcConsoleQuestionDialogCollectionValidator 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 against a set of valid results.
30. * This validator class, for {@link ezcConsoleQuestionDialog} objects,
31. * validates a given result against a set of predefined values.
32. *
33. * @package ConsoleTools
34. * @version //autogen//
35. *
36. * @property array $collection
37. * The collection of valid answers.
38. * @property mixed $default
39. * Default value.
40. * @property int $conversion
41. * ezcConsoleDialogValidator::CONVERT_NONE (default) or
42. * ezcConsoleDialogValidator::CONVERT_LOWER or
43. * ezcConsoleDialogValidator::CONVERT_UPPER.
44. */
45. class ezcConsoleQuestionDialogCollectionValidator implements ezcConsoleQuestionDialogValidator
46. {
48. /**
49. * Properties.
50. *
51. * @var array
52. */
53. protected $properties = array(
54. "collection" => array(),
55. "default" => null,
56. "conversion" => self::CONVERT_NONE,
57. );
59. /**
60. * Creates a new question dialog collection validator.
61. * Creates a new question dialog collection validator, which validates the
62. * result specified by the user against an array of valid results
63. * ($collection). If not value is provided by the user a possibly set
64. * $default value is used instead. The $conversion parameter can optionally
65. * define a conversion to be performed on the result before validating it.
66. * Valid conversions are defined by the CONVERT_* constants in this class.
67. *
68. * @param array $collection The collection of valid results.
69. * @param mixed $default Optional default value.
70. * @param int $conversion CONVERT_* constant.
71. * @return void
72. */
73. public function __construct ( array $collection, $default = null, $conversion = self::CONVERT_NONE )
74. {
75. $this->collection = $collection;
76. $this->default = $default;
77. $this->conversion = $conversion;
78. }
80. /**
81. /**
82. * Returns if the given result is valid.
83. * Returns if the result is in the $collection array.
84. *
85. * @param mixed $result The received result.
86. * @return bool If the result is valid.
87. */
88. public function validate( $result )
89. {
90. return in_array ( $result, $this->collection );
91. }
93. /**
94. * Returns a fixed version of the result, if possible.
95. * Converts the given result according to the conversion defined in the
96. * $conversion property.
97. *
98. * @param mixed $result The received result.
99. * @return mixed The manipulated result.
100. */
101. public function fixup ( $result )
102. {
103. if ( $result === "" && $this->default !== null )
104. {
105. return $this->default;
106. }
107. switch ( $this->conversion )
108. {
109. case self::CONVERT_UPPER:
110. return strtoupper ( $result );
111. case self::CONVERT_LOWER:
112. return strtolower ( $result );
113. default:
114. return $result;
115. }
116. }
118. /**
119. * Returns a string representing valid results.
120. * Returns the string that will be displayed with the question to
121. * indicate valid results to the user and a possibly set default, if
122. * available.
123. *
124. * @return string
125. */
126. public function getResultString ()
127. {
128. return "(" . implode ( "/", $this->collection ) . ")" . ( $this->default !== null ? " [{$this->default}]

" : "" );
- }
- /**
- * 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 "collection":
- if ( is_array( $propertyValue ) === false )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "array" );
- }
- break;
- case "default":
- if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
- }
- break;
- case "conversion":
- if ( $propertyValue !== self::CONVERT_NONE && $propertyValue !== self::CONVERT_UPPER && $propertyValue !== self::CONVERT_LOWER )
- {
- throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleQuestionDialogCollectionValidator::CONVERT_*" );
- }
- 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