Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * sfValidatorRegex validates a value with a regular expression.
13
 *
14
 * @package    symfony
15
 * @subpackage validator
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfValidatorRegex.class.php 22149 2009-09-18 14:09:53Z Kris.Wallsmith $
18
 */
19
class sfValidatorRegex extends sfValidatorString
20
{
21
  /**
22
   * Configures the current validator.
23
   *
24
   * Available options:
25
   *
26
   *  * pattern:    A regex pattern compatible with PCRE or {@link sfCallable} that returns one (required)
27
   *  * must_match: Whether the regex must match or not (true by default)
28
   *
29
   * @param array $options   An array of options
30
   * @param array $messages  An array of error messages
31
   *
32
   * @see sfValidatorString
33
   */
34
  protected function configure($options = array(), $messages = array())
35
  {
36
    parent::configure($options, $messages);
37
 
38
    $this->addRequiredOption('pattern');
39
    $this->addOption('must_match', true);
40
  }
41
 
42
  /**
43
   * @see sfValidatorString
44
   */
45
  protected function doClean($value)
46
  {
47
    $clean = parent::doClean($value);
48
 
49
    $pattern = $this->getPattern();
50
 
51
    if (
52
      ($this->getOption('must_match') && !preg_match($pattern, $clean))
53
      ||
54
      (!$this->getOption('must_match') && preg_match($pattern, $clean))
55
    )
56
    {
57
      throw new sfValidatorError($this, 'invalid', array('value' => $value));
58
    }
59
 
60
    return $clean;
61
  }
62
 
63
  /**
64
   * Returns the current validator's regular expression.
65
   *
66
   * @return string
67
   */
68
  public function getPattern()
69
  {
70
    $pattern = $this->getOption('pattern');
71
 
72
    return $pattern instanceof sfCallable ? $pattern->call() : $pattern;
73
  }
74
}