refactor all base things

This commit is contained in:
crazywhalecc
2022-08-13 17:00:29 +08:00
committed by Jerry Ma
parent 1c801bb205
commit b2c95d96b1
54 changed files with 3009 additions and 383 deletions

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\OneBot;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\Level;
use ZM\Exception\InvalidArgumentException;
use ZM\Exception\ZMKnownException;
/**
* Class BotCommand
* 机器人指令注解
*
* @Annotation
* @NamedArgumentConstructor()
* @Target("METHOD")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class BotCommand extends AnnotationBase implements Level
{
/** @var string */
public $name = '';
/** @var string */
public $match = '';
/** @var string */
public $pattern = '';
/** @var string */
public $regex = '';
/** @var string */
public $start_with = '';
/** @var string */
public $end_with = '';
/** @var string */
public $keyword = '';
/** @var string[] */
public $alias = [];
/** @var string */
public $message_type = '';
/** @var string */
public $user_id = '';
/** @var string */
public $group_id = '';
/** @var int */
public $level = 20;
/** @var array */
private $arguments = [];
public function __construct(
$name = '',
$match = '',
$pattern = '',
$regex = '',
$start_with = '',
$end_with = '',
$keyword = '',
$alias = [],
$message_type = '',
$user_id = '',
$group_id = '',
$level = 20
) {
$this->name = $name;
$this->match = $match;
$this->pattern = $pattern;
$this->regex = $regex;
$this->start_with = $start_with;
$this->end_with = $end_with;
$this->keyword = $keyword;
$this->alias = $alias;
$this->message_type = $message_type;
$this->user_id = $user_id;
$this->group_id = $group_id;
$this->level = $level;
}
public static function make(
$name = '',
$match = '',
$pattern = '',
$regex = '',
$start_with = '',
$end_with = '',
$keyword = '',
$alias = [],
$message_type = '',
$user_id = '',
$group_id = '',
$level = 20
): BotCommand {
return new static(...func_get_args());
}
/**
* @throws InvalidArgumentException
* @throws ZMKnownException
* @return $this
*/
public function withArgument(
string $name,
string $description = '',
string $type = 'string',
bool $required = false,
string $prompt = '',
string $default = '',
int $timeout = 60,
int $error_prompt_policy = 1
): BotCommand {
$this->arguments[] = new CommandArgument($name, $description, $type, $required, $prompt, $default, $timeout, $error_prompt_policy);
return $this;
}
public function getLevel(): int
{
return $this->level;
}
/**
* @param int $level
*/
public function setLevel($level)
{
$this->level = $level;
}
public function getArguments(): array
{
return $this->arguments;
}
}

View File

@@ -10,12 +10,14 @@ use Doctrine\Common\Annotations\Annotation\Target;
use ZM\Annotation\AnnotationBase;
/**
* 机器人相关事件注解
*
* @Annotation
* @Target("METHOD")
* @NamedArgumentConstructor
* @NamedArgumentConstructor()
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
class OnOneBotEvent extends AnnotationBase
class BotEvent extends AnnotationBase
{
/** @var null|string */
public $type;
@@ -50,4 +52,15 @@ class OnOneBotEvent extends AnnotationBase
$this->self_id = $self_id;
$this->sub_type = $sub_type;
}
public static function make(
?string $type = null,
?string $detail_type = null,
?string $impl = null,
?string $platform = null,
?string $self_id = null,
?string $sub_type = null
): BotEvent {
return new static(...func_get_args());
}
}

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace ZM\Annotation\OneBot;
use Attribute;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use ZM\Annotation\AnnotationBase;
use ZM\Annotation\Interfaces\ErgodicAnnotation;
use ZM\Exception\InvalidArgumentException;
use ZM\Exception\ZMKnownException;
/**
* Class CommandArgument
* @Annotation
* @NamedArgumentConstructor()
* @Target("ALL")
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_ALL)]
class CommandArgument extends AnnotationBase implements ErgodicAnnotation
{
/**
* @var string
* @Required()
*/
public $name;
/**
* @var string
*/
public $description = '';
/**
* @var string
*/
public $type = 'string';
/**
* @var bool
*/
public $required = false;
/**
* @var string
*/
public $prompt = '';
/**
* @var string
*/
public $default = '';
/**
* @var int
*/
public $timeout = 60;
/**
* @var int
*/
public $error_prompt_policy = 1;
/**
* @param string $name 参数名称(可以是中文)
* @param string $description 参数描述(默认为空)
* @param bool $required 参数是否必需如果是必需为true默认为false
* @param string $prompt 当参数为必需时,返回给用户的提示输入的消息(默认为"请输入$name"
* @param string $default 当required为false时未匹配到参数将自动使用default值默认为空
* @param int $timeout prompt超时时间默认为60秒
* @throws InvalidArgumentException|ZMKnownException
*/
public function __construct(
string $name,
string $description = '',
string $type = 'string',
bool $required = false,
string $prompt = '',
string $default = '',
int $timeout = 60,
int $error_prompt_policy = 1
) {
$this->name = $name;
$this->description = $description;
$this->type = $this->fixTypeName($type);
$this->required = $required;
$this->prompt = $prompt;
$this->default = $default;
$this->timeout = $timeout;
$this->error_prompt_policy = $error_prompt_policy;
if ($this->type === 'bool') {
if ($this->default === '') {
$this->default = 'yes';
}
if (!in_array($this->default, array_merge(TRUE_LIST, FALSE_LIST))) {
throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为布尔型,检测到非法的默认值 ' . $this->default);
}
} elseif ($this->type === 'number') {
if ($this->default === '') {
$this->default = '0';
}
if (!is_numeric($this->default)) {
throw new InvalidArgumentException('CommandArgument参数 ' . $name . ' 类型传入类型应为数字型,检测到非法的默认值 ' . $this->default);
}
}
}
public function getTypeErrorPrompt(): string
{
return '参数类型错误,请重新输入!';
}
public function getErrorQuitPrompt(): string
{
return '参数类型错误,停止输入!';
}
/**
* @throws ZMKnownException
*/
protected function fixTypeName(string $type): string
{
$table = [
'str' => 'string',
'string' => 'string',
'strings' => 'string',
'byte' => 'string',
'num' => 'number',
'number' => 'number',
'int' => 'number',
'float' => 'number',
'double' => 'number',
'boolean' => 'bool',
'bool' => 'bool',
'true' => 'bool',
'any' => 'any',
'all' => 'any',
'*' => 'any',
];
if (array_key_exists($type, $table)) {
return $table[$type];
}
throw new ZMKnownException(zm_internal_errcode('E00077') . 'Invalid argument type: ' . $type . ', only support any, string, number and bool !');
}
}