f2ff4ae123
- 将 FLOW_USE_CUSTOM_PROCESS 从 true 改为 false,禁用自定义流程 - 在 BlockTest 测试用例中改用 setBlockMode 方法设置阻断模式 - 设置统一的错误处理,将错误转为异常抛出 - 重命名 BlockTest 测试文件路径,优化测试组织结构 - 更新 IDE php include paths,调整依赖包引用顺序 - 删除无用的 tests/flow/Test.php 测试文件 - 微调 start.php、webman、windows.php 配置或代码模块
157 lines
3.8 KiB
PHP
157 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace app\flow\context\bean;
|
||
|
||
use app\flow\enum\VoiceMessage;
|
||
|
||
/**
|
||
* 语音状态值对象
|
||
* 封装语音播报相关状态(不可变)
|
||
*/
|
||
readonly class VoiceState
|
||
{
|
||
public function __construct(
|
||
/** 语音播报内容 */
|
||
public string $message = '',
|
||
/** 错误消息枚举 */
|
||
public VoiceMessage $errorMessage = VoiceMessage::NONE,
|
||
/** 期望下一步提示 */
|
||
public VoiceMessage $expectedNextStep = VoiceMessage::NONE,
|
||
/** 语音模板参数 */
|
||
public array $templateParams = [],
|
||
) {}
|
||
|
||
/**
|
||
* 创建空语音状态
|
||
*/
|
||
public static function empty(): self
|
||
{
|
||
return new self();
|
||
}
|
||
|
||
/**
|
||
* 从消息创建
|
||
*/
|
||
public static function fromMessage(string|VoiceMessage $message): self
|
||
{
|
||
if ($message instanceof VoiceMessage) {
|
||
return new self(message: $message->value);
|
||
}
|
||
return new self(message: $message);
|
||
}
|
||
|
||
/**
|
||
* 从错误枚举创建
|
||
*/
|
||
public static function fromError(VoiceMessage $error): self
|
||
{
|
||
return new self(errorMessage: $error);
|
||
}
|
||
|
||
/**
|
||
* 设置语音消息
|
||
*/
|
||
public function withMessage(string|VoiceMessage $message): self
|
||
{
|
||
$msg = $message instanceof VoiceMessage ? $message->value : $message;
|
||
return new self(
|
||
message: $msg,
|
||
errorMessage: $this->errorMessage,
|
||
expectedNextStep: $this->expectedNextStep,
|
||
templateParams: $this->templateParams
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 设置错误消息
|
||
*/
|
||
public function withError(VoiceMessage $error): self
|
||
{
|
||
return new self(
|
||
message: $this->message,
|
||
errorMessage: $error,
|
||
expectedNextStep: $this->expectedNextStep,
|
||
templateParams: $this->templateParams
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 设置期望下一步
|
||
*/
|
||
public function withExpectedNextStep(VoiceMessage $expected): self
|
||
{
|
||
return new self(
|
||
message: $this->message,
|
||
errorMessage: $this->errorMessage,
|
||
expectedNextStep: $expected,
|
||
templateParams: $this->templateParams
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 添加语音前缀
|
||
*/
|
||
public function prependMessage(string $prefix): self
|
||
{
|
||
return new self(
|
||
message: $prefix . $this->message,
|
||
errorMessage: $this->errorMessage,
|
||
expectedNextStep: $this->expectedNextStep,
|
||
templateParams: $this->templateParams
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 设置模板参数
|
||
*/
|
||
public function withTemplateParams(array $params): self
|
||
{
|
||
return new self(
|
||
message: $this->message,
|
||
errorMessage: $this->errorMessage,
|
||
expectedNextStep: $this->expectedNextStep,
|
||
templateParams: $params
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 获取完整语音(优先返回 message,否则返回 errorMessage)
|
||
*/
|
||
public function getFullVoice(): string
|
||
{
|
||
if (!empty($this->message)) {
|
||
return $this->message;
|
||
}
|
||
return $this->errorMessage->value;
|
||
}
|
||
|
||
/**
|
||
* 是否有错误
|
||
*/
|
||
public function hasError(): bool
|
||
{
|
||
return $this->errorMessage !== VoiceMessage::NONE;
|
||
}
|
||
|
||
/**
|
||
* 是否有期望下一步提示
|
||
*/
|
||
public function hasExpectedNextStep(): bool
|
||
{
|
||
return $this->expectedNextStep !== VoiceMessage::NONE;
|
||
}
|
||
|
||
/**
|
||
* 转换为数组
|
||
*/
|
||
public function toArray(): array
|
||
{
|
||
return [
|
||
'message' => $this->message,
|
||
'errorMessage' => $this->errorMessage->name,
|
||
'expectedNextStep' => $this->expectedNextStep->name,
|
||
'templateParams' => $this->templateParams,
|
||
];
|
||
}
|
||
}
|