Beispiel #1
0
    /**
     * Returns true if and only if $value only contains digit characters
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        $this->_setValue((string) $value);

        if ('' === $this->_value) {
            $this->_error(self::STRING_EMPTY);
            return false;
        }

        if (null === self::$_filter) {
            self::$_filter = new \Zend\Filter\Digits();
        }

        if ($this->_value !== self::$_filter->filter($this->_value)) {
            $this->_error(self::NOT_DIGITS);
            return false;
        }

        return true;
    }
Beispiel #2
0
 /**
  * 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 (null === self::$_filter) {
         self::$_filter = new \Zend\Filter\Digits();
     }
     $valueFiltered = self::$_filter->__invoke($value);
     $length = strlen($valueFiltered);
     if ($length < 13 || $length > 19) {
         $this->_error(self::LENGTH);
         return false;
     }
     $sum = 0;
     $weight = 2;
     for ($i = $length - 2; $i >= 0; $i--) {
         $digit = $weight * $valueFiltered[$i];
         $sum += floor($digit / 10) + $digit % 10;
         $weight = $weight % 2 + 1;
     }
     if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
         $this->_error(self::CHECKSUM, $valueFiltered);
         return false;
     }
     return true;
 }
Beispiel #3
0
 /**
  * Returns true if and only if $value only contains digit characters
  *
  * @param  string $value
  * @return bool
  */
 public function isValid($value)
 {
     if (!is_string($value) && !is_int($value) && !is_float($value)) {
         $this->error(self::INVALID);
         return false;
     }
     $this->setValue((string) $value);
     if ('' === $this->getValue()) {
         $this->error(self::STRING_EMPTY);
         return false;
     }
     if (null === static::$filter) {
         static::$filter = new DigitsFilter();
     }
     if ($this->getValue() !== static::$filter->filter($this->getValue())) {
         $this->error(self::NOT_DIGITS);
         return false;
     }
     return true;
 }