/** Datei: library/ARUNOX/API/PHP/Attributes/Magic/Set/Set.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Magic; use Attribute; use ARUNOX\API\PHP; #[Attribute( Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE )] class Set { use PHP\Attributes\Magic\Get\Helper; /* ### VARIABLES ################################################ # */ /* ### PROTECTED FUNCTIONS ###################################### # */ /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct( protected null|string $match = NULL, protected bool $fallback = FALSE ){} } /** Datei: library/ARUNOX/API/PHP/Attributes/Magic/Set/Helper.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\API\PHP\Attributes\Magic\Set; use ARUNOX\API\PHP; use ReflectionClass; use ReflectionMethod; trait Helper { /* ### VARIABLES ################################################ # */ protected static array $__CACHED_SETTER = []; /* ### PUBLIC STATIC FUNCTIONS ################################## # */ public static function getMagicSetter() : array { $class = static::class; if( !isset( static::$__CACHED_SETTER[ $class ] ) ) { static::$__CACHED_SETTER[ $class ] = static::resolveMagicSettersFromClass(); } return static::$__CACHED_SETTER[ $class ]; } /* ### PRIVATE STATIC FUNCTIONS ################################# # */ private static function resolveMagicSettersFromClass() : array { $ref = new ReflectionClass( static::class ); $result = [ "exact" => [], "pattern" => [], "fallback" => [], ]; foreach( $ref->getMethods(ReflectionMethod::IS_PUBLIC ) as $method ) { foreach( $method->getAttributes(PHP\Attributes\Magic\Set::class ) as $attr ) { $meta = $attr->newInstance(); $target = $method->getName(); if( $meta->fallback || $meta->match === NULL || $meta->match === "*" ) { $result["fallback"][] = $target; continue; } if( @preg_match("/^{$meta->match}$/", "" ) !== FALSE ) { $result["pattern"][] = [ "/^{$meta->match}$/", $target ]; continue; } $result["exact"][ $meta->match ] = $target; } } return $result; } /* ### MAGIC FUNCTIONS ########################################## # */ public function __set( string $name, mixed $value ) : void { $map = self::getMagicSetter(); // 1. exact if( isset( $map["exact"][ $name ] ) ) { // show( "Case 1", $map["exact"], $name, $value, FALSE ); $this->{$map["exact"][ $name ]}( $value ); return; } // 2. pattern foreach( $map["pattern"] as [ $regex, $method ]) { if( preg_match( $regex, $name ) ) { // show( "Case 2", $method, $name, $value, FALSE ); $this->{$method}( $name, $value ); return; } } // 3. fallback foreach( $map["fallback"] as $method ) { // show( "Case 3", $method, $name, $value, FALSE ); $this->{$method}( $name, $value ); return; } // 4. method getXyz() if( method_exists( $this, "set{$name}" ) ) { // show( "Case 4", "set{$name}", $value, FALSE ); $this->{"set{$name}"}( $value ); return; } // 5. property access if( property_exists( $this, $name ) ) { // show( "Case 5", $name, $value, FALSE ); $this->{$name} = $value; return; } throw new \BadMethodCallException("Unbekannte dynamische Eigenschaft: {$name}"); } }