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

35 lines
738 B
PHP
Raw Normal View History

2024-01-20 20:48:47 +00:00
<?php
/**
* @template T
* @param T|false $x
* @return T
*/
2024-02-05 13:41:32 +00:00
function false_throws(mixed $x, ?callable $errorgen = null): mixed
2024-01-20 20:48:47 +00:00
{
if($x === false) {
$msg = "Unexpected false";
if($errorgen) {
$msg = $errorgen();
}
throw new \Exception($msg);
2024-01-20 20:48:47 +00:00
}
return $x;
}
# https://github.com/thecodingmachine/safe/pull/428
2024-01-20 20:48:47 +00:00
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 filter_var_ex(mixed $variable, int $filter = FILTER_DEFAULT, mixed $options = null): mixed
{
return false_throws(filter_var($variable, $filter, $options));
}