Author: Tobias Schlitt
Date: 2007-04-07 10:37:12 +0200 (Sat, 07 Apr 2007)
New Revision: 4817

Log:
- Added menu dialog implementation.

Added:
   trunk/ConsoleTools/src/dialog/validators/menu_dialog_default.php
   trunk/ConsoleTools/src/interfaces/menu_dialog_validator.php
Modified:
   trunk/ConsoleTools/src/console_autoload.php
   trunk/ConsoleTools/src/dialog/menu_dialog.php
   trunk/ConsoleTools/src/options/menu_dialog.php

Modified: trunk/ConsoleTools/src/console_autoload.php
===================================================================
--- trunk/ConsoleTools/src/console_autoload.php 2007-04-07 08:36:48 UTC (rev 
4816)
+++ trunk/ConsoleTools/src/console_autoload.php 2007-04-07 08:37:12 UTC (rev 
4817)
@@ -49,19 +49,22 @@
     'ezcConsoleOptionTooManyValuesException'        => 
'ConsoleTools/exceptions/option_too_many_values.php',
 
     'ezcConsoleDialog'                              => 
'ConsoleTools/interfaces/dialog.php',
-    'ezcConsoleDialogValidator'                     => 
'ConsoleTools/interfaces/dialog_validator.php',
-    'ezcConsoleQuestionDialogValidator'             => 
'ConsoleTools/interfaces/question_dialog_validator.php',
     'ezcConsoleDialogViewer'                        => 
'ConsoleTools/dialog_viewer.php',
     'ezcConsoleQuestionDialog'                      => 
'ConsoleTools/dialog/question_dialog.php',
     'ezcConsoleMenuDialog'                          => 
'ConsoleTools/dialog/menu_dialog.php',
     
+    'ezcConsoleDialogValidator'                     => 
'ConsoleTools/interfaces/dialog_validator.php',
+    'ezcConsoleQuestionDialogValidator'             => 
'ConsoleTools/interfaces/question_dialog_validator.php',
+    'ezcConsoleMenuDialogValidator'                 => 
'ConsoleTools/interfaces/menu_dialog_validator.php',
+    'ezcConsoleQuestionDialogCollectionValidator'   => 
'ConsoleTools/dialog/validators/question_dialog_collection.php',
+    'ezcConsoleQuestionDialogTypeValidator'         => 
'ConsoleTools/dialog/validators/question_dialog_type.php',
+    'ezcConsoleMenuDialogDefaultValidator'          => 
'ConsoleTools/dialog/validators/menu_dialog_default.php',
+
+    
     'ezcConsoleDialogOptions'                       => 
'ConsoleTools/options/dialog.php',
     'ezcConsoleQuestionDialogOptions'               => 
'ConsoleTools/options/question_dialog.php',
     'ezcConsoleMenuDialogOptions'                   => 
'ConsoleTools/options/menu_dialog.php',
 
-    'ezcConsoleQuestionDialogCollectionValidator'   => 
'ConsoleTools/dialog/validators/question_dialog_collection.php',
-    'ezcConsoleQuestionDialogTypeValidator'         => 
'ConsoleTools/dialog/validators/question_dialog_type.php',
-
     'ezcConsoleNoValidDialogResultException'        => 
'ConsoleTools/exceptions/no_valid_dialog_result.php',
 );
 

Modified: trunk/ConsoleTools/src/dialog/menu_dialog.php
===================================================================
--- trunk/ConsoleTools/src/dialog/menu_dialog.php       2007-04-07 08:36:48 UTC 
(rev 4816)
+++ trunk/ConsoleTools/src/dialog/menu_dialog.php       2007-04-07 08:37:12 UTC 
(rev 4817)
@@ -12,8 +12,18 @@
 class ezcConsoleMenuDialog implements ezcConsoleDialog
 {
 
+    /**
+     * Dialog result 
+     * 
+     * @var mixed
+     */
     protected $result;
 
+    /**
+     * Properties 
+     * 
+     * @var array
+     */
     protected $properties = array(
         "options"   => null,
         "output"    => null,
@@ -67,27 +77,22 @@
     public function display()
     {
         $text = "{$this->options->text}\n";
-        $results = array();
-        foreach ( $this->options->entries as $key => $entry )
+        foreach ( $this->options->validator->getElements() as $key => $entry )
         {
             $text .= sprintf(
-                "%{$this->options->markerSpace}s{$this->options->marker} %s\n",
+                $this->options->formatString,
                 $key,
                 $entry
             );
-            $results[] = $key;
         }
-        $text .= "\n{$this->options->selectText}";
+        $text .= 
"\n{$this->options->selectText}{$this->options->validator->getResultString()} ";
 
-        $question = new ezcConsoleQuestionDialog( $this->output );
-        $question->options->text = $text;
-        $question->options->validResults = $results;
-        $question->options->format = $this->options->format;
-        $question->display();
+        $this->output->outputText( $text, $this->options->format );
 
-        if ( $question->hasValidResult() )
+        $result = $this->options->validator->fixup( 
ezcConsoleDialogViewer::readLine() );
+        if ( $this->options->validator->validate( $result ) )
         {
-            $this->result = $question->getResult();
+            $this->result = $result;
         }
     }
 

Added: trunk/ConsoleTools/src/dialog/validators/menu_dialog_default.php
===================================================================
--- trunk/ConsoleTools/src/dialog/validators/menu_dialog_default.php    
2007-04-07 08:36:48 UTC (rev 4816)
+++ trunk/ConsoleTools/src/dialog/validators/menu_dialog_default.php    
2007-04-07 08:37:12 UTC (rev 4817)
@@ -0,0 +1,163 @@
+<?php
+/**
+ * File containing the ezcConsoleMenuDialogDefaultValidator class.
+ *
+ * @package ConsoleTools
+ * @version //autogentag//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ */
+
+/**
+ * Default validator for ezcConsoleMenuDialog.
+ * 
+ * @package ConsoleTools
+ * @version //autogen//
+ * @copyright Copyright (C) 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ *
+ * @property array $elements The elements of the menu.
+ * @property string $default The default value.
+ */
+class ezcConsoleMenuDialogDefaultValidator implements 
ezcConsoleMenuDialogValidator
+{
+    /**
+     * Properties 
+     * 
+     * @var array
+     */
+    protected $properties = array(
+        "elements"  => array(),
+        "default"   => null,
+    );
+
+    /**
+     * Creates a new validator. 
+     * 
+     * @param array $elements The elements of the menu.
+     * @param mixed $default  The default value.
+     * @return void
+     */
+    public function __construct( array $elements = array(), $default = null )
+    {
+        $this->elements = $elements;
+        $this->default  = $default;
+    }
+
+    /**
+     * Returns if the given result is valid. 
+     * 
+     * @param mixed $result The received result.
+     * @return bool If the result is valid.
+     */
+    public function validate( $result )
+    {
+        return isset( $this->elements[$result] );
+    }
+
+    /**
+     * Returns a fixed version of the result, if possible.
+     * This method tries to repair the submitted result, if it is not valid,
+     * yet. Fixing can be done in different ways, like casting into a certain
+     * datatype, string manipulation, creating an object. A result returned
+     * by fixup must not necessarily be valid, so a dialog should call validate
+     * after trying to fix the result.
+     * 
+     * @param mixed $result The received result.
+     * @return mixed The manipulated result.
+     */
+    public function fixup( $result )
+    {
+        return ( $result === "" && $this->default !== null ) ? $this->default 
: $result;
+    }
+
+    /**
+     * Returns a string of possible results to be displayed with the question. 
+     * For example "(y/n) [y]" to indicate "y" and "n" are valid values and 
"y" is
+     * preselected.
+     *
+     * @return string The result string.
+     */
+    public function getResultString()
+    {
+        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 $key 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 $key Name of the property.
+     * @param mixed $val  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;
+            default:
+                throw new ezcBasePropertyNotFoundException( $propertyName );
+        }
+        $this->properties[$propertyName] = $propertyValue;
+    }
+
+    /**
+     * Property isset access.
+     * 
+     * @param string $key 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 );
+    }
+}
+
+?>


Property changes on: 
trunk/ConsoleTools/src/dialog/validators/menu_dialog_default.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: trunk/ConsoleTools/src/interfaces/menu_dialog_validator.php
===================================================================
--- trunk/ConsoleTools/src/interfaces/menu_dialog_validator.php 2007-04-07 
08:36:48 UTC (rev 4816)
+++ trunk/ConsoleTools/src/interfaces/menu_dialog_validator.php 2007-04-07 
08:37:12 UTC (rev 4817)
@@ -0,0 +1,23 @@
+<?php
+
+/**
+ * Interface for ezcConsoleMenuDialog validator classes. 
+ * 
+ * @package ConsoleTools
+ * @version //autogen//
+ * @copyright Copyright (C) 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+interface ezcConsoleMenuDialogValidator extends 
ezcConsoleQuestionDialogValidator
+{
+
+    /**
+     * Returns an array of the elements to display. 
+     * 
+     * @return array(string=>string) Elements to display.
+     */
+    public function getElements();
+
+}
+
+?>


Property changes on: trunk/ConsoleTools/src/interfaces/menu_dialog_validator.php
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: trunk/ConsoleTools/src/options/menu_dialog.php
===================================================================
--- trunk/ConsoleTools/src/options/menu_dialog.php      2007-04-07 08:36:48 UTC 
(rev 4816)
+++ trunk/ConsoleTools/src/options/menu_dialog.php      2007-04-07 08:37:12 UTC 
(rev 4817)
@@ -7,22 +7,42 @@
  * @version //autogen//
  * @copyright Copyright (C) 2006 eZ systems as. All rights reserved.
  * @license http://ez.no/licenses/new_bsd New BSD License
+ *
+ * @property string $text
+ *           The text to display before the menu.
+ * @property string $formatString
+ *           The format string for each menu element.
+ * @property string $selectText
+ *           The test to display after the menu to indicate the user that he
+ *           should select an item.
+ * @property ezcConsoleMenuDialogDefaultValidator $validator
+ *           The validator to use with this menu.
+ * @property string format
+ *           The output format for the dialog.
  */
 class ezcConsoleMenuDialogOptions extends ezcConsoleDialogOptions
 {
+
     /**
-     * Properties.
+     * Construct a new options object.
+     * Options are constructed from an option array by default. The constructor
+     * automatically passes the given options to the __set() method to set 
them 
+     * in the class.
      * 
-     * @var array(string=>mixed)
+     * @throws ezcBasePropertyNotFoundException
+     *         If trying to access a non existent property.
+     * @throws ezcBaseValueException
+     *         If the value for a property is out of range.
+     * @param array(string=>mixed) $options The initial options to set.
      */
-    protected $properties = array(
-        "format"        => "default",
-        "text"          => "Please choose:",
-        "entries"       => array(),
-        "marker"        => ")",
-        "markerSpace"   => 4,
-        "selectText"    => "Selection: ",
-    );
+    public function __construct( array $options = array() )
+    {
+        $this->properties["text"]           = "Please choose an item.";
+        $this->properties["formatString"]   = "%3s) %s\n";
+        $this->properties["selectText"]     = "Select: ";
+        $this->properties["validator"]      = new 
ezcConsoleMenuDialogDefaultValidator();
+        parent::__construct( $options );
+    }
 
     /**
      * Property set access.
@@ -46,46 +66,35 @@
                     );
                 }
                 break;
-            case "entries":
-                if ( is_array( $propertyValue ) === false )
+            case "selectText":
+                if ( is_string( $propertyValue ) === false )
                 {
                     throw new ezcBaseValueException(
                         $propertyName,
                         $propertyValue,
-                        "array"
+                        "string"
                     );
                 }
                 break;
-            case "marker":
-                if ( is_string( $propertyValue ) === false || strlen( 
$propertyValue ) < 1 )
+            case "formatString":
+                if ( is_string( $propertyValue ) === false )
                 {
                     throw new ezcBaseValueException(
                         $propertyName,
                         $propertyValue,
-                        "string, length > 0"
+                        "string"
                     );
                 }
                 break;
-            case "markerSpace":
-                if ( is_int( $propertyValue ) === false || $propertyValue < 1 )
+            case "validator":
+                if ( ( $propertyValue instanceof ezcConsoleMenuDialogValidator 
) === false )
                 {
                     throw new ezcBaseValueException(
                         $propertyName,
                         $propertyValue,
-                        "int > 0"
+                        "ezcConsoleMenuDialogValidator"
                     );
                 }
-                break;
-            case "selectText":
-                if ( is_string( $propertyValue ) === false )
-                {
-                    throw new ezcBaseValueException(
-                        $propertyName,
-                        $propertyValue,
-                        "string"
-                    );
-                }
-                break;
             default:
                 parent::__set( $propertyName, $propertyValue );
         }

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to