eZ Publish  [4.0]
ezfloatvalidator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZFloatValidator class
00004 //
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 /*! \file ezfloatvalidator.php
00031 */
00032 
00033 /*!
00034   \class eZFloatValidator ezintegervalidator.php
00035   \brief The class eZFloatValidator does
00036 
00037 */
00038 
00039 //include_once( "lib/ezutils/classes/ezregexpvalidator.php" );
00040 
00041 class eZFloatValidator extends eZRegExpValidator
00042 {
00043     /*!
00044      Constructor
00045     */
00046     function eZFloatValidator( $min = false, $max = false )
00047     {
00048         $rule = array( "accepted" => "/^-?[0-9]+([.][0-9]+)?$/",
00049                        "intermediate" => "/(-?[0-9]+([.][0-9]+)?)/",
00050                        "fixup" => "" );
00051         $this->eZRegExpValidator( $rule );
00052         $this->MinValue = $min;
00053         $this->MaxValue = $max;
00054         if ( $max !== false and $min !== false )
00055             $this->MaxValue = max( $min, $max );
00056     }
00057 
00058     function setRange( $min, $max )
00059     {
00060         $this->MinValue = $min;
00061         $this->MaxValue = $max;
00062         if ( $max !== false and $min !== false )
00063             $this->MaxValue = max( $min, $max );
00064     }
00065 
00066     function validate( $text )
00067     {
00068         $state = eZRegExpValidator::validate( $text );
00069         if ( $state == eZInputValidator::STATE_ACCEPTED )
00070         {
00071             if ( ( $this->MinValue !== false and $text < $this->MinValue ) or
00072                  ( $this->MaxValue !== false and $text > $this->MaxValue ) )
00073                 $state = eZInputValidator::STATE_INTERMEDIATE;
00074         }
00075         return $state;
00076     }
00077 
00078     function fixup( $text )
00079     {
00080         if ( preg_match( $this->RegExpRule["intermediate"], $text, $regs ) )
00081             $text = $regs[1];
00082         if ( $this->MinValue !== false and $text < $this->MinValue )
00083             $text = $this->MinValue;
00084         else if ( $this->MaxValue !== false and $text > $this->MaxValue )
00085             $text = $this->MaxValue;
00086         return $text;
00087     }
00088 
00089     /// \privatesection
00090     public $MinValue;
00091     public $MaxValue;
00092 }
00093 
00094 ?>