43 lines
1.2 KiB
PHP
43 lines
1.2 KiB
PHP
|
<?php
|
||
|
namespace nulib\os;
|
||
|
|
||
|
use RuntimeException;
|
||
|
|
||
|
/**
|
||
|
* Class IOException: erreur sur un flux ou un système de fichiers
|
||
|
*/
|
||
|
class IOException extends RuntimeException {
|
||
|
static final function last_error(?string $prefix=null): ?self {
|
||
|
$error = error_get_last();
|
||
|
if ($error === null) return null;
|
||
|
$message = $error["message"];
|
||
|
if ($prefix) $message = "$prefix: $message";
|
||
|
return new static($message);
|
||
|
}
|
||
|
|
||
|
static final function generic_error(?string $message=null): self {
|
||
|
if ($message === null) $message = "generic error";
|
||
|
return new static($message);
|
||
|
}
|
||
|
|
||
|
static final function error(?string $prefix=null): self {
|
||
|
$exception = self::last_error($prefix);
|
||
|
if ($exception !== null) throw $exception;
|
||
|
else throw self::generic_error($prefix);
|
||
|
}
|
||
|
|
||
|
static final function ensure_valid($value, bool $throw=true, $invalid=false) {
|
||
|
if ($value !== $invalid) return $value;
|
||
|
elseif (!$throw) return null;
|
||
|
else throw self::error();
|
||
|
}
|
||
|
|
||
|
static final function already_closed(): self {
|
||
|
return new static("already closed");
|
||
|
}
|
||
|
|
||
|
static final function ensure_open(bool $closed): void {
|
||
|
if ($closed) throw self::already_closed();
|
||
|
}
|
||
|
}
|