~ubuntu-branches/ubuntu/vivid/zendframework/vivid

« back to all changes in this revision

Viewing changes to library/Zend/Validate/Regex.php

  • Committer: Bazaar Package Importer
  • Author(s): Frank Habermann
  • Date: 2010-04-28 20:10:00 UTC
  • mfrom: (1.3.1 upstream) (9.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100428201000-o347bj5qb5i3tpot
Tags: 1.10.4-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * @package    Zend_Validate
17
17
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18
18
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
19
 
 * @version    $Id: Regex.php 20358 2010-01-17 19:03:49Z thomas $
 
19
 * @version    $Id: Regex.php 21574 2010-03-19 20:00:37Z thomas $
20
20
 */
21
21
 
22
22
/**
34
34
{
35
35
    const INVALID   = 'regexInvalid';
36
36
    const NOT_MATCH = 'regexNotMatch';
 
37
    const ERROROUS  = 'regexErrorous';
37
38
 
38
39
    /**
39
40
     * @var array
41
42
    protected $_messageTemplates = array(
42
43
        self::INVALID   => "Invalid type given, value should be string, integer or float",
43
44
        self::NOT_MATCH => "'%value%' does not match against pattern '%pattern%'",
 
45
        self::ERROROUS  => "There was an internal error while using the pattern '%pattern%'",
44
46
    );
45
47
 
46
48
    /**
61
63
     * Sets validator options
62
64
     *
63
65
     * @param  string|Zend_Config $pattern
 
66
     * @throws Zend_Validate_Exception On missing 'pattern' parameter
64
67
     * @return void
65
68
     */
66
69
    public function __construct($pattern)
95
98
     * Sets the pattern option
96
99
     *
97
100
     * @param  string $pattern
 
101
     * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
98
102
     * @return Zend_Validate_Regex Provides a fluent interface
99
103
     */
100
104
    public function setPattern($pattern)
101
105
    {
102
106
        $this->_pattern = (string) $pattern;
 
107
        $status         = @preg_match($this->_pattern, "Test");
 
108
 
 
109
        if (false === $status) {
 
110
            require_once 'Zend/Validate/Exception.php';
 
111
            throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");
 
112
        }
 
113
 
103
114
        return $this;
104
115
    }
105
116
 
109
120
     * Returns true if and only if $value matches against the pattern option
110
121
     *
111
122
     * @param  string $value
112
 
     * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
113
123
     * @return boolean
114
124
     */
115
125
    public function isValid($value)
123
133
 
124
134
        $status = @preg_match($this->_pattern, $value);
125
135
        if (false === $status) {
126
 
            require_once 'Zend/Validate/Exception.php';
127
 
            throw new Zend_Validate_Exception("Internal error matching pattern '$this->_pattern' against value '$value'");
 
136
            $this->_error(self::ERROROUS);
 
137
            return false;
128
138
        }
 
139
 
129
140
        if (!$status) {
130
141
            $this->_error(self::NOT_MATCH);
131
142
            return false;
132
143
        }
 
144
 
133
145
        return true;
134
146
    }
135
 
 
136
147
}