diff --git a/composer.json b/composer.json index 0c2a374..53d408e 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ ], "require": { "php": "^7.1 || ^8.0", + "ext-json": "*", "paragonie/random-lib": "^2.0", "symfony/polyfill-mbstring": "^1.19" }, diff --git a/src/Factory.php b/src/Factory.php index 5686323..352924a 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -27,17 +27,21 @@ final class Factory */ private static $factory; + /** + * @throws InvalidShortidException + */ public function generate(int $length = null, string $alphabet = null, bool $readable = null): Shortid { - $length = null === $length ? $this->length : $length; - $readable = null === $readable ? $this->readable : $readable; + $length = $length ?? $this->length; + $readable = $readable ?? $this->readable; if (null === $alphabet && $readable) { - $alphabet = Generator::EASY_TO_READ; + $alphabet = \str_replace(\str_split(Generator::AMBIGUOUS_CHARS), '', $this->alphabet); + $alphabet .= \str_repeat('_', \strlen(Generator::AMBIGUOUS_CHARS) / 2); + $alphabet .= \str_repeat('-', \strlen(Generator::AMBIGUOUS_CHARS) / 2); } - $alphabet = null === $alphabet ? $this->alphabet : $alphabet; - $id = self::getFactory()->getMediumStrengthGenerator()->generateString($length, $alphabet); + $id = self::getFactory()->getMediumStrengthGenerator()->generateString($length, $alphabet ?? $this->alphabet); - return new Shortid($id); + return new Shortid($id, $length, $alphabet); } public function setAlphabet(string $alphabet): void diff --git a/src/InvalidShortidException.php b/src/InvalidShortidException.php new file mode 100644 index 0000000..6f4edb9 --- /dev/null +++ b/src/InvalidShortidException.php @@ -0,0 +1,7 @@ +id = $id; } @@ -24,6 +31,9 @@ public function __toString(): string return $this->id; } + /** + * @throws InvalidShortidException + */ public static function generate(int $length = null, string $alphabet = null, bool $readable = false): self { if (null === $length) { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 65a57d6..2ea75a7 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -22,7 +22,7 @@ public function testGenerate(): void { $generated = $this->factory->generate(); - $this->assertRegExp('/^[a-z0-9\_\-]{7,7}$/i', $generated->__toString()); + self::assertRegExp('/^[a-z0-9\_\-]{7,7}$/i', $generated->__toString()); } /** @@ -57,7 +57,7 @@ public function testSetAlphabet(string $alphabet): void $newAlphabet = $this->factory->getAlphabet(); - $this->assertSame($alphabet, $newAlphabet); + self::assertSame($alphabet, $newAlphabet); } /** @@ -77,7 +77,7 @@ public function wrongAlphabetsProvider(): array { return [ 'test' => ['test'], - 'rand' => [\sha1((string) \random_int(0, \getrandmax()))], + 'rand' => [\sha1((string) \random_int(0, \mt_getrandmax()))], ]; } @@ -85,19 +85,19 @@ public function testGetFactory(): void { $factory = Factory::getFactory(); - $this->assertInstanceOf(RandomLibFactory::class, $factory); + self::assertInstanceOf(RandomLibFactory::class, $factory); } public function testSetLength(): void { $this->factory->setLength(5); - $this->assertSame(5, $this->factory->getLength()); + self::assertSame(5, $this->factory->getLength()); } public function testCheckLength(): void { $null = $this->factory->checkLength(null, false); - $this->assertNull($null); + self::assertNull($null); } public function testSetWrongLengthType(): void diff --git a/tests/ShortidTest.php b/tests/ShortidTest.php index 9dc4eeb..b4fd690 100644 --- a/tests/ShortidTest.php +++ b/tests/ShortidTest.php @@ -5,6 +5,7 @@ use JsonSerializable; use PHPUnit\Framework\TestCase; use PUGX\Shortid\Factory; +use PUGX\Shortid\InvalidShortidException; use PUGX\Shortid\Shortid; final class ShortidTest extends TestCase @@ -18,28 +19,28 @@ public function testGenerate(): void { $generated = Shortid::generate(); - $this->assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString()); + self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString()); } public function testGenerateWithReadable(): void { $generated = Shortid::generate(null, null, true); - $this->assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString()); + self::assertRegExp('/^[a-z0-9\_\-]{7}$/i', $generated->__toString()); } public function testGenerateWithLength(): void { $generated = Shortid::generate(8); - $this->assertRegExp('/^[a-z0-9\_\-]{8}$/i', $generated->__toString()); + self::assertRegExp('/^[a-z0-9\_\-]{8}$/i', $generated->__toString()); } public function testGetFactory(): void { $factory = Shortid::getFactory(); - $this->assertInstanceOf(Factory::class, $factory); + self::assertInstanceOf(Factory::class, $factory); } public function testSetFactory(): void @@ -47,18 +48,18 @@ public function testSetFactory(): void $factory = new Factory(); Shortid::setFactory($factory); - $this->assertSame($factory, Shortid::getFactory()); + self::assertSame($factory, Shortid::getFactory()); } public function testIsValid(): void { - $this->assertTrue(Shortid::isValid('shortid')); + self::assertTrue(Shortid::isValid('shortid')); } public function testIsNotValid(): void { - $this->assertFalse(Shortid::isValid('/(;#!')); - $this->assertFalse(Shortid::isValid('harmful string stuff')); + self::assertFalse(Shortid::isValid('/(;#!')); + self::assertFalse(Shortid::isValid('harmful string stuff')); } public function testIsValidWithRegexChar(): void @@ -67,28 +68,28 @@ public function testIsValidWithRegexChar(): void $factory->setAlphabet('hìjklmnòpqrstùvwxyzABCDEFGHIJKLMNOPQRSTUVWX.\+*?[^]$(){}=!<>|:-/'); Shortid::setFactory($factory); - $this->assertTrue(Shortid::isValid('slsh/]?')); + self::assertTrue(Shortid::isValid('slsh/]?')); } public function testJsonSerializable(): void { $generated = Shortid::generate(); - $this->assertInstanceOf(JsonSerializable::class, $generated); + self::assertInstanceOf(JsonSerializable::class, $generated); } public function testJsonEncode(): void { $generated = Shortid::generate(); - $this->assertSame('"'.$generated.'"', \json_encode($generated)); + self::assertSame('"'.$generated.'"', \json_encode($generated)); } public function testSerialize(): void { $shortid = new Shortid('shortid'); - $this->assertSame('shortid', $shortid->serialize()); + self::assertSame('shortid', $shortid->serialize()); } public function testUnserialize(): void @@ -96,14 +97,14 @@ public function testUnserialize(): void $shortid = Shortid::generate(); $shortid->unserialize('shortid'); - $this->assertSame('shortid', (string) $shortid); + self::assertSame('shortid', (string) $shortid); } public function testMagicSerialize(): void { $shortid = new Shortid('shortid'); - $this->assertSame(['id' => 'shortid'], $shortid->__serialize()); + self::assertSame(['id' => 'shortid'], $shortid->__serialize()); } public function testMagicUnserialize(): void @@ -111,6 +112,12 @@ public function testMagicUnserialize(): void $shortid = Shortid::generate(); $shortid->__unserialize(['id' => 'shortid']); - $this->assertSame('shortid', (string) $shortid); + self::assertSame('shortid', (string) $shortid); + } + + public function testInvalidArgumentInConstructor(): void + { + $this->expectException(InvalidShortidException::class); + new Shortid('an_invalid_too_long_shortid'); } }