|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenTelemetry\Extension\Propagator\XCloudTrace; |
| 6 | + |
| 7 | +/** |
| 8 | + * This class contains utilities that are used by the XCloudTracePropagator. |
| 9 | + * This class mostly contains numerical handling functions to work with |
| 10 | + * trace and span IDs. |
| 11 | + */ |
| 12 | +final class Utils |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * Pads the string with zero string characters on left hand side, to max total string size. |
| 17 | + * |
| 18 | + * @param string $str The string to pad. |
| 19 | + * @param int $amount Total String size, default is 16. |
| 20 | + * @return string The padded string |
| 21 | + */ |
| 22 | + public static function leftZeroPad(string $str, int $amount = 16) : string |
| 23 | + { |
| 24 | + return str_pad($str, $amount, '0', STR_PAD_LEFT); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Converts a decimal number in string format to a hex number in string format. |
| 29 | + * The returned number will not start with 0x. |
| 30 | + * |
| 31 | + * @param string $num The number to convert. |
| 32 | + * @return string The converted number. |
| 33 | + */ |
| 34 | + public static function decToHex(string $num) : string |
| 35 | + { |
| 36 | + $int = (int) $num; |
| 37 | + if (self::isBigNum($int)) { |
| 38 | + return self::baseConvert($num, 10, 16); |
| 39 | + } |
| 40 | + |
| 41 | + return dechex($int); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Converts a hex number in string format to a decimal number in string format. |
| 46 | + * The given number does not have to start with 0x. |
| 47 | + * |
| 48 | + * @param string $num The number to convert. |
| 49 | + * @return string The converted number. |
| 50 | + */ |
| 51 | + public static function hexToDec(string $num) : string |
| 52 | + { |
| 53 | + $dec = hexdec($num); |
| 54 | + if (self::isBigNum($dec)) { |
| 55 | + return self::baseConvert($num, 16, 10); |
| 56 | + } |
| 57 | + |
| 58 | + return (string) $dec; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Tests whether the given number is larger than the maximum integer of the installed PHP's build. |
| 63 | + * On 32-bit system it's 2147483647 and on 64-bit it's 9223372036854775807. |
| 64 | + * We are comparing with >= and no >, because this function is used in context of what method to use |
| 65 | + * to convert to some base (in our case hex to octal and vice versa). |
| 66 | + * So it's ok if we use >=, because it means that only for MAX_INT we will use the slower baseConvert |
| 67 | + * method. |
| 68 | + * |
| 69 | + * @param int|float $number The number to test. |
| 70 | + * @return bool Whether it was bigger or not than the max. |
| 71 | + */ |
| 72 | + public static function isBigNum($number) : bool |
| 73 | + { |
| 74 | + return $number >= PHP_INT_MAX; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Custom function to convert a number in string format from one base to another. |
| 79 | + * Built-in functions, specifically for hex, do not work well in PHP under |
| 80 | + * all versions (32/64-bit) or if the number only fits into an unsigned long. |
| 81 | + * PHP does not have unsigned longs, so this function is necessary. |
| 82 | + * |
| 83 | + * @param string $num The number to convert (in some base). |
| 84 | + * @param int $fromBase The base to convert from. |
| 85 | + * @param int $toBase The base to convert to. |
| 86 | + * @return string Converted number in the new base. |
| 87 | + */ |
| 88 | + public static function baseConvert(string $num, int $fromBase, int $toBase) : string |
| 89 | + { |
| 90 | + $num = strtolower($num); |
| 91 | + $chars = '0123456789abcdefghijklmnopqrstuvwxyz'; |
| 92 | + $newstring = substr($chars, 0, $toBase); |
| 93 | + |
| 94 | + $length = strlen($num); |
| 95 | + $result = ''; |
| 96 | + |
| 97 | + $number = []; |
| 98 | + for ($i = 0; $i < $length; $i++) { |
| 99 | + $number[$i] = strpos($chars, $num[$i]); |
| 100 | + } |
| 101 | + |
| 102 | + do { |
| 103 | + $divide = 0; |
| 104 | + $newlen = 0; |
| 105 | + for ($i = 0; $i < $length; $i++) { |
| 106 | + if (!isset($number[$i]) || $number[$i] === false) { |
| 107 | + return ''; |
| 108 | + } |
| 109 | + $divide = $divide * $fromBase + $number[$i]; |
| 110 | + if ($divide >= $toBase) { |
| 111 | + $number[$newlen++] = (int) ($divide / $toBase); |
| 112 | + $divide %= $toBase; |
| 113 | + } elseif ($newlen > 0) { |
| 114 | + $number[$newlen++] = 0; |
| 115 | + } |
| 116 | + } |
| 117 | + $length = $newlen; |
| 118 | + $result = $newstring[$divide] . $result; |
| 119 | + } while ($newlen != 0); |
| 120 | + |
| 121 | + return $result; |
| 122 | + } |
| 123 | +} |
0 commit comments