This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/stdlib_ex.php
2024-02-05 13:41:32 +00:00

96 lines
2 KiB
PHP

<?php
/**
* @template T
* @param T|false $x
* @return T
*/
function false_throws(mixed $x, ?callable $errorgen = null): mixed
{
if($x === false) {
$msg = "Unexpected false";
if($errorgen) {
$msg = $errorgen();
}
throw new \Exception($msg);
}
return $x;
}
/**
* @template T
* @param T|null $x
* @return T
*/
function null_throws(mixed $x, ?callable $errorgen = null): mixed
{
if($x === null) {
$msg = "Unexpected null";
if($errorgen) {
$msg = $errorgen();
}
throw new \Exception($msg);
}
return $x;
}
/**
* @param int<1,max> $depth
*/
function json_encode_ex(mixed $value, int|null $flags = 0, int $depth = 512): string
{
return false_throws(json_encode($value, $flags, $depth), "json_last_error_msg");
}
function strtotime_ex(string $time, int|null $now = null): int
{
return false_throws(strtotime($time, $now));
}
function md5_file_ex(string $filename, bool|null $raw_output = false): string
{
return false_throws(md5_file($filename, $raw_output));
}
/**
* @return string[]
*/
function glob_ex(string $pattern, int|null $flags = 0): array
{
return false_throws(glob($pattern, $flags));
}
function file_get_contents_ex(string $filename): string
{
return false_throws(file_get_contents($filename));
}
function filesize_ex(string $filename): int
{
return false_throws(filesize($filename));
}
function inet_ntop_ex(string $in_addr): string
{
return false_throws(inet_ntop($in_addr));
}
function inet_pton_ex(string $ip_address): string
{
return false_throws(inet_pton($ip_address));
}
function dir_ex(string $directory): \Directory
{
return false_throws(dir($directory));
}
function exec_ex(string $command): string
{
return false_throws(exec($command));
}
function filter_var_ex(mixed $variable, int $filter = FILTER_DEFAULT, mixed $options = null): mixed
{
return false_throws(filter_var($variable, $filter, $options));
}