/** * @param $uid * @return HasSageId * @throws \LogicException */ public function generateSageId($uid) { $uidBase = ''; if (LuhnCheckDigit::validateNumber($uid) && strlen($uid) === 12) { $uidBase = substr((string) $uid, 1, 10); } else { throw new \LogicException('The uid supplied did not pass validation'); } $this->sageId = strtoupper(sprintf('L%07s', base_convert($uidBase, 10, 36))); return $this; }
/** * @param mixed $uid * @return bool|int */ public function isValid($uid) { $result = true; $this->setValue(trim($uid)); if (!preg_match($this->uidRegexp, $this->getValue())) { $this->error(self::NOT_CORRECT_FORMAT); $result &= false; } if (false == LuhnCheckDigit::validateNumber($uid)) { $this->error(self::CHECKSUM_NOT_VALID); $result &= false; } return $result; }
public function testValidateBad() { $expected = 8; $this->assertFalse(LuhnCheckDigit::validateNumber('1234' . $expected)); }