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

Source for file question_dialog_type.php

Documentation is available at question_dialog_type.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleQuestionDialogTypeValidator 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 a certain data type.
  30.  * Validator class for ezcConsoleQuestionDialog objects that validates a
  31.  * certain datatype.
  32.  * 
  33.  * @package ConsoleTools
  34.  * @version //autogen//
  35.  *
  36.  * @property int $type 
  37.  *            One of ezcConsoleQuestionDialogTypeValidator::TYPE_*. The type to
  38.  *            check against and convert results to.
  39.  * @property mixed $default 
  40.  *            A default value if no result given.
  41.  */
  42. class ezcConsoleQuestionDialogTypeValidator implements ezcConsoleQuestionDialogValidator
  43. {
  44.     /**
  45.      * Data type string.
  46.      */
  47.     const TYPE_STRING 0;
  48.     /**
  49.      * Data type int.
  50.      */
  51.     const TYPE_INT    1;
  52.     /**
  53.      * Data type float.
  54.      */
  55.     const TYPE_FLOAT  2;
  56.     /**
  57.      * Data type bool.
  58.      * The results 1 and "true" will be cased to true, 0 and "false" to false.
  59.      */
  60.     const TYPE_BOOL   3;
  61.  
  62.     /**
  63.      * Properties.
  64.      * 
  65.      * @var array 
  66.      */
  67.     protected $properties = array(
  68.         "type"      => self::TYPE_STRING,
  69.         "default"   => null,
  70.     );
  71.  
  72.     /**
  73.      * Creates a new question dialog type validator.
  74.      * Creates a new question dialog type validator, which validates the result
  75.      * specified to be of a certaon type. The $type must be one of the TYPE_*
  76.      * constants in this class. If no value is provided by the user a possibly
  77.      * set $default value is used instead.
  78.      * 
  79.      * @param int $type      One of ezcConsoleQuestionDialogTypeValidator::TYPE_*.
  80.      * @param mixed $default Default value according to $type.
  81.      * @return void 
  82.      */
  83.     public function __construct$type self::TYPE_STRING$default null )
  84.     {
  85.         $this->type $type;
  86.         $this->default $default;
  87.     }
  88.  
  89.     /**
  90.      * Returns if the given result is valid.
  91.      * Returns if the result is of the given type.
  92.      * 
  93.      * @param mixed $result The result to check.
  94.      * @return bool True if the result is valid. Otherwise false.
  95.      */
  96.     public function validate$result )
  97.     {
  98.         if $result === "" )
  99.         {
  100.             return $this->default !== null;
  101.         }
  102.         switch $this->type )
  103.         {
  104.             case self::TYPE_INT:
  105.                 return is_int$result );
  106.             case self::TYPE_FLOAT:
  107.                 return is_float$result );
  108.             case self::TYPE_BOOL:
  109.                 return is_bool$result );
  110.             case self::TYPE_STRING:
  111.             default:
  112.                 return is_string$result );
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Returns a fixed version of the result, if possible.
  118.      * Returns the value casted into the correct type or the default value, if
  119.      * it exists and the result is empty.
  120.      * 
  121.      * @param mixed $result The result received.
  122.      * @return mixed The manipulated result.
  123.      */
  124.     public function fixup$result )
  125.     {
  126.         if $result === "" && $this->default !== null )
  127.         {
  128.             return $this->default;
  129.         }
  130.         switch $this->type )
  131.         {
  132.             case self::TYPE_INT:
  133.                 return preg_match"/^[0-9\-]+$/"$result !== ? (int) $result $result;
  134.             case self::TYPE_FLOAT:
  135.                 return preg_match"/^[0-9.E\-]+$/i"$result !== ? (float) $result $result;
  136.             case self::TYPE_BOOL:
  137.                 switch $result )
  138.                 {
  139.                     case "1":
  140.                     case "true":
  141.                         return true;
  142.                     case "0":
  143.                     case "false":
  144.                         return false;
  145.                 }
  146.             case self::TYPE_STRING:
  147.             default:
  148.                 return $result;
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Returns a string representing valid results.
  154.      * Returns the string that can will be displayed with the question to
  155.      * indicate valid results to the user and a possibly set default, if
  156.      * available.
  157.      * 
  158.      * @return string 
  159.      */
  160.     public function getResultString()
  161.     {
  162.         $res "(<%s>)" $this->default !== null " [{$this->default}]" : "" );
  163.         switch ( $this->type )
  164.         {
  165.             case self::TYPE_INT:
  166.                 return sprintf( $res, "int" );
  167.             case self::TYPE_FLOAT:
  168.                 return sprintf( $res, "float" );
  169.             case self::TYPE_BOOL:
  170.                 return sprintf( $res, "bool" );
  171.             case self::TYPE_STRING:
  172.             default:
  173.                 return sprintf( $res, "string" );
  174.         }
  175.     }
  176.     
  177.     /**
  178.      * Property read access.
  179.      * 
  180.      * @param string $propertyName Name of the property.
  181.      * @return mixed Value of the property or null.
  182.      *
  183.      * @throws ezcBasePropertyNotFoundException
  184.      *         If the the desired property is not found.
  185.      * @ignore
  186.      */
  187.     public function __get( $propertyName )
  188.     {
  189.         if ( isset( $this->$propertyName ) )
  190.         {
  191.             return $this->properties[$propertyName];
  192.         }
  193.         throw new ezcBasePropertyNotFoundException( $propertyName );
  194.     }
  195.  
  196.     /**
  197.      * Property write access.
  198.      * 
  199.      * @param string $propertyName Name of the property.
  200.      * @param mixed $propertyValue The value for the property.
  201.      *
  202.      * @throws ezcBasePropertyNotFoundException
  203.      *         If a the value for the property options is not an instance of
  204.      * @throws ezcBaseValueException
  205.      *         If a the value for a property is out of range.
  206.      * @ignore
  207.      */
  208.     public function __set( $propertyName, $propertyValue )
  209.     {
  210.         switch ( $propertyName )
  211.         {
  212.             case "type":
  213.                 if ( $propertyValue !== self::TYPE_STRING && $propertyValue !== self::TYPE_INT && $propertyValue !== self::TYPE_FLOAT && $propertyValue !== self::TYPE_BOOL )
  214.                 {
  215.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "ezcConsoleQuestionDialogTypeValidator::TYPE_*" );
  216.                 }
  217.                 break;
  218.             case "default":
  219.                 if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
  220.                 {
  221.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
  222.                 }
  223.                 break;
  224.             default:
  225.                 throw new ezcBasePropertyNotFoundException( $propertyName );
  226.         }
  227.         $this->properties[$propertyName$propertyValue;
  228.     }
  229.  
  230.     /**
  231.      * Property isset access.
  232.      * 
  233.      * @param string $propertyName Name of the property.
  234.      * @return bool True is the property is set, otherwise false.
  235.      * @ignore
  236.      */
  237.     public function __isset( $propertyName )
  238.     {
  239.         return array_key_exists( $propertyName, $this->properties );
  240.     }
  241. }
  242.  
Documentation generated by phpDocumentor 1.4.3