-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathNotMatchValidator.php
More file actions
73 lines (68 loc) · 1.94 KB
/
NotMatchValidator.php
File metadata and controls
73 lines (68 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
/**
* A validator for regular expressions.
*
* This validator will return true, when the passed value does *not* match
* the regular expression.
*
* If you do want to test if the value *matches* an expression, you can use
* the MatchValidator class instead.
*
* Below is an example usage for your Propel xml schema file.
*
* <code>
* <column name="ISBN" type="VARCHAR" size="20" required="true" />
* <validator column="username">
* <!-- disallow everything that's not a digit or minus -->
* <rule
* name="notMatch"
* value="/[^\d-]+/"
* message="Please enter a valid email address." />
* </validator>
* </code>
*
* @author Michael Aichler <aichler@mediacluster.de>
* @author Hans Lellelid <hans@xmpl.org>
* @version $Revision$
* @package propel.runtime.validator
*/
class NotMatchValidator implements BasicValidator
{
/**
* Prepares the regular expression entered in the XML
* for use with preg_match().
*
* @param string $exp
*
* @return string
*/
private function prepareRegexp($exp)
{
// remove surrounding '/' marks so that they don't get escaped in next step
if ($exp[0] !== '/' || $exp[strlen($exp) - 1] !== '/') {
$exp = '/' . $exp . '/';
}
// if they did not escape / chars; we do that for them
$exp = preg_replace('/([^\\\])\/([^$])/', '$1\/$2', $exp);
return $exp;
}
/**
* @see BasicValidator::isValid()
*
* @param ValidatorMap $map
* @param string $str
*
* @return boolean
*/
public function isValid(ValidatorMap $map, $str)
{
return (preg_match($this->prepareRegexp($map->getValue()), $str) == 0);
}
}