/** Datei: library/ARUNOX/API/PHP/Attributes/Database/Attributes/Resolve/Helper.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Database\Attributes\Resolve; use ARUNOX\API\PHP; use ReflectionClass; use ARUNOX\Interfaces\Supports; trait Helper { /* ### VARIABLES ################################################ # */ protected static array $__CACHED_RESOLVES = []; /* ### PUBLIC FUNCTIONS ######################################### # */ #[PHP\Attributes\Magic\Get( fallback: TRUE, priority: 5 )] public function handleGetResolve( string $column ) { if( static::existsResolve( $column ) ) { $resolves = static::getResolve( $column ); $result = NULL; foreach( $resolves as $resolve ) { if( preg_match( '/\$\{[a-z]+\}/', $resolve ) ) { $result = preg_replace_callback( '/\$\{([a-z]+)\}/', fn( $m ) => $this->attributes[ $m[1] ], $resolve ); } else if( is_string( $resolve ) ) $result = $this->attributes[ $resolve ]; if( preg_replace( "/\s+/", "", $result ) != NULL ) return $result; } return ""; } throw new PHP\Attributes\Magic\Get\Exceptions\FallbackFailed(); } /* ### PUBLIC STATIC FUNCTIONS ################################## # */ public static function existsResolve( string $column ) : bool { $resolves = static::getResolves(); return isset( $resolves[ $column ] ); } public static function getResolve( string $column ) : array { return static::resolveResolvesFromClass( $column ); } public static function getResolves() : array { return static::resolveResolvesFromClass(); } /* ### PRIVATE STATIC FUNCTIONS ################################# # */ private static function resolveResolvesFromClass( null|string $column = NULL ) : string|array { $reflection = new ReflectionClass( static::class ); $attributes = []; if( !isset( static::$__CACHED_RESOLVES[ static::class ] ) ) { do { foreach( $reflection->getAttributes( PHP\Attributes\Database\Attributes\Resolve::class ) as $attribute ) { $instance = $attribute->newInstance(); if( !empty( $attributes ) ) break 2; $attributes[ $instance->column ] = $instance->resolves; } $reflection = $reflection->getParentClass(); } while ($reflection); static::$__CACHED_RESOLVES[ static::class ] = $attributes; } if( $column ) { return static::$__CACHED_RESOLVES[ static::class ][ $column ] ?? ""; } return static::$__CACHED_RESOLVES[ static::class ] ?? ""; } } /** Datei: library/ARUNOX/API/PHP/Attributes/Database/Attributes/Resolve/Resolve.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Database\Attributes; use Attribute; use ARUNOX\API\PHP; #[Attribute( Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE )] class Resolve { use PHP\Attributes\Magic\Get\Helper; /* ### VARIABLES ################################################ # */ protected array $resolves = []; /* ### PROTECTED FUNCTIONS ###################################### # */ /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct( protected string $column, ...$resolves ){ $this->resolves = (array)$resolves; } }