$assocValue) { $templateVar = '{' . strtoupper($assocKey) . '}'; $templateVar = str_replace('-', '_', $templateVar); $templateVars[$templateVar] = $assocValue; } return strtr($template, $templateVars); }; } /** * @param int $decimals * @param string $decimalSeperator * @param string $thousandsSeperator * * @return Closure */ public static function number($decimals = 2, $decimalSeperator = ',', $thousandsSeperator = '.') { return static function ($value) use ($decimals, $decimalSeperator, $thousandsSeperator) { return number_format($value, $decimals, $decimalSeperator, $thousandsSeperator); }; } /** * @param int $decimals * @param string $decimalSeperator * @param string $thousandsSeperator * * @return Closure */ public static function bytes($decimals = 1, $decimalSeperator = ',', $thousandsSeperator = '.') { return static function ($bytes) use ($decimals, $decimalSeperator, $thousandsSeperator) { $bytes = (float)$bytes; $base = log($bytes, 1024); $suffixes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; $suffixIndex = (int)floor($base); $suffix = $suffixes[$suffixIndex]; $number = pow(1024, $base - floor($base)); return number_format($number, $decimals, $decimalSeperator, $thousandsSeperator) . ' ' . $suffix; }; } /** * @param string $dateFormat https://www.php.net/manual/de/function.date.php * * @return Closure */ public static function date($dateFormat) { return static function ($dateString) use ($dateFormat) { try { $date = new DateTime($dateString); return $date->format($dateFormat); } catch (Exception $exception) { return $exception->getMessage(); } }; } /** * @return Closure */ public static function htmlEscape() { return static function ($value) { return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); }; } /** * @todo Fixen * * @return Closure */ public static function dump() { return static function ($value, $row) { $data = [ 'value' => $value, 'row' => $row, ]; return sprintf( '
%s
', htmlspecialchars(var_export($data, true)) ); }; } }