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

## Source for file menu_dialog.php

Documentation is available at menu_dialog.php

1. <?php
2. /**
3. * File containing the ezcConsoleMenuDialog 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. * Dialog class to make the user choose from a menu.
30. * This dialog implementation displays a menu structure to the user and
31. * receives a valid menu selection.
32. *
33. * The behaviour of this dialog is defined by an instance of
34. * {@link ezcConsoleMenuDialogOptions}.
35. *
36. * @package ConsoleTools
37. * @version //autogen//
38. */
39. class ezcConsoleMenuDialog implements ezcConsoleDialog
40. {
42. /**
43. * Dialog result
44. *
45. * @var mixed
46. */
47. protected $result ;
49. /**
50. * Properties
51. *
52. * @var array
53. */
54. protected $properties = array(
55. "options" => null,
56. "output" => null,
57. );
59. /**
60. * Creates a new menu dialog.
61. * Creates a new question dialog to be displayed to the user. All behaviour is
62. * defined through the $options parameter. The $output parameter is used to
63. * display the dialog in the {@link display()} method.
64. *
65. * @param ezcConsoleOutput $output Output object.
66. * @param ezcConsoleMenuDialogOptions $options The options.
67. * @return void
68. */
69. public function __construct ( ezcConsoleOutput $output, ezcConsoleMenuDialogOptions $options = null )
70. {
71. $this->output = $output;
72. $this->options = $options === null ? new ezcConsoleMenuDialogOptions () : $options;
73. }
75. /**
76. * Returns if the dialog retrieved a valid result.
77. * If a valid result has already been received, this method returns true,
78. * otherwise false.
79. *
80. * @return bool If a valid result was retrieved.
81. */
82. public function hasValidResult ()
83. {
84. return ( $this-> result !== null );
85. }
87. /**
88. * Returns the result retrieved.
89. * If no valid result was retreived, yet, this method throws an
90. * ezcConsoleNoValidDialogResultException. Use {@link hasValidResult()} to
91. * avoid this.
92. *
93. * @return mixed The retreived result.
94. *
95. * @throws ezcDialogNoValidResultException
96. * if this method is called without a valid result being retrieved
97. * by the object. Use {@link hasValidResult()} to avoid this
98. * exception.
99. */
100. public function getResult ()
101. {
102. if ( $this-> result === null )
103. {
104. throw new ezcConsoleNoValidDialogResultException ();
105. }
106. return $this-> result ;
107. }
109. /**
110. * Displays the dialog and retreives a value from the user.
111. * Displays the dialog and retreives the desired answer from the user. If
112. * the a valid result is retrieved, it can be obtained using {@link }
113. * getResult()}. The method {@link hasValidResult()} can be used to check
114. * if a valid result is available.
115. *
116. * @return void
117. * @throws ezcConsoleDialogAbortException
118. * if the user closes STDIN using <CTRL>-D.
119. */
120. public function display ()
121. {
122. $this-> reset ();
124. $text = "{$this->options->text}\n";
125. foreach ( $this->options->validator->getElements() as $key => $entry )
126. {
127. $text .= sprintf (
128. $this->options->formatString,
129. $key,
130. $entry
131. );
132. }
133. $text .= "\n{$this->options->selectText}{$this

->options->validator->getResultString()} ";
- $this->output->outputText( $text, $this->options->format );
- $result = $this->options->validator->fixup(
- ezcConsoleDialogViewer :: readLine ()
- );
- if ( $this->options->validator->validate( $result ) )
- {
- $this-> result = $result;
- }
- }
- /**
- * Reset the dialog.
- * Resets a possibly received result and all changes made to the dialog
- * during {@link display()}. After that, the dialog can be re-used. All
- * option values are kept.
- *
- * @return void
- */
- public function reset ()
- {
- $this-> result = null;
- }
- /**
- * Property read access.
- *
- * @throws ezcBasePropertyNotFoundException
- * If the the desired property is not found.
- *
- * @param string $propertyName Name of the property.
- * @return mixed Value of the property or null.
- * @ignore
- */
- public function __get( $propertyName )
- {
- if ( array_key_exists ( $propertyName, $this-> properties ) )
- {
- 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 ezcBasePropertyPermissionException
- * If the property you try to access is read-only.
- * @throws ezcBasePropertyNotFoundException
- * If the the desired property is not found.
- * @ignore
- */
- public function __set( $propertyName, $propertyValue )
- {
- switch ( $propertyName )
- {
- case "options":
- if ( ( $propertyValue instanceof ezcConsoleMenuDialogOptions ) === false )
- {
- throw new ezcBaseValueException (
- $propertyName,
- ( is_object ( $propertyValue ) ? get_class ( $propertyValue ) : gettype ( $propertyValue ) ),
- "instance of ezcConsoleMenuDialogOptions"
- );
- }
- break;
- case "output":
- if ( ( $propertyValue instanceof ezcConsoleOutput ) === false )
- {
- throw new ezcBaseValueException (
- $propertyName,
- ( is_object ( $propertyValue ) ? get_class ( $propertyValue ) : gettype ( $propertyValue ) ),
- "instance of ezcConsoleOutput"
- );
- }
- break;
- default:
- throw new ezcBasePropertyNotFoundException ( $propertyName );
- }
- $this-> properties [$propertyName] = $propertyValue;
- }
- /**
- * Property isset access.
- *
- * @param string $propertyName Name of the property to check.
- * @return bool If the property exists or not.
- * @ignore
- */
- public function __isset( $propertyName )
- {
- return array_key_exists ( $propertyName, $this-> properties );
- }
- }
- ?>
Documentation generated by phpDocumentor 1.4.3