This commit is contained in:
2023-03-30 20:39:00 +08:00
parent bfa39c3669
commit 5c9f92f955
6 changed files with 275 additions and 167 deletions
+3
View File
@@ -5,6 +5,7 @@ namespace Database\Annotation;
use Attribute;
use Database\Base\Getter;
use Kiri\Annotation\AbstractAttribute;
@@ -32,6 +33,8 @@ use Kiri\Annotation\AbstractAttribute;
*/
public function execute(mixed $class, mixed $method = null): bool
{
$keys = \Kiri::getDi()->get(Getter::class);
$keys->write($this->name, $class, $method);
return true;
}
+3 -1
View File
@@ -4,8 +4,8 @@
namespace Database\Annotation;
use Database\Base\Setter;
use Kiri\Annotation\AbstractAttribute;
use Exception;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute
{
@@ -27,6 +27,8 @@ use Exception;
*/
public function execute(mixed $class, mixed $method = null): bool
{
$keys = \Kiri::getDi()->get(Setter::class);
$keys->write($this->name, $class, $method);
return true;
}
+47
View File
@@ -2,7 +2,54 @@
namespace Database\Base;
use Database\ModelInterface;
class Getter
{
private array $getter = [];
/**
* @param string $name
* @param string $className
* @param string $method
* @return void
*/
public function write(string $name, string $className, string $method): void
{
if (!isset($this->getter[$className])) {
$this->getter[$className] = [];
}
$this->getter[$className][$name] = $method;
}
/**
* @param string $className
* @return array
*/
public function getAll(string $className): array
{
return $this->getter[$className] ?? [];
}
/**
* @param string $className
* @param string $name
* @return bool
*/
public function has(string $className, string $name): bool
{
return isset($this->getter[$className]) && isset($this->getter[$className][$name]);
}
public function get(string $className, string $name): ?string
{
if (!$this->has($className,$name)) {
return null;
}
return $this->getter[$className][$name];
}
}
+8 -6
View File
@@ -422,8 +422,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
*/
public function setAttribute($name, $value): mixed
{
if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) {
$value = $this->{'set' . ucfirst($name) . 'Attribute'}($value);
$keys = Kiri::getDi()->get(Setter::class);
if ($keys->has(static::class, $name)) {
$method = $keys->get(static::class, $name);
$value = $this->{$method}($value);
}
return $this->_attributes[$name] = $value;
}
@@ -842,12 +844,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (is_null($value)) {
$value = $this->_attributes[$name] ?? NULL;
}
$method = 'get' . ucfirst($name) . 'Attribute';
if (!method_exists($this, $method)) {
$getter = Kiri::getDi()->get(Getter::class);
if ($getter->has(static::class, $name)) {
return $this->{$getter->get(static::class, $name)}($value);
} else {
return $value;
return $this->_decode($name, $value);
}
return $this->{$method}($value);
}
+52
View File
@@ -5,5 +5,57 @@ namespace Database\Base;
class Setter
{
private array $setter = [];
/**
* @param string $name
* @param string $className
* @param string $method
* @return void
*/
public function write(string $name, string $className, string $method): void
{
if (!isset($this->setter[$className])) {
$this->setter[$className] = [];
}
$this->setter[$className][$name] = $method;
}
/**
* @param string $className
* @param string $name
* @return bool
*/
public function has(string $className, string $name): bool
{
return isset($this->setter[$className]) && isset($this->setter[$className][$name]);
}
/**
* @param string $className
* @return array|null
*/
public function getAll(string $className): ?array
{
return $this->setter[$className] ?? null;
}
/**
* @param string $className
* @param string $name
* @return string|null
*/
public function get(string $className, string $name): ?string
{
if (!$this->has($className, $name)) {
return null;
}
return $this->setter[$className][$name];
}
}
+4 -2
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Database;
use Database\Base\Getter;
use Exception;
use Kiri;
use Kiri\Exception\NotFindClassException;
@@ -264,8 +265,9 @@ class Model extends Base\Model
public function toArray(): array
{
$data = $this->_attributes;
foreach ($data as $key => $datum) {
$data[$key] = $this->withPropertyOverride($key, $datum);
$keys = Kiri::getDi()->get(Getter::class);
foreach ($keys->getAll(static::class) as $key => $datum) {
$data[$key] = $this->{$datum}($data[$key]);
}
return $this->withRelates($data);
}