magic methods fixed

This commit is contained in:
2026-03-09 09:10:11 +05:00
parent 393f37577d
commit 6d1f1b79ba

View File

@@ -8,7 +8,8 @@ namespace mirzaev\minimal\traits;
use mirzaev\minimal\http\enumerations\status; use mirzaev\minimal\http\enumerations\status;
// Built-in libraries // Built-in libraries
use exception; use Exception as exception,
DomainException as exception_domain;
/** /**
* Trait of magical methods * Trait of magical methods
@@ -28,6 +29,8 @@ trait magic
/** /**
* Write property * Write property
* *
* @throws exception_domain Not found the proprty
*
* @param string $name Name of the property * @param string $name Name of the property
* @param mixed $value Value of the property * @param mixed $value Value of the property
* *
@@ -35,14 +38,24 @@ trait magic
*/ */
public function __set(string $name, mixed $value = null): void public function __set(string $name, mixed $value = null): void
{ {
match ($name) { if (property_exists(static, $name)) {
default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value) // Exist the property
};
// Writing the property
$this->{$name} = $value;
} else {
// Not exist the property
// Exit (fail)
throw new exception_domain('Not found the property: ' . static::class . "::\$$name", status::not_found->value);
}
} }
/** /**
* Read property * Read property
* *
* @throws exception_domain Not found the property
*
* @param string $name Name of the property * @param string $name Name of the property
* *
* @return mixed Value of the property * @return mixed Value of the property
@@ -50,7 +63,7 @@ trait magic
public function __get(string $name): mixed public function __get(string $name): mixed
{ {
return match ($name) { return match ($name) {
default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value) default => throw new exception_domain('Not found the property: ' . static::class . "::\$$name", status::not_found->value)
}; };
} }