/** * @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 testForCollision() { $iterations = 10000; $this->ids = $this->expectedIds = array(); for ($i = 1; $i <= $iterations; ++$i) { $uid = sprintf('7%010d', $i); $this->ids[] = sprintf('%d%d', $uid, LuhnCheckDigit::createCheckSum($uid)); $this->expectedIds[] = sprintf('L%07s', strtoupper(base_convert($i, 10, 36))); } $this->assertEquals($iterations, count(array_unique($this->expectedIds))); $this->assertEquals($iterations, count(array_unique($this->ids))); $this->assertEquals(count(array_unique($this->ids)), count(array_unique($this->expectedIds))); foreach ($this->ids as $key => $id) { $this->stub->uId = $id; $this->stub->unsetId(); $this->assertEquals($this->expectedIds[$key], $this->stub->getSageId()); $this->assertEquals(substr($id, 1, 10), base_convert(substr($this->stub->getSageId(), 1), 36, 10)); } }
public function testValidateBad() { $expected = 8; $this->assertFalse(LuhnCheckDigit::validateNumber('1234' . $expected)); }