This commit is contained in:
Jephté Clain 2024-04-24 23:16:47 +04:00
parent 52631cc977
commit 3550730a9f
3 changed files with 13 additions and 4 deletions

View File

@ -141,6 +141,15 @@ class cl {
return $array !== null? array_keys($array): []; return $array !== null? array_keys($array): [];
} }
/**
* retourner la première valeur de $array ou $default si le tableau est null
* ou vide
*/
static final function first($array, $default=null) {
if (is_array($array)) return $array[array_key_first($array)];
return $default;
}
############################################################################# #############################################################################
/** /**

View File

@ -7,8 +7,8 @@ namespace nur\sery\os;
*/ */
class EOFException extends IOException { class EOFException extends IOException {
static final function ensure_not_eof($data, bool $throw=true, $eof=false) { static final function ensure_not_eof($data, bool $throw=true, $eof=false) {
if (!$throw) return null; if ($data !== $eof) return $data;
elseif ($data !== $eof) return $data; elseif (!$throw) return null;
else throw new self(); else throw new self();
} }
} }

View File

@ -27,8 +27,8 @@ class IOException extends RuntimeException {
} }
static final function ensure_valid($value, bool $throw=true, $invalid=false) { static final function ensure_valid($value, bool $throw=true, $invalid=false) {
if (!$throw) return null; if ($value !== $invalid) return $value;
elseif ($value !== $invalid) return $value; elseif (!$throw) return null;
else throw self::error(); else throw self::error();
} }