Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: NotMatchValidator.php 1262 2009-10-26 20:54:39Z francois $
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 does *not* match
26
 * the regular expression.
27
 *
28
 * If you do want to test if the value *matches* an expression, you can use
29
 * the MatchValidator class instead.
30
 *
31
 * Below is an example usage for your Propel xml schema file.
32
 *
33
 * <code>
34
 *   <column name="ISBN" type="VARCHAR" size="20" required="true" />
35
 *   <validator column="username">
36
 *     <!-- disallow everything that's not a digit or minus -->
37
 *     <rule
38
 *       name="notMatch"
39
 *       value="/[^\d-]+/"
40
 *       message="Please enter a valid email adress." />
41
 *   </validator>
42
 * </code>
43
 *
44
 * @author     Michael Aichler <aichler@mediacluster.de>
45
 * @author     Hans Lellelid <hans@xmpl.org>
46
 * @version    $Revision: 1262 $
47
 * @package    propel.validator
48
 */
49
class NotMatchValidator implements BasicValidator
50
{
51
	/**
52
	 * Prepares the regular expression entered in the XML
53
	 * for use with preg_match().
54
	 * @param      string $exp
55
	 * @return     string Prepared regular expession.
56
	 */
57
	private function prepareRegexp($exp)
58
	{
59
		// remove surrounding '/' marks so that they don't get escaped in next step
60
		if ($exp{0} !== '/' || $exp{strlen($exp)-1} !== '/' ) {
61
			$exp = '/' . $exp . '/';
62
		}
63
 
64
		// if they did not escape / chars; we do that for them
65
		$exp = preg_replace('/([^\\\])\/([^$])/', '$1\/$2', $exp);
66
 
67
		return $exp;
68
	}
69
 
70
	/**
71
	 * Whether the passed string matches regular expression.
72
	 */
73
	public function isValid (ValidatorMap $map, $str)
74
	{
75
		return (preg_match($this->prepareRegexp($map->getValue()), $str) == 0);
76
	}
77
}