/** Datei: library/ARUNOX/Core/Errors/Errors.php **/ * @copyright 2025 ARUNOX * @version 1.0.0 * @since 1.0.0 File is created */ namespace ARUNOX\Core; class Errors { /* ### CONSTANTS ################################################ # */ /* ### VARIABLES ################################################ # */ protected static array $ERRORS = []; protected static array $CODES = [ "100" => "Continue", "200" => "OK", "208" => "Already Reported", "304" => "Not Modified", "400" => "Bad Request", "401" => "Unauthorized", "402" => "Payment Required", "403" => "Forbidden", "404" => "Not Found", "405" => "Method Not Allowed", "409" => "Conflict", "500" => "Internal Server Error", ]; /* ################################################################ # */ /* ### PUBLIC FUNCTIONS ######################################### # */ /* ################################################################ # */ public static function init() : void { if( !key_exists( "errors", $_SESSION ) ) { $_SESSION["errors"] = []; } static::$ERRORS = &$_SESSION["errors"]; } public static function reset( int $index = -1 ) : void { $index = static::getIndex( $index ); unset( static::$ERRORS[ $index ] ); } public static function resetAll() : void { static::$ERRORS = []; } public static function get( bool $reset = TRUE, int $index = -1 ) : array { $index = static::getIndex( $index ); $error = static::$ERRORS[ $index ]; if( $reset ) static::reset( $index ); return $error; } public static function getAll( bool $reset = TRUE ) : array { $errors = static::$ERRORS; if( $reset ) static::resetAll(); return $errors; } public static function getIndex( int $index = -1 ) : int { $errors = array_keys( static::$ERRORS ); $errors = array_slice( $errors, $index, 1 ); return current( $errors ); } public static function has( bool|string $includes = "all" ) : bool { if( $includes === "all" ) return !!static::$ERRORS; if( $includes === TRUE ) return !!array_filter( static::$ERRORS, fn( $item ) => $item["code"] < 300 ); if( $includes === FALSE ) return !!array_filter( static::$ERRORS, fn( $item ) => $item["code"] >= 300 ); if( is_numeric( $includes ) ) return !!array_filter( static::$ERRORS, fn( $item ) => $item["code"] == $includes ); if( preg_match( '/([0-9])([0-9]{2})(s|\+|-)/', $includes, $matches ) ) { if( $matches[3] == "s" ) $range = [ "start" => "{$matches[1]}00", "end" => "{$matches[1]}99" ]; if( $matches[3] == "+" ) $range = [ "start" => "{$matches[1]}00", "end" => "999" ]; if( isset( $range ) ) return !!array_filter( static::$ERRORS, fn( $item ) => $item["code"] >= $range["start"] && $item["code"] <= $range["end"] ); } return FALSE; } public static function add( int $code, null|string $message = NULL, null|string $title = NULL, null|string $icon = NULL, null|string $color = NULL ) { static::$ERRORS[] = static::parse(...[ "code" => $code, "message" => $message, "title" => $title, "icon" => $icon, "color" => $color, ]); } /* ### PARSING FUNCTIONS ####################################### # */ public static function parseCode( int $code, null|string $value = NULL ) { if( $code >= 100 ) $info = [ "color" => "primary", "beep" => "short", "title" => __( "Information" ), "icon" => "bi bi-info-lg" ]; if( $code >= 200 ) $info = [ "color" => "success", "beep" => "short", "title" => __( "Erfolg" ), "icon" => "bi bi-check-lg" ]; if( $code >= 300 ) $info = [ "color" => "warning", "beep" => "alert", "title" => __( "Warnung" ), "icon" => "bi bi-exclamation-lg" ]; if( $code >= 400 ) $info = [ "color" => "danger", "beep" => "alert", "title" => __( "Fehler" ), "icon" => "bi bi-shield-exclamation" ]; if( $code >= 500 ) $info = [ "color" => "danger", "beep" => "long", "title" => __( "Unbekannter Fehler" ), "icon" => "bi bi-exclamation-diamond" ]; $info["code"] = $code; $info["origin"] = static::$CODES[ $code ] ?? NULL; if( $value ) return $info[ $value ] ?? NULL; return $info; } /* ################################################################ # */ /* ### PROTECTED FUNCTIONS ###################################### # */ /* ################################################################ # */ protected static function parse( int $code, null|string $message = NULL, null|string $title = NULL, null|string $icon = NULL, null|string $color = NULL ) { return [ "code" => $code, "message" => __( $message ), "title" => __( $title ?? static::parseCode( $code, "title" )), "icon" => $icon, "color" => $color ?? static::parseCode( $code, "color" ), ]; } }