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
+4 -1
View File
@@ -5,6 +5,7 @@ namespace Database\Annotation;
use Attribute; use Attribute;
use Database\Base\Getter;
use Kiri\Annotation\AbstractAttribute; use Kiri\Annotation\AbstractAttribute;
@@ -30,8 +31,10 @@ use Kiri\Annotation\AbstractAttribute;
* @param mixed|null $method * @param mixed|null $method
* @return bool * @return bool
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$keys = \Kiri::getDi()->get(Getter::class);
$keys->write($this->name, $class, $method);
return true; return true;
} }
+3 -1
View File
@@ -4,8 +4,8 @@
namespace Database\Annotation; namespace Database\Annotation;
use Database\Base\Setter;
use Kiri\Annotation\AbstractAttribute; use Kiri\Annotation\AbstractAttribute;
use Exception;
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute #[\Attribute(\Attribute::TARGET_METHOD)] class Set extends AbstractAttribute
{ {
@@ -27,6 +27,8 @@ use Exception;
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
$keys = \Kiri::getDi()->get(Setter::class);
$keys->write($this->name, $class, $method);
return true; return true;
} }
+47
View File
@@ -2,7 +2,54 @@
namespace Database\Base; namespace Database\Base;
use Database\ModelInterface;
class Getter 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 public function setAttribute($name, $value): mixed
{ {
if (method_exists($this, 'set' . ucfirst($name) . 'Attribute')) { $keys = Kiri::getDi()->get(Setter::class);
$value = $this->{'set' . ucfirst($name) . 'Attribute'}($value); if ($keys->has(static::class, $name)) {
$method = $keys->get(static::class, $name);
$value = $this->{$method}($value);
} }
return $this->_attributes[$name] = $value; return $this->_attributes[$name] = $value;
} }
@@ -842,12 +844,12 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
if (is_null($value)) { if (is_null($value)) {
$value = $this->_attributes[$name] ?? NULL; $value = $this->_attributes[$name] ?? NULL;
} }
$method = 'get' . ucfirst($name) . 'Attribute'; $getter = Kiri::getDi()->get(Getter::class);
if (!method_exists($this, $method)) { if ($getter->has(static::class, $name)) {
return $this->{$getter->get(static::class, $name)}($value);
} else {
return $value; return $value;
return $this->_decode($name, $value);
} }
return $this->{$method}($value);
} }
+52
View File
@@ -5,5 +5,57 @@ namespace Database\Base;
class Setter 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; namespace Database;
use Database\Base\Getter;
use Exception; use Exception;
use Kiri; use Kiri;
use Kiri\Exception\NotFindClassException; use Kiri\Exception\NotFindClassException;
@@ -264,8 +265,9 @@ class Model extends Base\Model
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
foreach ($data as $key => $datum) { $keys = Kiri::getDi()->get(Getter::class);
$data[$key] = $this->withPropertyOverride($key, $datum); foreach ($keys->getAll(static::class) as $key => $datum) {
$data[$key] = $this->{$datum}($data[$key]);
} }
return $this->withRelates($data); return $this->withRelates($data);
} }