.gitattributes000064400000000266144760116100007443 0ustar00benchmark/ export-ignore examples/ export-ignore tests/ export-ignore .gitignore export-ignore .editorconfig export-ignore *.md export-ignore *.yml export-ignore *.xml export-ignore composer.json000064400000003060144760116100007265 0ustar00{ "name": "hidehalo/nanoid-php", "type": "library", "description": "A copy of nanoid in PHP", "keywords": [ "hidehalo", "nanoid-php" ], "homepage": "https://github.com/hidehalo/nanoid-php", "license": "MIT", "authors": [ { "name": "hidehalo", "email": "tianchen_cc@yeah.net", "homepage": "https://github.com/hidehalo", "role": "Owner" } ], "require": { "php": "~5.6|~7.0|~8.0", "paragonie/random_compat": ">=2.0" }, "require-dev": { "phpunit/phpunit" : ">=5.6", "squizlabs/php_codesniffer": "^2.3" }, "archive": { "exclude": [ "tests/", "benchmark/", "examples/", ".gitignore", ".editorconfig", "*.md", "*.yml", "*.xml" ] }, "autoload": { "psr-4": { "Hidehalo\\Nanoid\\": "src" } }, "autoload-dev": { "psr-4": { "Hidehalo\\Nanoid\\Test\\": "tests" } }, "scripts": { "test": "phpunit", "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests" }, "extra": { "branch-alias": { "dev-master": "1.0-dev" } }, "config": { "sort-packages": true } } src/Client.php000064400000006251144760116100007266 0ustar00size = $size > 0 ? $size : 21; $this->generator = $generator?:new Generator(); $this->core = new Core(); $this->alphbet = CoreInterface::SAFE_SYMBOLS; } /** * Generate nanoid via optional modes * * @param integer $size * @param integer $mode Client::MODE_NORMAL|Client::MODE_DYNAMIC * @return string */ public function generateId($size = 0, $mode = self::MODE_NORMAL) { $size = $size>0? $size: $this->size; switch ($mode) { case self::MODE_DYNAMIC: return $this->core->random($this->generator, $size, $this->alphbet); default: return $this->normalRandom($size); } } /** * The original API of nanoid. Use it be careful, Please make sure * you have been implements your custom GeneratorInterface as correctly. * Otherwise use the build-in default random bytes generator * * @param GeneratorInterface $generator * @param integer $size * @param string $alphabet default CoreInterface::SAFE_SYMBOLS * @return string */ public function formattedId($alphabet, $size = 0, GeneratorInterface $generator = null) { $alphabet = $alphabet?:CoreInterface::SAFE_SYMBOLS; $size = $size>0? $size: $this->size; $generator = $generator?:$this->generator; return $this->core->random($generator, $size, $alphabet); } /** * Backwards-compatible method name. * * @param string $alphabet * @param integer $size * @param GeneratorInterface $generator * * @return string * @since 1.0.0 */ public function formatedId($alphabet, $size = 0, GeneratorInterface $generator = null) { $size = $size>0? $size: $this->size; return $this->formattedId($alphabet, $size, $generator); } /** * Generate secure URL-friendly unique ID. * By default, ID will have 21 symbols to have same collisions probability * as UUID v4. * * @see https://github.com/ai/nanoid/blob/master/non-secure/index.js#L19 * @param integer $size * @return string */ private function normalRandom($size) { $id = ''; while (1 <= $size--) { $rand = mt_rand()/(mt_getrandmax() + 1); $id .= $this->alphbet[$rand*64 | 0]; } return $id; } } src/Core.php000064400000001543144760116100006737 0ustar00random($step); for ($i = 1; $i <= $step; $i++) { $byte = $bytes[$i] & $mask; if (isset($alphabet[$byte])) { $id .= $alphabet[$byte]; if (strlen($id) === $size) { return $id; } } } } } } src/CoreInterface.php000064400000001121144760116100010550 0ustar00