/** Datei: plugins/rest/library/ARUNOX/Plugins/REST/Response/Response.php **/ * @copyright 2025 ARUNOX * @version 1.0.0 * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\REST; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Core\Errors; use ARUNOX\Database\BaseEntity; use ARUNOX\Plugins\REST\Notifications; use ARUNOX\Plugins\REST\Wrapper\Modal; use ARUNOX\Plugins\REST\Notifications\Type; class Response { /* ### VARIABLES ################################################ # */ protected static array $RESPONSE = []; /* ### PUBLIC FUNCTIONS ######################################### # */ public static function setTemplate( string $name ) { static::$RESPONSE = array_filter( static::$RESPONSE, function( $item ) { return $item->type != "template"; } ); return static::add([ "type" => "template", "template" => $name, "title" => Hooks::filter( 'template::pagetitle', NULL ), "copyright" => "2025 ARUNOX", ]); } /* ### PUBLIC ADD FUNCTIONS ##################################### # */ public static function add( mixed $item ) { $item = (object)$item; return static::$RESPONSE[] = Hooks::filterWithNames( 'rest::response->add', $item->type, $item ); } public static function addUpdater( array $uuid, mixed $data = [], bool $status = TRUE ) { return static::add([ "type" => "updater", "updater" => preg_replace('/[^a-zA-Z0-9]/', '', base64_encode( implode(":", $uuid ) ) ), "status" => $status, "data" => Hooks::filter( 'rest::response->data', $data ), ]); } public static function addLink( string $url, string $rel = "" ) { return static::add([ "type" => "generator", "generator" => "header/link", "data" => [ "href" => $url, "rel" => $rel, ], ]); } public static function addClick( array $uuid, null|string $selector = NULL ) { return static::add([ "type" => "click", "parent" => base64_encode( implode(":", $uuid ) ), "selector" => $selector ? "[data-key^='{$selector}']" : NULL, ]); } public static function addWrapper( string $name, ...$args ) { if( !static::hasTemplate() ) { static::setTemplate( "default" ); } $name = ucfirst( $name ); $class = "ARUNOX\\Plugins\\REST\\Wrapper\\{$name}"; $object = $class::create( ...$args ); return static::add( $object ); } public static function addModal( string $name, ...$args ) { $name = strtolower( $name ); $object = Modal::{$name}( ...$args ); $object->setId( base64_encode( REQUEST_ROUTE ) ); return static::add( $object ); } public static function addNotify( string|Type $type, string $message, ?string $icon = NULL, ?int $duration = NULL, ?string $title = NULL, ...$args) { $notify = Notifications::create( $type, $message, $icon, $duration, $title ); foreach( static::$RESPONSE as &$response ) { if( is_a( $response, Notifications::class ) ) { $response = $notify->top(); return $notify; } } static::add( $notify->top() ); return $notify; } /* ### PUBLIC GET FUNCTIONS ##################################### # */ public static function get( string $type, ?string $key = NULL, ?string $value = NULL ) { return array_filter( static::$RESPONSE, function( $item ) use( $type, $key, $value ) { if( $item->type != $type ) return FALSE; if( $key && $value ) { return $item->{$key} == $value; } return TRUE; }); } public static function getTemplate( ?string $name = NULL ) { return static::get( "template", "template", $name ); } public static function getUpdater() { return static::get( "updater" ); } public static function getRoute() { return !!static::get( "route" ); } public static function getWrapper() { return static::get( "wrapper" ); } public static function getGenerator() { return static::get( "generator" ); } /* ### PUBLIC HAS FUNCTIONS ##################################### # */ public static function hasTemplate( ?string $name = NULL ) { return !!static::get( "template", "template", $name ); } public static function hasRoute() { return !!static::get( "route" ); } public static function hasUpdater() { return !!static::get( "updater" ); } public static function hasWrapper() { return !!static::get( "wrapper" ); } public static function hasGenerator() { return !!static::get( "generator" ); } /* ### PUBLIC FUNCTIONS ######################################### # */ public static function closeModal( string $name ){ return static::add( Modal::close( base64_encode( $name ) ) ); } public static function closeModals(){ return static::add( Modal::close("#ALL") ); } public static function redirect( string $route ){ return static::add([ "type" => "route", "route" => $route ]); } public static function refresh( string $hash = "" ){ return static::redirect( "/" . HTTP\Request::referer("path") . "#" . preg_replace( "/^#/", "", $hash ) ); } /* ### PUBLIC RESPONSE FUNCTIONS ################################ # */ public static function &response() : array { // if( !static::hasTemplate() ) { // static::setTemplate( "default" ); // } // if( Errors::has() ) { // foreach( Errors::getAll() as $error ) { // static::addNotify(...[ // "type" => $error["color"], // "message" => $error["message"], // "title" => $error["title"], // "icon" => $error["icon"], // ]); // } // } $template = NULL; $content = []; $generator = []; foreach( static::$RESPONSE as $item ) { if( $item->type == "template" ) $template = $item; else if( $item->type == "wrapper" ) $content[] = $item; else $generator[] = $item; } if( $template ) { $template->content = $content; $generator[] = $template; } else $generator = array_merge( $generator, $content ); // show( static::$RESPONSE, "STOP" ); return $generator; show( $generator, static::$RESPONSE, FALSE ); return static::$RESPONSE; } public static function json() : string { // show( json_encode( static::response() ), json_last_error(), json_last_error_msg(), static::response() ); return json_encode( static::response() ); } public static function jsonResponse( int $status = 200, ?array $data = NULL ) : HTTP\Response { $data = Hooks::filter( 'response::data', $data ); return new HTTP\Response( $status, [ "Content-Type" => "application/json" ], json_encode( $data ?? static::response() ) ); } public static function autoResponse( int $status = 200, ?string $message = NULL, ?array $data = NULL, null|string $title = "Serverantwort" ) : HTTP\Response { $data = Hooks::filter( 'response::data', $data ); // if( $status !== 200 || $message ) Errors::add( $status, $message, $title ); // $json = $data ?? static::response(); if( $status !== 200 ) $json = array_merge( [ "code" => $status, "message" => $message ?? static::$ERRORMSG[ $status ], "title" => $title ], $data ?? [] ); else if( $message ) $json = array_merge( [ "code" => $status, "message" => $message, "title" => $title ], $data ?? [] ); else $json = $data ?? static::response(); return new HTTP\Response( $status, [ "Content-Type" => "application/json" ], json_encode( $json ) ); } /* ### PRIVATE FUNCTIONS ######################################## # */ /* ### MAGIC FUNCTIONS ########################################## # */ }