/**
   * Executes this validator.
   *
   * @param mixed A parameter value
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    $decodedValue = sfToolkit::isUTF8($value) && function_exists('utf8_decode') ? utf8_decode($value) : $value;

    $min = $this->getParameterHolder()->get('min');
    if ($min !== null && strlen(trim($decodedValue)) < $min)
    {
      // too short
      $error = $this->getParameterHolder()->get('min_error');

      return false;
    }

    $max = $this->getParameterHolder()->get('max');
    if ($max !== null && strlen(trim($decodedValue)) > $max)
    {
      // too long
      $error = $this->getParameterHolder()->get('max_error');

      return false;
    }

    $values = $this->getParameterHolder()->get('values');
    if ($values !== null)
    {
      if ($this->getParameterHolder()->get('insensitive'))
      {
        $value = strtolower($value);
        $found = false;
        foreach ($values as $avalue)
        {
          if ($value == strtolower($avalue))
          {
            $found = true;
            break;
          }
        }
        if (!$found)
        {
          // can't find a match
          $error = $this->getParameterHolder()->get('values_error');

          return false;
        }
      }
      else
      {
        if (!in_array($value, (array) $values))
        {
          // can't find a match
          $error = $this->getParameterHolder()->get('values_error');

          return false;
        }
      }
    }

    return true;
  }
Example #2
0
  'foo_1=bar_1' => array('foo_1' => 'bar_1'),
);

foreach ($tests as $string => $attributes)
{
  $t->is(sfToolkit::stringToArray($string), $attributes, '->stringToArray()');
}

// ::isUTF8()
$t->diag('::isUTF8()');
$t->is('été', true, '::isUTF8() returns true if the parameter is an UTF-8 encoded string');
$t->is(sfToolkit::isUTF8('AZERTYazerty1234-_'), true, '::isUTF8() returns true if the parameter is an UTF-8 encoded string');
$t->is(sfToolkit::isUTF8('AZERTYazerty1234-_'.chr(254)), false, '::isUTF8() returns false if the parameter is not an UTF-8 encoded string');
// check a very long string
$string = str_repeat('Here is an UTF8 string avec du français.', 1000);
$t->is(sfToolkit::isUTF8($string), true, '::isUTF8() can operate on very large strings');

// ::literalize()
$t->diag('::literalize()');
foreach (array('true', 'on', '+', 'yes') as $param)
{
  $t->is(sfToolkit::literalize($param), true, sprintf('::literalize() returns true with "%s"', $param));
  if (strtoupper($param) != $param)
  {
    $t->is(sfToolkit::literalize(strtoupper($param)), true, sprintf('::literalize() returns true with "%s"', strtoupper($param)));
  }
  $t->is(sfToolkit::literalize(' '.$param.' '), true, sprintf('::literalize() returns true with "%s"', ' '.$param.' '));
}

foreach (array('false', 'off', '-', 'no') as $param)
{