/** Datei: library/ARUNOX/API/PHP/Attributes/Controller/Notifications/Helper.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Controller\Notifications; use Attribute; use ARUNOX\API\PHP; use ReflectionClass; trait Helper { /* ### VARIABLES ################################################ # */ protected static array $__CACHED_NOTIFIES = []; /* ### PUBLIC STATIC FUNCTIONS ################################## # */ public static function getNotify( string|array $type, null|string $label = NULL ) : string|bool { return static::getNotification( $type, $label ); } public static function getNotification( string|array $type, null|string|array $label = NULL ) : string|bool { $notify = static::resolveNotificationsFromClass( ...(array)$type ); if( $label ) { $label = new Label( ...(array)$label ); $label = $label->getLabel() ?? NULL; } if( $label == NULL ) { $label = static::getLabel(); } if( $label ) { $notify = preg_replace_callback( '/\{\$LABEL\.([a-z]+)\}/', fn( $match ) => $label[ $match[1] ], $notify ); } return $notify; } public static function getNotifications() : array { return static::resolveNotificationsFromClass(); } /* ### PRIVATE STATIC FUNCTIONS ################################# # */ private static function resolveNotificationsFromClass( ...$type ) : string|array { $reflection = new ReflectionClass( static::class ); $attributes = []; $type = implode( ".", $type ); if( !isset( static::$__CACHED_NOTIFIES[ static::class ] ) ) { do { foreach( $reflection->getAttributes( PHP\Attributes\Controller\Notifications::class ) as $attribute ) { $instance = $attribute->newInstance(); if( key_exists( $instance->type, $attributes ) ) continue; $attributes[ $instance->type ] = $instance->message; } $reflection = $reflection->getParentClass(); } while ($reflection); static::$__CACHED_NOTIFIES[ static::class ] = $attributes; } if( $type ) return static::$__CACHED_NOTIFIES[ static::class ][ $type ] ?? "Unbekannter Fehler!"; else return static::$__CACHED_NOTIFIES[ static::class ] ?? []; } } /** Datei: library/ARUNOX/API/PHP/Attributes/Controller/Notifications/Notifications.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Controller; use Attribute; use ARUNOX\API\PHP; #[Attribute( Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE )] class Notifications { use PHP\Attributes\Magic\Get\Helper; /* ### VARIABLES ################################################ # */ /* ### PROTECTED FUNCTIONS ###################################### # */ /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct( protected string $type, protected string $message, protected bool $default = FALSE ){} }