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.  */
  27.  
  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. {
  41.  
  42.     /**
  43.      * Dialog result
  44.      * 
  45.      * @var mixed 
  46.      */
  47.     protected $result;
  48.  
  49.     /**
  50.      * Properties
  51.      * 
  52.      * @var array 
  53.      */
  54.     protected $properties = array(
  55.         "options"   => null,
  56.         "output"    => null,
  57.     );
  58.  
  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 __constructezcConsoleOutput $outputezcConsoleMenuDialogOptions $options null )
  70.     {
  71.         $this->output  $output;
  72.         $this->options $options === null new ezcConsoleMenuDialogOptions($options;
  73.     }
  74.  
  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.     }
  86.  
  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.     }
  108.  
  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();
  123.  
  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()} ";
  134.  
  135.         $this->output->outputText$text$this->options->format );
  136.  
  137.         $result $this->options->validator->fixup(
  138.             ezcConsoleDialogViewer::readLine()
  139.         );
  140.         if $this->options->validator->validate$result ) )
  141.         {
  142.             $this->result = $result;
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Reset the dialog.
  148.      * Resets a possibly received result and all changes made to the dialog
  149.      * during {@link display()}. After that, the dialog can be re-used. All
  150.      * option values are kept.
  151.      * 
  152.      * @return void 
  153.      */
  154.     public function reset()
  155.     {
  156.         $this->result = null;
  157.     }
  158.  
  159.     /**
  160.      * Property read access.
  161.      *
  162.      * @throws ezcBasePropertyNotFoundException
  163.      *          If the the desired property is not found.
  164.      * 
  165.      * @param string $propertyName Name of the property.
  166.      * @return mixed Value of the property or null.
  167.      * @ignore
  168.      */
  169.     public function __get$propertyName )
  170.     {
  171.         if array_key_exists$propertyName$this->properties ) )
  172.         {
  173.             return $this->properties[$propertyName];
  174.         }
  175.         throw new ezcBasePropertyNotFoundException$propertyName );
  176.     }
  177.  
  178.     /**
  179.      * Property write access.
  180.      * 
  181.      * @param string $propertyName Name of the property.
  182.      * @param mixed $propertyValue The value for the property.
  183.      *
  184.      * @throws ezcBasePropertyPermissionException
  185.      *          If the property you try to access is read-only.
  186.      * @throws ezcBasePropertyNotFoundException
  187.      *          If the the desired property is not found.
  188.      * @ignore
  189.      */
  190.     public function __set$propertyName$propertyValue )
  191.     {
  192.         switch $propertyName )
  193.         {
  194.             case "options":
  195.                 if ( ( $propertyValue instanceof ezcConsoleMenuDialogOptions === false )
  196.                 {
  197.                     throw new ezcBaseValueException(
  198.                         $propertyName,
  199.                         is_object$propertyValue get_class$propertyValue gettype$propertyValue ) ),
  200.                         "instance of ezcConsoleMenuDialogOptions"
  201.                     );
  202.                 }
  203.                 break;
  204.             case "output":
  205.                 if ( ( $propertyValue instanceof ezcConsoleOutput === false )
  206.                 {
  207.                     throw new ezcBaseValueException(
  208.                         $propertyName,
  209.                         is_object$propertyValue get_class$propertyValue gettype$propertyValue ) ),
  210.                         "instance of ezcConsoleOutput"
  211.                     );
  212.                 }
  213.                 break;
  214.             default:
  215.                 throw new ezcBasePropertyNotFoundException$propertyName );
  216.         }
  217.         $this->properties[$propertyName$propertyValue;
  218.     }
  219.  
  220.     /**
  221.      * Property isset access.
  222.      * 
  223.      * @param string $propertyName Name of the property to check.
  224.      * @return bool If the property exists or not.
  225.      * @ignore
  226.      */
  227.     public function __isset$propertyName )
  228.     {
  229.         return array_key_exists$propertyName$this->properties );
  230.     }
  231. }
  232.  
  233. ?>
Documentation generated by phpDocumentor 1.4.3