This commit is contained in:
2023-04-05 10:15:50 +08:00
parent bc3c286416
commit c86eb1a83d
3 changed files with 207 additions and 173 deletions
+20
View File
@@ -44,6 +44,26 @@ class Getter
} }
/**
* @param ModelInterface $class
* @param string $key
* @param mixed $value
* @return mixed
*/
public function override(ModelInterface $class, string $key, mixed $value): mixed
{
$method = $this->getter[$class::class][$key] ?? null;
if ($method !== null) {
return $class->{$method}($value);
}
return $value;
}
/**
* @param string $className
* @param string $name
* @return string|null
*/
public function get(string $className, string $name): ?string public function get(string $className, string $name): ?string
{ {
if (!$this->has($className,$name)) { if (!$this->has($className,$name)) {
+11 -16
View File
@@ -100,15 +100,16 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @var array * @var Getter
*/ */
protected array $overrideGetter = []; #[Inject(Getter::class, [self::class])]
protected Getter $overrideGetter;
/** /**
* @var array * @var Setter
*/ */
#[Inject(Setter::class, [self::class])] #[Inject(Setter::class, [self::class])]
protected array $overrideSetter = []; protected Setter $overrideSetter;
/** /**
@@ -827,12 +828,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
$method = 'set' . ucfirst($name); $method = 'set' . ucfirst($name);
if (method_exists($this, $method)) { if (method_exists($this, $method)) {
$this->{$method}($value); $this->{$method}($value);
return;
}
$method = $method . 'Attribute';
if (method_exists($this, $method)) {
$this->_attributes[$name] = $this->{$method}($value);
} else { } else {
$value = $this->overrideSetter->override($this, $name, $value);
$this->_attributes[$name] = $value; $this->_attributes[$name] = $value;
} }
} }
@@ -847,9 +845,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
{ {
if (isset($this->_attributes[$name])) { if (isset($this->_attributes[$name])) {
return $this->withPropertyOverride($name); return $this->withPropertyOverride($name);
} } else {
return $this->getRelateValue($name); return $this->getRelateValue($name);
} }
}
/** /**
@@ -863,12 +862,8 @@ 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;
} }
$getter = Kiri::getDi()->get(Getter::class);
if ($getter->has(static::class, $name)) { return $this->overrideGetter->override($this, $name, $value);
return $this->{$getter->get(static::class, $name)}($value);
} else {
return $value;
}
} }
+19
View File
@@ -2,6 +2,8 @@
namespace Database\Base; namespace Database\Base;
use Database\ModelInterface;
class Setter class Setter
{ {
@@ -44,6 +46,23 @@ class Setter
} }
/**
* @param ModelInterface $class
* @param string $key
* @param mixed $value
* @return mixed
*/
public function override(ModelInterface $class, string $key, mixed $value): mixed
{
$method = $this->setter[$class::class][$key] ?? null;
if ($method !== null) {
return $class->{$method}($value);
}
return $value;
}
/** /**
* @param string $className * @param string $className
* @param string $name * @param string $name