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

## Source for file menu_dialog_default.php

Documentation is available at menu_dialog_default.php

1. <?php
2. /**
3. * File containing the ezcConsoleMenuDialogDefaultValidator 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. * Default validator for ezcConsoleMenuDialog.
30. * This dialog contains a set of menu entries, defined in the $elements
31. * property. The result, provided by the user, is checked against the keys of
32. * this array. A conversion can be specified to relax the rules for validation
33. * (like coverting the result to lower case first). For possibly conversions
34. * see the CONVERT_* constants in this class If the user does not provide an
35. * answer, a possibly set default value is used.
36. *
37. * @package ConsoleTools
38. * @version //autogen//
39. *
40. * @property array $elements The elements of the menu.
41. * @property string $default The default value.
42. * @property int $conversion
43. * ezcConsoleDialogValidator::CONVERT_NONE (default) or
44. * ezcConsoleDialogValidator::CONVERT_LOWER or
45. * ezcConsoleDialogValidator::CONVERT_UPPER.
46. */
47. class ezcConsoleMenuDialogDefaultValidator implements ezcConsoleMenuDialogValidator
48. {
49. /**
50. * Properties
51. *
52. * @var array
53. */
54. protected $properties = array(
55. "elements" => array(),
56. "default" => null,
57. "conversion" => self::CONVERT_NONE,
58. );
60. /**
61. * Creates a new menu default validator.
62. * Creates a validator specified by the given parameters. The $elements
63. * array specifies the possible menu items to select from. The item
64. * identifier (the key of the array) is used to validate the result. The
65. * assigned text is displayed as the menu item text. If no result is
66. * provided and an optionally provided default value is used. The
67. * $conversion parameter can be used to get a conversion applied to the
68. * result before validating it.
69. *
70. * @param array $elements The elements of the menu.
71. * @param mixed $default The default value.
72. * @param int $conversion The conversion to apply.
73. * @return void
74. */
75. public function __construct ( array $elements = array(), $default = null, $conversion = self::CONVERT_NONE )
76. {
77. $this->elements = $elements;
78. $this->default = $default;
79. $this->conversion = $conversion;
80. }
82. /**
83. * Returns if the given result is valid.
84. * Checks if the given result is a valid key in the $elements property.
85. *
86. * @param mixed $result The received result.
87. * @return bool If the result is valid.
88. */
89. public function validate ( $result )
90. {
91. return isset( $this->elements[$result] );
92. }
94. /**
95. * Returns a fixed version of the result, if possible.
96. * Converts the given result according to the conversion defined in the
97. * $conversion property.
98. *
99. * @param mixed $result The received result.
100. * @return mixed The manipulated result.
101. */
102. public function fixup ( $result )
103. {
104. if ( $result === "" && $this->default !== null )
105. {
106. return $this->default;
108. }
109. switch ( $this->conversion )
110. {
111. case self::CONVERT_LOWER:
112. return strtolower ( $result );
113. case self::CONVERT_UPPER:
114. return strtoupper ( $result );
115. case self::CONVERT_NONE:
116. default:
117. return $result;
118. }
119. }
121. /**
122. * Returns a string representing the default value.
123. * For example "[y]" to indicate that "y" is the preselected result and
124. * will be chosen if no result is provided.
125. *
126. * @return string The result string.
127. */
128. public function getResultString ()
129. {
130. return $this->default === null ? "" : "[{$this->default}]

";
- }
- /**
- * Returns an array of the elements to display.
- *
- * @return array(string=>string) Elements to display.
- */
- public function getElements()
- {
- return $this->elements;
- }
- /**
- * 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 "elements":
- 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, "ezcConsoleMenuDialogDefaultValidator::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