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
 * sfValidatorChoice validates than the value is one of the expected values.
13
 *
14
 * @package    symfony
15
 * @subpackage validator
16
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17
 * @version    SVN: $Id: sfValidatorChoice.class.php 22264 2009-09-23 05:54:32Z fabien $
18
 */
19
class sfValidatorChoice extends sfValidatorBase
20
{
21
  /**
22
   * Configures the current validator.
23
   *
24
   * Available options:
25
   *
26
   *  * choices:  An array of expected values (required)
27
   *  * multiple: true if the select tag must allow multiple selections
28
   *  * min:      The minimum number of values that need to be selected (this option is only active if multiple is true)
29
   *  * max:      The maximum number of values that need to be selected (this option is only active if multiple is true)
30
   *
31
   * @param array $options    An array of options
32
   * @param array $messages   An array of error messages
33
   *
34
   * @see sfValidatorBase
35
   */
36
  protected function configure($options = array(), $messages = array())
37
  {
38
    $this->addRequiredOption('choices');
39
    $this->addOption('multiple', false);
40
    $this->addOption('min');
41
    $this->addOption('max');
42
 
43
    $this->addMessage('min', 'At least %min% values must be selected (%count% values selected).');
44
    $this->addMessage('max', 'At most %max% values must be selected (%count% values selected).');
45
  }
46
 
47
  /**
48
   * @see sfValidatorBase
49
   */
50
  protected function doClean($value)
51
  {
52
    $choices = $this->getChoices();
53
 
54
    if ($this->getOption('multiple'))
55
    {
56
      $value = $this->cleanMultiple($value, $choices);
57
    }
58
    else
59
    {
60
      if (!self::inChoices($value, $choices))
61
      {
62
        throw new sfValidatorError($this, 'invalid', array('value' => $value));
63
      }
64
    }
65
 
66
    return $value;
67
  }
68
 
69
  public function getChoices()
70
  {
71
    $choices = $this->getOption('choices');
72
    if ($choices instanceof sfCallable)
73
    {
74
      $choices = $choices->call();
75
    }
76
 
77
    return $choices;
78
  }
79
 
80
  /**
81
   * Cleans a value when multiple is true.
82
   *
83
   * @param  mixed $value The submitted value
84
   *
85
   * @return array The cleaned value
86
   */
87
  protected function cleanMultiple($value, $choices)
88
  {
89
    if (!is_array($value))
90
    {
91
      $value = array($value);
92
    }
93
 
94
    foreach ($value as $v)
95
    {
96
      if (!self::inChoices($v, $choices))
97
      {
98
        throw new sfValidatorError($this, 'invalid', array('value' => $v));
99
      }
100
    }
101
 
102
    $count = count($value);
103
 
104
    if ($this->hasOption('min') && $count < $this->getOption('min'))
105
    {
106
      throw new sfValidatorError($this, 'min', array('count' => $count, 'min' => $this->getOption('min')));
107
    }
108
 
109
    if ($this->hasOption('max') && $count > $this->getOption('max'))
110
    {
111
      throw new sfValidatorError($this, 'max', array('count' => $count, 'max' => $this->getOption('max')));
112
    }
113
 
114
    return $value;
115
  }
116
 
117
  /**
118
   * Checks if a value is part of given choices (see bug #4212)
119
   *
120
   * @param  mixed $value   The value to check
121
   * @param  array $choices The array of available choices
122
   *
123
   * @return Boolean
124
   */
125
  static protected function inChoices($value, array $choices = array())
126
  {
127
    foreach ($choices as $choice)
128
    {
129
      if ((string) $choice == (string) $value)
130
      {
131
        return true;
132
      }
133
    }
134
 
135
    return false;
136
  }
137
}