propel-validator
[ class tree: propel-validator ] [ index: propel-validator ] [ all elements ]

Source for file MatchValidator.php

Documentation is available at MatchValidator.php

  1. <?php
  2. /*
  3.  *  $Id: MatchValidator.php 521 2007-01-05 13:29:36Z heltem $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://propel.phpdb.org>.
  20.  */
  21.  
  22. /**
  23.  * A validator for regular expressions.
  24.  *
  25.  * This validator will return true, when the passed value *matches* the
  26.  * regular expression.
  27.  *
  28.  * ## This class replaces the former class MaskValidator ##
  29.  *
  30.  * If you do want to test if the value does *not* match an expression,
  31.  * you can use the MatchValidator class instead.
  32.  *
  33.  * Below is an example usage for your Propel xml schema file.
  34.  *
  35.  * <code>
  36.  *   <column name="email" type="VARCHAR" size="128" required="true" />
  37.  *   <validator column="username">
  38.  *     <!-- allow strings that match the email adress pattern -->
  39.  *     <rule
  40.  *       name="match"
  41.  *       value="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/"
  42.  *       message="Please enter a valid email address." />
  43.  *   </validator>
  44.  * </code>
  45.  *
  46.  * @author     Michael Aichler <aichler@mediacluster.de>
  47.  * @author     Hans Lellelid <hans@xmpl.org>
  48.  * @version    $Revision: 521 $
  49.  * @package    propel.validator
  50.  */
  51. class MatchValidator implements BasicValidator
  52. {
  53.     /**
  54.      * Prepares the regular expression entered in the XML
  55.      * for use with preg_match().
  56.      * @param      string $exp 
  57.      * @return     string Prepared regular expession.
  58.      */
  59.     private function prepareRegexp($exp)
  60.     {
  61.         // remove surrounding '/' marks so that they don't get escaped in next step
  62.         if ($exp{0!== '/' || $exp{strlen($exp)-1!== '/' {
  63.             $exp '/' $exp '/';
  64.         }
  65.  
  66.         // if they did not escape / chars; we do that for them
  67.         $exp preg_replace('/([^\\\])\/([^$])/''$1\/$2'$exp);
  68.  
  69.         return $exp;
  70.     }
  71.  
  72.     /**
  73.      * Whether the passed string matches regular expression.
  74.      */
  75.     public function isValid (ValidatorMap $map$str)
  76.     {
  77.         return (preg_match($this->prepareRegexp($map->getValue())$str!= 0);
  78.     }
  79. }

Documentation generated on Thu, 22 Nov 2007 03:33:44 +0000 by phpDocumentor 1.4.0