/** * @dataProvider luhnProvider * * @param string $number */ public function testLuhn($number) { $rawNumber = substr($number, 0, -1); $checkDigit = substr($number, -1); // Ensure that the check digit is correctly computed. $this->assertEquals($checkDigit, Luhn::getCheckDigit($rawNumber)); // Ensure that the number is valid only with the correct check digit. for ($digit = 0; $digit < 10; $digit++) { $this->assertEquals($digit == $checkDigit, Luhn::isValid($rawNumber . $digit)); } }
/** * {@inheritdoc} */ protected function validate($value) { $length = strlen($value); $minLength = $this->hasCheckDigit === true ? 19 : 18; $maxLength = $this->hasCheckDigit === false ? 19 : 20; if ($length < $minLength || $length > $maxLength) { $this->addFailureMessage('validator.sim.invalid'); return; } if ($this->hasCheckDigit === true || $length == 20) { if (Luhn::isValid($value)) { return; } } else { if (ctype_digit($value)) { return; } } $this->addFailureMessage('validator.sim.invalid'); }
/** * {@inheritdoc} */ protected function validate($value) { if (!Luhn::isValid($value) || strlen($value) != 15) { $this->addFailureMessage('validator.imei.invalid'); } }