2024-01-20 20:48:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T|false $x
|
|
|
|
* @return T
|
|
|
|
*/
|
2024-02-05 13:25:18 +00:00
|
|
|
function false_throws(mixed $x, ?callable $errorgen=null): mixed
|
2024-01-20 20:48:47 +00:00
|
|
|
{
|
|
|
|
if($x === false) {
|
2024-02-05 13:25:18 +00:00
|
|
|
$msg = "Unexpected false";
|
|
|
|
if($errorgen) {
|
|
|
|
$msg = $errorgen();
|
|
|
|
}
|
|
|
|
throw new \Exception($msg);
|
2024-01-20 20:48:47 +00:00
|
|
|
}
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param T|null $x
|
|
|
|
* @return T
|
|
|
|
*/
|
2024-02-05 13:25:18 +00:00
|
|
|
function null_throws(mixed $x, ?callable $errorgen=null): mixed
|
2024-01-20 20:48:47 +00:00
|
|
|
{
|
|
|
|
if($x === null) {
|
2024-02-05 13:25:18 +00:00
|
|
|
$msg = "Unexpected null";
|
|
|
|
if($errorgen) {
|
|
|
|
$msg = $errorgen();
|
|
|
|
}
|
|
|
|
throw new \Exception($msg);
|
2024-01-20 20:48:47 +00:00
|
|
|
}
|
|
|
|
return $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int<1,max> $depth
|
|
|
|
*/
|
|
|
|
function json_encode_ex(mixed $value, int|null $flags = 0, int $depth = 512): string
|
|
|
|
{
|
2024-02-05 13:25:18 +00:00
|
|
|
return false_throws(json_encode($value, $flags, $depth), "json_last_error_msg");
|
2024-01-20 20:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|