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

Source for file question_dialog_mapping.php

Documentation is available at question_dialog_mapping.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleQuestionDialogMappingValidator 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 map certain results to others.
  30.  * This validator class, for {@link ezcConsoleQuestionDialog} objects,
  31.  * validates a given result against a set of predefined values, exactly like
  32.  * {@link ezcConsoleQuestionDialogCollectionValidator} does, but allows in
  33.  * addition to map certain results to other results. The $map property contains
  34.  * an array of mappings that are checked before a received result is validated.
  35.  * If a mapping matches, the received result is converted to the mapping target
  36.  * before being validated.
  37.  *
  38.  * A valid $map looks like this:
  39.  * <code>
  40.  *      array(
  41.  *          'yes' => 'y',
  42.  *          'no'  => 'n',
  43.  *          '1'   => 'y',
  44.  *          '0'   => 'n'
  45.  *      )
  46.  * </code>
  47.  * While the corresponding collection of valid answers would look like
  48.  * this:
  49.  * <code>
  50.  *      array(
  51.  *          'y', 'n'
  52.  *      )
  53.  * </code>
  54.  * If the answer 'yes' is received by the validator, it is mapped to 'y',
  55.  * therefore considered valid and 'y' is returned as the received value.
  56.  * 
  57.  * @package ConsoleTools
  58.  * @version //autogen//
  59.  *
  60.  * @property array(string) $collection 
  61.  *            The collection of valid answers.
  62.  * @property mixed $default 
  63.  *            Default value.
  64.  * @property int $conversion 
  65.  *            ezcConsoleDialogValidator::CONVERT_NONE (default) or
  66.  *            ezcConsoleDialogValidator::CONVERT_LOWER or
  67.  *            ezcConsoleDialogValidator::CONVERT_UPPER.
  68.  * @property array(string=>string) $map 
  69.  *            Mapping of answers to valid answers (e.g. array('yes' => 'y') to
  70.  *            map 'yes' to 'y' while 'y' must be in $collection).
  71.  */
  72. {
  73.     /**
  74.      * Creates a new question dialog mapping validator.
  75.      * Creates a new question dialog mapping validator, which validates the
  76.      * result specified by the user against an array of valid results
  77.      * ($collection). If not value is provided by the user a possibly set
  78.      * $default value is used instead. The $conversion parameter can optionally
  79.      * define a conversion to be performed on the result before validating it.
  80.      * Valid conversions are defined by the CONVERT_* constants in this class.
  81.      *
  82.      * While this functionality is already provided by {@link }
  83.      * ezcConsoleQuestionDialogCollectionValidator}, the additional $map
  84.      * paramater allows the sepcification of a map of result values. These
  85.      * mapping is then checked for matches, before a received answer is
  86.      * validated against the collection.
  87.      *
  88.      * @param array(string) $collection The collection of valid results.
  89.      * @param mixed $default    Optional default value.
  90.      * @param int $conversion   CONVERT_* constant.
  91.      * @param array(string=>string) $map 
  92.      * @return void 
  93.      */
  94.     public function __constructarray $collection$default null$conversion self::CONVERT_NONEarray $map array() )
  95.     {
  96.         // Initialize additional property
  97.         $this->properties['map'$map;
  98.  
  99.         parent::__construct$collection$default$conversion );
  100.     }
  101.  
  102.     /**
  103.      * Returns a fixed version of the result, if possible.
  104.      * Converts the given result according to the conversion defined in the
  105.      * $conversion property.
  106.      * 
  107.      * @param mixed $result The received result.
  108.      * @return mixed The manipulated result.
  109.      */
  110.     public function fixup$result )
  111.     {
  112.         if $result === "" && $this->default !== null )
  113.         {
  114.             return $this->default;
  115.         }
  116.         switch $this->conversion )
  117.         {
  118.             case self::CONVERT_UPPER:
  119.                 $result strtoupper$result );
  120.                 break;
  121.             case self::CONVERT_LOWER:
  122.                 $result strtolower$result );
  123.                 break;
  124.         }
  125.         return isset$this->map[$result$this->map[$result$result );
  126.     }
  127.     
  128.     /**
  129.      * Property write access.
  130.      * 
  131.      * @param string $propertyName Name of the property.
  132.      * @param mixed $propertyValue The value for the property.
  133.      *
  134.      * @throws ezcBasePropertyNotFoundException
  135.      *          If a the value for the property options is not an instance of
  136.      * @throws ezcBaseValueException
  137.      *          If a the value for a property is out of range.
  138.      * @ignore
  139.      */
  140.     public function __set$propertyName$propertyValue )
  141.     {
  142.         switch $propertyName )
  143.         {
  144.             case "map":
  145.                 if is_array$propertyValue === false )
  146.                 {
  147.                     throw new ezcBaseValueException$propertyName$propertyValue"array" );
  148.                 }
  149.                 break;
  150.             default:
  151.                 return parent::__set$propertyName$propertyValue );
  152.         }
  153.         $this->properties[$propertyName$propertyValue;
  154.     }
  155. }
  156.  
  157. ?>
Documentation generated by phpDocumentor 1.4.3