<?php
 abstract class vB_Utility_Functions { use vB_Utility_Trait_NoSerialize; private function __construct() { } public static function getPositiveIntParam(ArrayAccess|array $params, string|int $key, int $default, int $min = 1, int $max = PHP_INT_MAX) : int { $value = 0; if (!empty($params[$key])) { $value = intval($params[$key]); } if ($value <= 0) { $value = $default; } $value = max($value, $min); $value = min($value, $max); return $value; } public static function getNonNegativeIntParam(ArrayAccess|array $params, string|int $key, int $default, int $min = 0, int $max = PHP_INT_MAX) : int { $value = $params[$key] ?? null; if (!is_numeric($value) OR $value < 0) { $value = $default; } $value = max($value, $min); $value = min($value, $max); return $value; } public static function callSupressError(int $errorsToSuppress, callable $callback, ...$args) : mixed { $original = error_reporting(); error_reporting($original &~ $errorsToSuppress); try { $result = $callback(...$args); } finally { error_reporting($original); } return $result; } public static function callSupressErrorArray(int $errorsToSuppress, callable $callback, array $args) : mixed { $original = error_reporting(); error_reporting($original &~ $errorsToSuppress); try { $result = $callback(...$args); } finally { error_reporting($original); } return $result; } public static function setPhpTimeout(int $time) : bool { if (function_exists('set_time_limit')) { return set_time_limit($time); } return false; } public static function isAbsolutePath(string $path) : bool { if (!strlen($path)) { return false; } if (realpath($path) == $path) { return true; } if (preg_match('#^[a-zA-Z]:\\\\#', $path)) { return true; } return ($path[0] == '/' || $path[0] == '\\'); } public static function expandBitfield(int $bitfield, array $bits) : array { $values = []; foreach ($bits AS $name => $bit) { $values[$name] = ($bitfield & $bit) ? 1 : 0; } return $values; } public static function arrayColumnKeys(array $array, int|string $column_key) : array { $newarray = []; foreach ($array AS $key => $value) { if (is_array($value) AND array_key_exists($column_key, $value)) { $newarray[$key] = $value[$column_key]; } } return $newarray; } public static function iterableColumnGrouped(iterable $iterator, int|string $column_key, int|string $index_key) : array { $newarray = []; foreach ($iterator AS $value) { if ( (is_array($value) AND array_key_exists($column_key, $value) AND array_key_exists($index_key, $value)) OR (($value instanceof ArrayAccess) AND $value->offsetExists($column_key) AND $value->offsetExists($index_key)) ) { $key = $value[$index_key]; $newarray[$key] ??= []; $newarray[$key][] = $value[$column_key]; } } return $newarray; } public static function arrayKeySubset(array $array, array $keys) : array { $flippedkeys = array_flip($keys); $newarray = []; foreach ($array AS $key => $value) { if (isset($flippedkeys[$key])) { $newarray[$key] = $value; } } return $newarray; } public static function arrayCombineOptions(array $default, array $options) { $combined = []; foreach ($default AS $field => $value) { $combined[$field] = $options[$field] ?? $value; } return $combined; } } 