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
 * sfValidatorNumber validates a number (integer or float). It also converts the input value to a float.
13
 *
14
 * @package    symfony
15
 * @subpackage validator
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfValidatorNumber.class.php 22018 2009-09-14 16:56:28Z fabien $
18
 */
19
class sfValidatorNumber extends sfValidatorBase
20
{
21
  /**
22
   * Configures the current validator.
23
   *
24
   * Available options:
25
   *
26
   *  * max: The maximum value allowed
27
   *  * min: The minimum value allowed
28
   *
29
   * Available error codes:
30
   *
31
   *  * max
32
   *  * min
33
   *
34
   * @param array $options   An array of options
35
   * @param array $messages  An array of error messages
36
   *
37
   * @see sfValidatorBase
38
   */
39
  protected function configure($options = array(), $messages = array())
40
  {
41
    $this->addMessage('max', '"%value%" must be at most %max%.');
42
    $this->addMessage('min', '"%value%" must be at least %min%.');
43
 
44
    $this->addOption('min');
45
    $this->addOption('max');
46
 
47
    $this->setMessage('invalid', '"%value%" is not a number.');
48
  }
49
 
50
  /**
51
   * @see sfValidatorBase
52
   */
53
  protected function doClean($value)
54
  {
55
    if (!is_numeric($value))
56
    {
57
      throw new sfValidatorError($this, 'invalid', array('value' => $value));
58
    }
59
 
60
    $clean = floatval($value);
61
 
62
    if ($this->hasOption('max') && $clean > $this->getOption('max'))
63
    {
64
      throw new sfValidatorError($this, 'max', array('value' => $value, 'max' => $this->getOption('max')));
65
    }
66
 
67
    if ($this->hasOption('min') && $clean < $this->getOption('min'))
68
    {
69
      throw new sfValidatorError($this, 'min', array('value' => $value, 'min' => $this->getOption('min')));
70
    }
71
 
72
    return $clean;
73
  }
74
}