76 lines
1.4 KiB
PHP
76 lines
1.4 KiB
PHP
<?php
|
|
|
|
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]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @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)) {
|
|
return null;
|
|
}
|
|
return $this->getter[$className][$name];
|
|
}
|
|
|
|
}
|