|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Visus\Cuid2; |
| 6 | + |
| 7 | +/** |
| 8 | + * Utility functions for CUID2 generation. |
| 9 | + */ |
| 10 | +final class Utils |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Base36 alphabet for encoding (0-9, a-z). |
| 14 | + */ |
| 15 | + private const BASE36_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 16 | + |
| 17 | + /** |
| 18 | + * Prevents instantiation of utility class. |
| 19 | + * |
| 20 | + * @codeCoverageIgnore |
| 21 | + */ |
| 22 | + private function __construct() |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Converts a single hexadecimal character to its numeric value. |
| 28 | + * |
| 29 | + * @param string $char Single hex character (0-9, a-f, A-F). |
| 30 | + * |
| 31 | + * @return int Numeric value (0-15). |
| 32 | + */ |
| 33 | + private static function parseHexCharacter(string $char): int |
| 34 | + { |
| 35 | + return match (true) { |
| 36 | + $char >= '0' && $char <= '9' => ord($char) - 48, |
| 37 | + $char >= 'a' && $char <= 'f' => ord($char) - 87, |
| 38 | + $char >= 'A' && $char <= 'F' => ord($char) - 55, |
| 39 | + default => 0, |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Converts a hexadecimal string to large base representation. |
| 45 | + * |
| 46 | + * @param string $hexValue Hexadecimal string to convert. |
| 47 | + * @param int $base Large base for digit storage (100 million). |
| 48 | + * |
| 49 | + * @return array<int> Array of digits in large base representation. |
| 50 | + */ |
| 51 | + private static function convertHexToLargeBase(string $hexValue, int $base): array |
| 52 | + { |
| 53 | + $digits = [0]; |
| 54 | + |
| 55 | + for ($i = 0, $len = strlen($hexValue); $i < $len; $i++) { |
| 56 | + $hexDigit = self::parseHexCharacter($hexValue[$i]); |
| 57 | + $carry = $hexDigit; |
| 58 | + |
| 59 | + for ($j = 0, $jlen = count($digits); $j < $jlen; $j++) { |
| 60 | + $current = $digits[$j] * 16 + $carry; |
| 61 | + $digits[$j] = $current % $base; |
| 62 | + $carry = intdiv($current, $base); |
| 63 | + } |
| 64 | + |
| 65 | + while ($carry > 0) { |
| 66 | + $digits[] = $carry % $base; |
| 67 | + $carry = intdiv($carry, $base); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return $digits; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Converts large base digit array to base36 string. |
| 76 | + * |
| 77 | + * @param array<int> $digits Array of digits in large base representation. |
| 78 | + * @param int $base Large base (100 million). |
| 79 | + * |
| 80 | + * @return string Base36 encoded string. |
| 81 | + */ |
| 82 | + private static function convertLargeBaseToBase36(array $digits, int $base): string |
| 83 | + { |
| 84 | + $resultChars = []; |
| 85 | + |
| 86 | + while (count($digits) > 1 || $digits[0] !== 0) { |
| 87 | + $carry = 0; |
| 88 | + $newDigits = []; |
| 89 | + |
| 90 | + for ($i = count($digits) - 1; $i >= 0; $i--) { |
| 91 | + $current = $carry * $base + $digits[$i]; |
| 92 | + $quotient = intdiv($current, 36); |
| 93 | + $carry = $current % 36; |
| 94 | + |
| 95 | + if ($quotient > 0 || $newDigits !== []) { |
| 96 | + $newDigits[] = $quotient; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $resultChars[] = self::BASE36_ALPHABET[$carry]; |
| 101 | + $digits = $newDigits !== [] ? array_reverse($newDigits) : [0]; |
| 102 | + } |
| 103 | + |
| 104 | + return implode('', array_reverse($resultChars)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Converts a hexadecimal string to base36 encoding. |
| 109 | + * |
| 110 | + * This function performs arbitrary precision base conversion without requiring |
| 111 | + * the GMP extension. It uses a large intermediate base (100 million) for efficient |
| 112 | + * arithmetic operations on large numbers. |
| 113 | + * |
| 114 | + * Base36 encoding uses digits 0-9 and lowercase letters a-z (36 characters total), |
| 115 | + * producing shorter strings than hexadecimal while remaining URL-safe. |
| 116 | + * |
| 117 | + * Algorithm: |
| 118 | + * 1. Convert hex string to internal representation using base 100M |
| 119 | + * 2. Convert internal representation to base36 by repeated division |
| 120 | + * |
| 121 | + * @param string $hexValue Hexadecimal string to convert (case-insensitive). |
| 122 | + * |
| 123 | + * @return string The value encoded in base36 (lowercase alphanumeric). |
| 124 | + */ |
| 125 | + public static function hexToBase36(string $hexValue): string |
| 126 | + { |
| 127 | + if ($hexValue === '' || $hexValue === '0') { |
| 128 | + return '0'; |
| 129 | + } |
| 130 | + |
| 131 | + $base = 100_000_000; |
| 132 | + $digits = self::convertHexToLargeBase($hexValue, $base); |
| 133 | + |
| 134 | + return self::convertLargeBaseToBase36($digits, $base); |
| 135 | + } |
| 136 | +} |
0 commit comments