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.  */
  27.  
  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.     );
  59.  
  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 __constructarray $elements array()$default null$conversion self::CONVERT_NONE )
  76.     {
  77.         $this->elements     $elements;
  78.         $this->default      $default;
  79.         $this->conversion   $conversion;
  80.     }
  81.  
  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.     }
  93.  
  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;
  107.  
  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.     }
  120.  
  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}]";
  131.    }
  132.  
  133.     /**
  134.      * Returns an array of the elements to display. 
  135.      * 
  136.      * @return array(string=>string) Elements to display.
  137.      */
  138.     public function getElements()
  139.     {
  140.         return $this->elements;
  141.     }
  142.  
  143.     /**
  144.      * Property read access.
  145.      * 
  146.      * @param string $propertyName Name of the property.
  147.      * @return mixed Value of the property or null.
  148.      *
  149.      * @throws ezcBasePropertyNotFoundException
  150.      *         If the the desired property is not found.
  151.      * @ignore
  152.      */
  153.     public function __get( $propertyName )
  154.     {
  155.         if ( isset( $this->$propertyName ) )
  156.         {
  157.             return $this->properties[$propertyName];
  158.         }
  159.         throw new ezcBasePropertyNotFoundException( $propertyName );
  160.     }
  161.  
  162.     /**
  163.      * Property write access.
  164.      * 
  165.      * @param string $propertyName Name of the property.
  166.      * @param mixed $propertyValue The value for the property.
  167.      *
  168.      * @throws ezcBasePropertyNotFoundException
  169.      *         If a the value for the property options is not an instance of
  170.      * @throws ezcBaseValueException
  171.      *         If a the value for a property is out of range.
  172.      * @ignore
  173.      */
  174.     public function __set( $propertyName, $propertyValue )
  175.     {
  176.         switch ( $propertyName )
  177.         {
  178.             case "elements":
  179.                 if ( is_array( $propertyValue ) === false )
  180.                 {
  181.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "array" );
  182.                 }
  183.                 break;
  184.             case "default":
  185.                 if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
  186.                 {
  187.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
  188.                 }
  189.                 break;
  190.             case "conversion":
  191.                 if ( $propertyValue !== self::CONVERT_NONE && $propertyValue !== self::CONVERT_UPPER && $propertyValue !== self::CONVERT_LOWER )
  192.                 {
  193.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleMenuDialogDefaultValidator::CONVERT_*" );
  194.                 }
  195.                 break;
  196.             default:
  197.                 throw new ezcBasePropertyNotFoundException( $propertyName );
  198.         }
  199.         $this->properties[$propertyName$propertyValue;
  200.     }
  201.  
  202.     /**
  203.      * Property isset access.
  204.      * 
  205.      * @param string $propertyName Name of the property.
  206.      * @return bool True is the property is set, otherwise false.
  207.      * @ignore
  208.      */
  209.     public function __isset( $propertyName )
  210.     {
  211.         return array_key_exists( $propertyName, $this->properties );
  212.     }
  213. }
  214.  
Documentation generated by phpDocumentor 1.4.3