[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: MatchValidator.php 64 2005-05-13 02:43:56Z root $ 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 require_once 'propel/validator/BasicValidator.php'; 23 24 /** 25 * A validator for regular expressions. 26 * 27 * This validator will return true, when the passed value *matches* the 28 * regular expression. 29 * 30 * ## This class replaces the former class MaskValidator ## 31 * 32 * If you do want to test if the value does *not* match an expression, 33 * you can use the MatchValidator class instead. 34 * 35 * Below is an example usage for your Propel xml schema file. 36 * 37 * <code> 38 * <column name="email" type="VARCHAR" size="128" required="true" /> 39 * <validator column="username"> 40 * <!-- allow strings that match the email adress pattern --> 41 * <rule 42 * name="match" 43 * value="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/" 44 * message="Please enter a valid email address." /> 45 * </validator> 46 * </code> 47 * 48 * @author Michael Aichler <aichler@mediacluster.de> 49 * @author Hans Lellelid <hans@xmpl.org> 50 * @version $Revision: 64 $ 51 * @package propel.validator 52 */ 53 class MatchValidator implements BasicValidator 54 { 55 /** 56 * Prepares the regular expression entered in the XML 57 * for use with preg_match(). 58 * @param string $exp 59 * @return string Prepared regular expession. 60 */ 61 private function prepareRegexp($exp) 62 { 63 // remove surrounding '/' marks so that they don't get escaped in next step 64 if ($exp{0} !== '/' || $exp{strlen($exp)-1} !== '/' ) { 65 $exp = '/' . $exp . '/'; 66 } 67 68 // if they did not escape / chars; we do that for them 69 $exp = preg_replace('/([^\\\])\/([^$])/', '$1\/$2', $exp); 70 71 return $exp; 72 } 73 74 /** 75 * Whether the passed string matches regular expression. 76 */ 77 public function isValid (ValidatorMap $map, $str) 78 { 79 return (preg_match($this->prepareRegexp($map->getValue()), $str) != 0); 80 } 81 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |