public function testSettingDefaultOptionsAfterwards() { $valid = new Validator\Callback(array($this, 'objectCallback')); $valid->setOptions('options'); $this->assertEquals(array('options'), $valid->getOptions()); $this->assertTrue($valid->isValid('test')); }
/** * {@inheritDoc} */ public function setOptions($options = []) { if (isset($options['messageVariables'])) { $this->abstractOptions['messageVariables'] = $options['messageVariables']; } return parent::setOptions($options); }
/** * Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum) * * @param string $value * @return boolean */ public function isValid($value) { $this->setValue($value); if (!is_string($value)) { $this->error(self::INVALID, $value); return false; } if (!ctype_digit($value)) { $this->error(self::CONTENT, $value); return false; } $length = strlen($value); $types = $this->getType(); $foundp = false; $foundl = false; foreach ($types as $type) { foreach ($this->cardType[$type] as $prefix) { if (substr($value, 0, strlen($prefix)) == $prefix) { $foundp = true; if (in_array($length, $this->cardLength[$type])) { $foundl = true; break 2; } } } } if ($foundp == false) { $this->error(self::PREFIX, $value); return false; } if ($foundl == false) { $this->error(self::LENGTH, $value); return false; } $sum = 0; $weight = 2; for ($i = $length - 2; $i >= 0; $i--) { $digit = $weight * $value[$i]; $sum += floor($digit / 10) + $digit % 10; $weight = $weight % 2 + 1; } if ((10 - $sum % 10) % 10 != $value[$length - 1]) { $this->error(self::CHECKSUM, $value); return false; } $service = $this->getService(); if (!empty($service)) { try { $callback = new Callback($service); $callback->setOptions($this->getType()); if (!$callback->isValid($value)) { $this->error(self::SERVICE, $value); return false; } } catch (\Exception $e) { $this->error(self::SERVICEFAILURE, $value); return false; } } return true; }
/** * Returns true if and only if $value is a valid postalcode * * @param string $value * @return bool * @throws Exception\InvalidArgumentException */ public function isValid($value) { if (!is_string($value) && !is_int($value)) { $this->error(self::INVALID); return false; } $this->setValue($value); $service = $this->getService(); $locale = $this->getLocale(); $format = $this->getFormat(); if ((null === $format || '' === $format) && !empty($locale)) { $region = Locale::getRegion($locale); if ('' === $region) { throw new Exception\InvalidArgumentException("Locale must contain a region"); } if (isset(static::$postCodeRegex[$region])) { $format = static::$postCodeRegex[$region]; } } if (null === $format || '' === $format) { throw new Exception\InvalidArgumentException("A postcode-format string has to be given for validation"); } if ($format[0] !== '/') { $format = '/^' . $format; } if ($format[strlen($format) - 1] !== '/') { $format .= '$/'; } if (!empty($service)) { if (!is_callable($service)) { throw new Exception\InvalidArgumentException('Invalid callback given'); } try { $callback = new Callback($service); $callback->setOptions(array('format' => $format, 'locale' => $locale)); if (!$callback->isValid($value)) { $this->error(self::SERVICE, $value); return false; } } catch (\Exception $e) { $this->error(self::SERVICEFAILURE, $value); return false; } } if (!preg_match($format, $value)) { $this->error(self::NO_MATCH); return false; } return true; }
/** * Returns true if and only if $value is a valid postalcode * * @param string $value * @return boolean */ public function isValid($value) { $this->setValue($value); if (!is_string($value) && !is_int($value)) { $this->error(self::INVALID); return false; } $service = $this->getService(); if (!empty($service)) { try { $callback = new Callback($service); $callback->setOptions(array('format' => $this->options['format'], 'locale' => $this->options['locale'])); if (!$callback->isValid($value)) { $this->error(self::SERVICE, $value); return false; } } catch (\Exception $e) { $this->error(self::SERVICEFAILURE, $value); return false; } } $format = $this->getFormat(); if (!preg_match($format, $value)) { $this->error(self::NO_MATCH); return false; } return true; }