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

Source for file question_dialog_regex.php

Documentation is available at question_dialog_regex.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleQuestionDialogRegexValidator 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.  * Regex validator for ezcConsoleQuestionDialog
  30.  * Validator class for ezcConsoleQuestionDialog objects that validates by
  31.  * matching a certain regular expression.
  32.  * 
  33.  * @package ConsoleTools
  34.  * @version //autogen//
  35.  *
  36.  * @property string $pattern 
  37.  *            The pattern to use for validation. Delimiters and modifiers
  38.  *            included.
  39.  * @property mixed $default 
  40.  *            A default value if no (an empty string) result given.
  41.  */
  42. class ezcConsoleQuestionDialogRegexValidator implements ezcConsoleQuestionDialogValidator
  43. {
  44.  
  45.     /**
  46.      * Properties
  47.      * 
  48.      * @var array(string=>mixed) 
  49.      */
  50.     protected $properties = array(
  51.         "pattern" => null,
  52.         "default" => null,
  53.     );
  54.  
  55.     /**
  56.      * Create a new question dialog regex validator.
  57.      * Create a new question dialog regex validator, which validates the result
  58.      * specified against a given regular expression. The delimiters and
  59.      * eventual modifiers must be included in the pattern. If not value is
  60.      * provided by the user a possibly set $default value is used instead.
  61.      *
  62.      * 
  63.      * @param string $pattern Pattern to validate against.
  64.      * @param mixed $default  Default value.
  65.      * @return void 
  66.      */
  67.     public function __construct$pattern$default null )
  68.     {
  69.         $this->pattern $pattern;
  70.         $this->default $default;
  71.     }
  72.  
  73.     /**
  74.      * Returns if the given result is valid.
  75.      * Returns if the result matches the regular expression.
  76.      * 
  77.      * @param mixed $result The received result.
  78.      * @return bool If the result is valid.
  79.      */
  80.     public function validate$result )
  81.     {
  82.         if $result === "" )
  83.         {
  84.             return $this->default !== null;
  85.         }
  86.         return preg_match$this->pattern$result 0;
  87.     }
  88.  
  89.     /**
  90.      * Returns a fixed version of the result, if possible.
  91.      * If no result was provided by the user, the default value will be
  92.      * returned, if set.
  93.      * 
  94.      * @param mixed $result The received result.
  95.      * @return mixed The manipulated result.
  96.      */
  97.     public function fixup$result )
  98.     {
  99.         if $result === "" && $this->default !== null )
  100.         {
  101.             return $this->default;
  102.         }
  103.         return $result;
  104.     }
  105.  
  106.     /**
  107.      * Returns a string representing valid results.
  108.      * Returns the string that will be displayed with the question to
  109.      * indicate valid results to the user and a possibly set default, if
  110.      * available.
  111.      * 
  112.      * @return string 
  113.      */
  114.     public function getResultString()
  115.     {
  116.         return "(match {$this->pattern})" . ( $this->default !== null " [{$this->default}]" : "" );
  117.     }
  118.     
  119.     /**
  120.      * Property read access.
  121.      * 
  122.      * @param string $propertyName Name of the property.
  123.      * @return mixed Value of the property or null.
  124.      *
  125.      * @throws ezcBasePropertyNotFoundException
  126.      *         If the the desired property is not found.
  127.      * @ignore
  128.      */
  129.     public function __get( $propertyName )
  130.     {
  131.         if ( $this->__isset$propertyName ) )
  132.         {
  133.             return $this->properties[$propertyName];
  134.         }
  135.         throw new ezcBasePropertyNotFoundException( $propertyName );
  136.     }
  137.  
  138.     /**
  139.      * Property write access.
  140.      * 
  141.      * @param string $propertyName Name of the property.
  142.      * @param mixed $propertyValue The value for the property.
  143.      *
  144.      * @throws ezcBasePropertyNotFoundException
  145.      *         If a the value for the property options is not an instance of
  146.      * @throws ezcBaseValueException
  147.      *         If a the value for a property is out of range.
  148.      * @ignore
  149.      */
  150.     public function __set( $propertyName, $propertyValue )
  151.     {
  152.         switch ( $propertyName )
  153.         {
  154.             case "pattern":
  155.                 if ( is_string( $propertyValue ) === false || strlen( $propertyValue ) < 2 )
  156.                 {
  157.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "string, length > 1" );
  158.                 }
  159.                 break;
  160.             case "default":
  161.                 if ( is_scalar( $propertyValue ) === false && $propertyValue !== null )
  162.                 {
  163.                     throw new ezcBaseValueException( $propertyName, $propertyValue, "scalar" );
  164.                 }
  165.                 break;
  166.             default:
  167.                 throw new ezcBasePropertyNotFoundException( $propertyName );
  168.         }
  169.         $this->properties[$propertyName$propertyValue;
  170.     }
  171.  
  172.     /**
  173.      * Property isset access.
  174.      * 
  175.      * @param string $propertyName Name of the property.
  176.      * @return bool True is the property is set, otherwise false.
  177.      * @ignore
  178.      */
  179.     public function __isset( $propertyName )
  180.     {
  181.         return array_key_exists( $propertyName, $this->properties );
  182.     }
  183. }
  184.  
Documentation generated by phpDocumentor 1.4.3