Apache Zeta Components Manual :: File Source for question_dialog_collection.php

Source for file question_dialog_collection.php

Documentation is available at question_dialog_collection.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleQuestionDialogCollectionValidator 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.  * Validator class to validate against a set of valid results.
  30.  * This validator class, for {@link ezcConsoleQuestionDialog} objects,
  31.  * validates a given result against a set of predefined values.
  32.  * 
  33.  * @package ConsoleTools
  34.  * @version //autogen//
  35.  *
  36.  * @property array $collection 
  37.  *            The collection of valid answers.
  38.  * @property mixed $default 
  39.  *            Default value.
  40.  * @property int $conversion 
  41.  *            ezcConsoleDialogValidator::CONVERT_NONE (default) or
  42.  *            ezcConsoleDialogValidator::CONVERT_LOWER or
  43.  *            ezcConsoleDialogValidator::CONVERT_UPPER.
  44.  */
  45. class ezcConsoleQuestionDialogCollectionValidator implements ezcConsoleQuestionDialogValidator
  46. {
  47.  
  48.     /**
  49.      * Properties.
  50.      * 
  51.      * @var array 
  52.      */
  53.     protected $properties  = array(
  54.         "collection"    => array(),
  55.         "default"       => null,
  56.         "conversion"    => self::CONVERT_NONE,
  57.     );
  58.  
  59.     /**
  60.      * Creates a new question dialog collection validator.
  61.      * Creates a new question dialog collection validator, which validates the
  62.      * result specified by the user against an array of valid results
  63.      * ($collection). If not value is provided by the user a possibly set
  64.      * $default value is used instead. The $conversion parameter can optionally
  65.      * define a conversion to be performed on the result before validating it.
  66.      * Valid conversions are defined by the CONVERT_* constants in this class.
  67.      * 
  68.      * @param array $collection The collection of valid results.
  69.      * @param mixed $default    Optional default value.
  70.      * @param int $conversion   CONVERT_* constant.
  71.      * @return void 
  72.      */
  73.     public function __constructarray $collection$default null$conversion self::CONVERT_NONE )
  74.     {
  75.         $this->collection $collection;
  76.         $this->default $default;
  77.         $this->conversion $conversion;
  78.     }
  79.  
  80.     /**
  81. /**
  82.      * Returns if the given result is valid.
  83.      * Returns if the result is in the $collection array.
  84.      * 
  85.      * @param mixed $result The received result.
  86.      * @return bool If the result is valid.
  87.      */
  88.     public function validate$result )
  89.     {
  90.         return in_array$result$this->collection );
  91.     }
  92.  
  93.     /**
  94.      * Returns a fixed version of the result, if possible.
  95.      * Converts the given result according to the conversion defined in the
  96.      * $conversion property.
  97.      * 
  98.      * @param mixed $result The received result.
  99.      * @return mixed The manipulated result.
  100.      */
  101.     public function fixup$result )
  102.     {
  103.         if $result === "" && $this->default !== null )
  104.         {
  105.             return $this->default;
  106.         }
  107.         switch $this->conversion )
  108.         {
  109.             case self::CONVERT_UPPER:
  110.                 return strtoupper$result );
  111.             case self::CONVERT_LOWER:
  112.                 return strtolower$result );
  113.             default:
  114.                 return $result;
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Returns a string representing valid results.
  120.      * Returns the string that will be displayed with the question to
  121.      * indicate valid results to the user and a possibly set default, if
  122.      * available.
  123.      * 
  124.      * @return string 
  125.      */
  126.     public function getResultString()
  127.     {
  128.         return "(" implode"/"$this->collection ")" $this->default !== null " [{$this->default}]" : "" );
  129.     }
  130.     
  131.     /**
  132.      * Property read access.
  133.      * 
  134.      * @param string $propertyName Name of the property.
  135.      * @return mixed Value of the property or null.
  136.      *
  137.      * @throws ezcBasePropertyNotFoundException
  138.      *         If the the desired property is not found.
  139.      * @ignore
  140.      */
  141.     public function __get( $propertyName )
  142.     {
  143.         if ( isset( $this->$propertyName ) )
  144.         {
  145.             return $this->properties[$propertyName];
  146.         }
  147.         throw new ezcBasePropertyNotFoundException( $propertyName );
  148.     }
  149.  
  150.     /**
  151.      * Property write access.
  152.      * 
  153.      * @param string $propertyName Name of the property.
  154.      * @param mixed $propertyValue The value for the property.
  155.      *
  156.      * @throws ezcBasePropertyNotFoundException
  157.      *         If a the value for the property options is not an instance of
  158.      * @throws ezcBaseValueException
  159.      *         If a the value for a property is out of range.
  160.      * @ignore
  161.      */
  162.     public function __set( $propertyName, $propertyValue )
  163.     {
  164.         switch ( $propertyName )
  165.         {
  166.             case "collection":
  167.                 if ( is_array( $propertyValue ) === false )
  168.                 {
  169.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "array" );
  170.                 }
  171.                 break;
  172.             case "default":
  173.                 if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
  174.                 {
  175.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
  176.                 }
  177.                 break;
  178.             case "conversion":
  179.                 if ( $propertyValue !== self::CONVERT_NONE && $propertyValue !== self::CONVERT_UPPER && $propertyValue !== self::CONVERT_LOWER )
  180.                 {
  181.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleQuestionDialogCollectionValidator::CONVERT_*" );
  182.                 }
  183.                 break;
  184.             default:
  185.                 throw new ezcBasePropertyNotFoundException( $propertyName );
  186.         }
  187.         $this->properties[$propertyName$propertyValue;
  188.     }
  189.  
  190.     /**
  191.      * Property isset access.
  192.      * 
  193.      * @param string $propertyName Name of the property.
  194.      * @return bool True is the property is set, otherwise false.
  195.      * @ignore
  196.      */
  197.     public function __isset( $propertyName )
  198.     {
  199.         return array_key_exists( $propertyName, $this->properties );
  200.     }
  201. }
  202.  
Documentation generated by phpDocumentor 1.4.3