eZ Publish  [4.0]
ezinputvalidator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZInputValidator class
00004 //
00005 // Created on: <08-Jul-2002 16:01:35 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezinputvalidator.php
00032 */
00033 
00034 /*!
00035   \class eZInputValidator ezinputvalidator.php
00036   \brief Input text validation and correction
00037 
00038   This is the base class for doing validation of input text and eventually correction.
00039   The general eZRegExpValidator can be used for most validations by supplying it with
00040   a regexp rule set, for more advanced validation you can use the eZIntegerValidator
00041   which can validate integers withing ranges.
00042 
00043   For creating your own validators you can either inherit this class or any of the
00044   advanced classes. The inherited class must implement the validate() function for
00045   validation and fixup() for fixing text to be acceptable.
00046 
00047   A validation will return a state which can either be Accepted, Intermediate or Invalid.
00048   Accepted means that the text can be used without modification, Invalid means that the
00049   text cannot be used at any cost while Intermediate means that the text can be used
00050   if it's fixed with the fixup() function.
00051 
00052   Example of a simple integer validator
00053 \code
00054 class IntegerValidator
00055 {
00056     function IntegerValidator()
00057     {
00058     }
00059 
00060     function validate( $text )
00061     {
00062         return is_numeric( $text ) ? eZInputValidator::STATE_ACCEPTED : eZInputValidator::STATE_INVALID;
00063     }
00064 
00065     function fixup( $text )
00066     {
00067     }
00068 }
00069 \endcode
00070 
00071   Example of a boolean validator
00072 \code
00073 class BooleanValidator
00074 {
00075     function BooleanValidator()
00076     {
00077     }
00078 
00079     function validate( $text )
00080     {
00081         if ( strtolower( $text ) == "true" or
00082              strtolower( $text ) == "false" )
00083             return eZInputValidator::STATE_ACCEPTED;
00084         if ( is_numeric( $text ) )
00085             return eZInputValidator::STATE_INTERMEDIATE;
00086         return eZInputValidator::STATE_INVALID;
00087     }
00088 
00089     function fixup( $text )
00090     {
00091         $text = ( $text == 0 ? "false" : "true" );
00092     }
00093 }
00094 \endcode
00095 
00096 
00097 
00098 */
00099 
00100 class eZInputValidator
00101 {
00102     const STATE_ACCEPTED = 1;
00103     const STATE_INTERMEDIATE = 2;
00104     const STATE_INVALID = 3;
00105 
00106     /*!
00107      Default constructor, does nothing.
00108     */
00109     function eZInputValidator()
00110     {
00111     }
00112 
00113     /*!
00114      Tries to validate to the text \a $text and returns one of the validator states
00115      eZInputValidator::STATE_ACCEPTED, eZInputValidator::STATE_INTERMEDIATE or
00116      eZInputValidator::STATE_INVALID.
00117      This returns eZInputValidator::STATE_ACCEPTED as default and must be reimplemented
00118      in real valiators.
00119     */
00120     function validate( $text )
00121     {
00122         return eZInputValidator::STATE_ACCEPTED;
00123     }
00124 
00125     /*!
00126      Tries to fix the text \a $text which was previously marked as eZInputValidator::STATE_INTERMEDIATE
00127      so that it can be seen as eZInputValidator::STATE_ACCEPTED.
00128     */
00129     function fixup( $text )
00130     {
00131     }
00132 }
00133 
00134 ?>