95 lines
3.1 KiB
PHP
95 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Platform;
|
|
|
|
use App\Model\Model as Entity;
|
|
use App\Model\Company;
|
|
use App\Model\Platform;
|
|
use App\Model\Store;
|
|
use Hyperf\Amqp\Message\ConsumerMessageInterface;
|
|
use Psr\Log\InvalidArgumentException;
|
|
use Hyperf\Stringable\Str;
|
|
|
|
abstract class EntityParse implements EntityParseInterface
|
|
{
|
|
private ?ConsumerMessageInterface $message = null;
|
|
private ?Platform $platform = null;
|
|
private ?Company $company = null;
|
|
private ?Store $store = null;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->platform = $this->platformScopeMatch($this->message);
|
|
$this->company = $this->companyScopeMatch($this->message);
|
|
$this->store = $this->storeScopeMatch($this->message);
|
|
|
|
if(!$this->platform instanceof Platform || !isset($this->platform->id)){
|
|
throw new InvalidArgumentException('platform is undefined');
|
|
}
|
|
|
|
if(!$this->company instanceof Platform || !isset($this->company->id)){
|
|
throw new InvalidArgumentException('company is undefined');
|
|
}
|
|
|
|
if(!$this->store instanceof Platform || !isset($this->store->id)){
|
|
throw new InvalidArgumentException('store is undefined');
|
|
}
|
|
}
|
|
|
|
public function companyScopeMatch(ConsumerMessageInterface $message): Company
|
|
{
|
|
// TODO: Implement companyScopeMatch() method.
|
|
}
|
|
|
|
public function platformScopeMatch(ConsumerMessageInterface $message): Platform
|
|
{
|
|
$platformName = Str::of($message->getExchange())->before('.')->ucfirst()->toString();
|
|
|
|
$platform = Platform::where('name','=',$platformName)->first();
|
|
|
|
// 确保实例化的对象是 Entity 类型
|
|
if (!$platform) {
|
|
throw new InvalidArgumentException("PLatform name {$platformName} not exist!");
|
|
}
|
|
|
|
return $platform;
|
|
|
|
}
|
|
|
|
public function storeScopeMatch(ConsumerMessageInterface $message): Store
|
|
{
|
|
// TODO: Implement storeScopeMatch() method.
|
|
}
|
|
|
|
public function entityMatch(ConsumerMessageInterface $message): Entity
|
|
{
|
|
// 从路由键中提取实体名称,例如 'order.tmall' -> 'Order'
|
|
$entityName = Str::of($message->getRoutingKey())->before('.')->ucfirst()->toString();
|
|
|
|
// 构建完整的类名
|
|
$entityClass = "App\\Model\\{$entityName}";
|
|
|
|
// 检查类是否存在
|
|
if (!class_exists($entityClass)) {
|
|
throw new InvalidArgumentException("Entity class {$entityClass} does not exist");
|
|
}
|
|
|
|
// 实例化实体对象
|
|
$entity = new $entityClass();
|
|
|
|
// 确保实例化的对象是 Entity 类型
|
|
if (!$entity instanceof Entity) {
|
|
throw new InvalidArgumentException("Class {$entityClass} is not an instance of Entity");
|
|
}
|
|
|
|
return $entity;
|
|
}
|
|
|
|
public function entityMap(array $data, Entity $entity): Entity
|
|
{
|
|
// TODO: Implement entityMap() method.
|
|
// entityMap 方法应该返回一个 属性已被更新设置的数据模型,而非默认空状态的 Entity
|
|
|
|
return $this->entityMatch($this->message);
|
|
}
|
|
} |