dsadsa - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of dsadsa.php
- <?php
- declare(strict_types=1);
- namespace Kyoto\Sandbox;
- use InvalidArgumentException;
- final class IDGenerator
- {
- /**
- * @var string[]
- */
- private array $alphabet = [
- '0', '1', '2', '3', '4', '5', '6', '7', '8',
- '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
- 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T',
- 'V', 'W', 'X', 'Y', 'Z'
- ];
- /**
- * @var string[]
- */
- private array $checksumAlphabet = [
- '*', '$', '=', '@', '&', '%'
- ];
- public function generate(string $bytes): string
- {
- $bin = gmp_strval(gmp_import($bytes), 2);
- $bin = str_pad($bin, strlen($bytes) * 8, '0', STR_PAD_LEFT);
- $bin = str_split($bin, 5);
- $last = array_pop($bin);
- if (! is_null($last)) {
- $bin[] = str_pad($last, 5, '0', STR_PAD_RIGHT);
- }
- $encoded = implode(
- '',
- array_map(
- fn ($bits) => $this->getCharFromAlphabet(bindec($bits)),
- $bin
- )
- );
- $checksum = $this->calculateChecksum($bytes);
- return $encoded . $this->getCharFromChecksumAlphabet($checksum);
- }
- public function verify(string $id): bool
- {
- $calculatedChecksum = $this->calculateChecksum($this->decode($id));
- return $this->getCharFromChecksumAlphabet($calculatedChecksum) === $this->getChecksumCharFromId($id);
- }
- public function decode(string $id): string
- {
- $normalizedId = $this->getIdWithoutChecksumChar($id);
- $normalizedId = strtoupper($normalizedId);
- $normalizedId = str_replace(['O', 'L', 'I', '-'], ['0', '1', '1', ''], $normalizedId);
- $normalizedId = str_split($normalizedId);
- $bin = implode(
- '',
- array_map(
- fn ($char) => sprintf('%05b', array_search($char, $this->alphabet)),
- $normalizedId
- )
- );
- $normalizedId = str_split($bin, 8);
- $last = array_pop($normalizedId);
- if (! is_null($last) && strlen($last) === 8) {
- $normalizedId[] = $last;
- }
- return implode(
- '',
- array_map(
- fn ($bits) => chr(intval(bindec($bits))),
- $normalizedId
- )
- );
- }
- private function calculateChecksum(string $bytes): int
- {
- $bigInt = gmp_strval(gmp_import($bytes));
- $checksum = gmp_mod($bigInt, count($this->alphabet) + count($this->checksumAlphabet));
- return intval($checksum);
- }
- private function getCharFromAlphabet(int $index): string
- {
- return $this->alphabet[$index] ?? throw new InvalidArgumentException(
- 'An array Index Out Of Bounds'
- );
- }
- private function getCharFromChecksumAlphabet(int $index): string
- {
- $mergedAlphabet = array_merge($this->alphabet, $this->checksumAlphabet);
- return $mergedAlphabet[$index] ?? throw new InvalidArgumentException(
- 'An array Index Out Of Bounds'
- );
- }
- private function getIdWithoutChecksumChar(string $id): string
- {
- return substr($id, 0, -1);
- }
- private function getChecksumCharFromId(string $id): string
- {
- return substr($id, -1);
- }
- }