update
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Model;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \App_Model_Platform
|
||||||
|
*/
|
||||||
|
class Platform extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*
|
||||||
|
* @TODO check if we realy need the platform table, or consider use a virtual model
|
||||||
|
*/
|
||||||
|
# protected ?string $table = 'Platform';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*/
|
||||||
|
protected array $fillable = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast to native types.
|
||||||
|
*/
|
||||||
|
protected array $casts = [];
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Platform;
|
||||||
|
|
||||||
|
use Hyperf\Amqp\Message\ConsumerMessageInterface;
|
||||||
|
use App\Model\Company;
|
||||||
|
use App\Model\Platform;
|
||||||
|
use App\Model\Store;
|
||||||
|
use App\Model\Model as Entity;
|
||||||
|
|
||||||
|
interface EntityParseInterface
|
||||||
|
{
|
||||||
|
public function companyScopeMatch(ConsumerMessageInterface $message) : Company;
|
||||||
|
|
||||||
|
public function platformScopeMatch(ConsumerMessageInterface $message) : Platform;
|
||||||
|
|
||||||
|
public function storeScopeMatch(ConsumerMessageInterface $message) : Store;
|
||||||
|
|
||||||
|
public function entityMatch(ConsumerMessageInterface $message) : Entity;
|
||||||
|
|
||||||
|
public function entityMap(array $data, Entity $entity): Entity;
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.1",
|
"php": ">=8.1",
|
||||||
"composer": "*",
|
"composer": "^2.7",
|
||||||
"96qbhy/hyperf-auth": "^3.1",
|
"96qbhy/hyperf-auth": "^3.1",
|
||||||
"casbin/casbin": "^4.0",
|
"casbin/casbin": "^4.0",
|
||||||
"hyperf/amqp": "^3.1",
|
"hyperf/amqp": "^3.1",
|
||||||
|
|||||||
Reference in New Issue
Block a user