public function testSettingDefaultOptionsAfterwards() { $valid = new Zend_Validate_Callback(array($this, 'objectCallback')); $valid->setOptions('options'); $this->assertEquals(array('options'), $valid->getOptions()); $this->assertTrue($valid->isValid('test')); }
/** * Defined by Zend_Validate_Interface * * 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; } if (!empty($this->_service)) { try { require_once 'Zend/Validate/Callback.php'; $callback = new Zend_Validate_Callback($this->_service); $callback->setOptions($this->_type); if (!$callback->isValid($value)) { $this->_error(self::SERVICE, $value); return false; } } catch (Zend_Exception $e) { $this->_error(self::SERVICEFAILURE, $value); return false; } } return true; }
/** * @see Zend_Validate_Interface::isValid() */ public function isValid($value) { if (!is_string($value)) { $this->_error(self::INVALID); return false; } $this->_setValue($value); $mode = $this->getSeparatorMode(); if (self::FORMAT_SEPARATOR === $mode) { if (!preg_match('/^\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}$/', $value)) { $this->_error(self::INVALID_FORMAT); return false; } $value = $this->_cleanFormat($value); } else { if (self::FORMAT_AUTO === $mode) { if (!preg_match('/^\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}$/', $value) && !preg_match('/^\\d{11}$/', $value)) { $this->_error(self::INVALID_FORMAT); return false; } $value = $this->_cleanFormat($value); } else { if (self::FORMAT_CLEAN === $mode) { if (!preg_match('/^\\d{11}$/', $value)) { $this->_error(self::INVALID_FORMAT); return false; } } } } // checksum $check = $this->_checkSum($value, 10) && $this->_checkSum($value, 11); if (!$check) { $this->_error(self::NOT_CPF); return false; } if (null !== $this->_service) { require_once 'Zend/Validate/Callback.php'; $callback = new Zend_Validate_Callback($this->_service); $callback->setOptions($this->_separatorMode); if (!$callback->isValid($value)) { $this->_error(self::SERVICE, $value); return false; } } return true; }