OpenTelemetry PHP

Utils
in package

FinalYes

This class contains utilities that are used by the CloudTracePropagator.

This class mostly contains numerical handling functions to work with trace and span IDs.

Table of Contents

Methods

baseConvert()  : string
Custom function to convert a number in string format from one base to another.
decToHex()  : string
Converts a decimal number in string format to a hex number in string format.
hexToDec()  : string
Converts a hex number in string format to a decimal number in string format.
isBigNum()  : bool
Tests whether the given number is larger than the maximum integer of the installed PHP's build.
leftZeroPad()  : string
Pads the string with zero string characters on left hand side, to max total string size.

Methods

baseConvert()

Custom function to convert a number in string format from one base to another.

public static baseConvert(string $num, int $fromBase, int $toBase) : string

Built-in functions, specifically for hex, do not work well in PHP under all versions (32/64-bit) or if the number only fits into an unsigned long. PHP does not have unsigned longs, so this function is necessary.

Parameters
$num : string

The number to convert (in some base).

$fromBase : int

The base to convert from.

$toBase : int

The base to convert to.

Return values
string

Converted number in the new base.

decToHex()

Converts a decimal number in string format to a hex number in string format.

public static decToHex(string $num) : string

The returned number will not start with 0x.

Parameters
$num : string

The number to convert.

Return values
string

The converted number.

hexToDec()

Converts a hex number in string format to a decimal number in string format.

public static hexToDec(string $num) : string

The given number does not have to start with 0x.

Parameters
$num : string

The number to convert.

Return values
string

The converted number.

isBigNum()

Tests whether the given number is larger than the maximum integer of the installed PHP's build.

public static isBigNum(int|float $number) : bool

On 32-bit system it's 2147483647 and on 64-bit it's 9223372036854775807. We are comparing with >= and no >, because this function is used in context of what method to use to convert to some base (in our case hex to octal and vice versa). So it's ok if we use >=, because it means that only for MAX_INT we will use the slower baseConvert method.

Parameters
$number : int|float

The number to test.

Return values
bool

Whether it was bigger or not than the max.

leftZeroPad()

Pads the string with zero string characters on left hand side, to max total string size.

public static leftZeroPad(string $str[, int $amount = 16 ]) : string
Parameters
$str : string

The string to pad.

$amount : int = 16

Total String size, default is 16.

Return values
string

The padded string


        
On this page

Search results