变更
This commit is contained in:
@@ -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
|
||||
{
|
||||
if (!$this->has($className,$name)) {
|
||||
|
||||
+11
-16
@@ -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])]
|
||||
protected array $overrideSetter = [];
|
||||
protected Setter $overrideSetter;
|
||||
|
||||
|
||||
/**
|
||||
@@ -827,12 +828,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
||||
$method = 'set' . ucfirst($name);
|
||||
if (method_exists($this, $method)) {
|
||||
$this->{$method}($value);
|
||||
return;
|
||||
}
|
||||
$method = $method . 'Attribute';
|
||||
if (method_exists($this, $method)) {
|
||||
$this->_attributes[$name] = $this->{$method}($value);
|
||||
} else {
|
||||
$value = $this->overrideSetter->override($this, $name, $value);
|
||||
|
||||
$this->_attributes[$name] = $value;
|
||||
}
|
||||
}
|
||||
@@ -847,9 +845,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
||||
{
|
||||
if (isset($this->_attributes[$name])) {
|
||||
return $this->withPropertyOverride($name);
|
||||
}
|
||||
} else {
|
||||
return $this->getRelateValue($name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -863,12 +862,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
|
||||
if (is_null($value)) {
|
||||
$value = $this->_attributes[$name] ?? NULL;
|
||||
}
|
||||
$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->overrideGetter->override($this, $name, $value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Database\Base;
|
||||
|
||||
use Database\ModelInterface;
|
||||
|
||||
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 $name
|
||||
|
||||
Reference in New Issue
Block a user