Files
pyrodactyl/app/helpers.php
2025-06-14 20:50:52 +01:00

52 lines
1.4 KiB
PHP

<?php
if (!function_exists('is_digit')) {
/**
* Deal with normal (and irritating) PHP behavior to determine if
* a value is a non-float positive integer.
*/
function is_digit(mixed $value): bool
{
return !is_bool($value) && ctype_digit(strval($value));
}
}
if (!function_exists('object_get_strict')) {
/**
* Get an object using dot notation. An object key with a value of null is still considered valid
* and will not trigger the response of a default value (unlike object_get).
*/
function object_get_strict(object $object, ?string $key, $default = null): mixed
{
if (is_null($key) || trim($key) == '') {
return $object;
}
foreach (explode('.', $key) as $segment) {
if (!is_object($object) || !property_exists($object, $segment)) {
return value($default);
}
$object = $object->{$segment};
}
return $object;
}
}
if (!function_exists('humanizeSize')) {
/**
* Convert bytes to a human readable format with binary units.
*/
function humanizeSize(int|float $bytes): string
{
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$i = 0;
while ($bytes >= 1024 && $i < count($units) - 1) {
$bytes /= 1024;
$i++;
}
return round($bytes, 2) . ' ' . $units[$i];
}
}