You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

240 lines
6.3 KiB

<?php
declare(strict_types=1);
/**
* ScoreContext.php
* Created by PhpStorm.
* Date: 2026/10/17
* Time: 09:40
* Author: qian.guo
* E-mail: yiyezhiqiutel@gmail.com
*/
namespace app\service\score;
use app\BusinessException;
/**
* 评分输入
* 设计说明:
* - 由调用方装配好后传入,策略内部不再取数:这是多策略本身的前提
* 要能用同一批答案跑多个策略做对比,输入就必须是一个可复制的值对象
* - 题目数量/轴数量/极点/类型/功能全部由外部数据决定,本类不写死任何数量
* - 不改动数据源:本类只读传进来的数组,不查库不读盘
*/
class ScoreContext
{
// 试卷编码
public $paper_code = '';
// 轴列表,每条含 code / name / a / b / a_name / b_name
public $axis_list = [];
// 极点列表,键为极点码
public $pole_list = [];
// 雷达配置,含 order / norm / colors
public $radar = [];
// 认知功能定义列表
public $function_list = [];
// 功能强度计算公式
public $function_formula = 'product';
// 题目列表,键为 q_no
public $question_list = [];
// 作答,键为 q_no,值为选项 key
public $answer_list = [];
// 类型字典,键为类型码
public $type_list = [];
public function __construct($param)
{
$this->paper_code = (string) ($param['paper_code'] ?? '');
$this->axis_list = (array) ($param['axis_list'] ?? []);
$this->pole_list = (array) ($param['pole_list'] ?? []);
$this->radar = (array) ($param['radar'] ?? []);
$this->function_list = (array) ($param['function_list'] ?? []);
$this->function_formula = (string) ($param['function_formula'] ?? 'product');
$this->question_list = (array) ($param['question_list'] ?? []);
$this->answer_list = (array) ($param['answer_list'] ?? []);
$this->type_list = (array) ($param['type_list'] ?? []);
}
/**
* 获取轴数量
* getAxisCount
* @return int 类型码的字母数即为此值
*/
public function getAxisCount()
{
return count($this->axis_list);
}
/**
* 获取题目数量
* getQuestionCount
* @return int
*/
public function getQuestionCount()
{
return count($this->question_list);
}
/**
* 获取类型名称
* getTypeName
* @param string $type_code 类型码
* @return string
*/
public function getTypeName($type_code)
{
if (empty($this->type_list[$type_code]['name'])) {
return '';
}
return (string) $this->type_list[$type_code]['name'];
}
/**
* 获取类型关键词
* getTypeKeywords
* @param string $type_code 类型码
* @return array
*/
public function getTypeKeywords($type_code)
{
if (empty($this->type_list[$type_code]['keywords'])) {
return [];
}
return (array) $this->type_list[$type_code]['keywords'];
}
/**
* 获取极点名称
* getPoleName
* @param string $pole_code 极点码
* @return string
*/
public function getPoleName($pole_code)
{
if (empty($this->pole_list[$pole_code]['name'])) {
return $pole_code;
}
return (string) $this->pole_list[$pole_code]['name'];
}
/**
* 获取极点对应的对偶极点
* getPairPole
* @param string $pole_code 极点码
* @return string
*/
public function getPairPole($pole_code)
{
foreach ($this->axis_list as $axis) {
if ($axis['a'] === $pole_code) {
return (string) $axis['b'];
}
if ($axis['b'] === $pole_code) {
return (string) $axis['a'];
}
}
throw new BusinessException(
config('code.err.score_failed'),
'极点 ' . $pole_code . ' 不属于任何轴,无法确定对偶极'
);
}
/**
* 校验作答完整性
* checkAnswerComplete
* @return array 缺失或非法的题号列表,全部合法时返回空数组
*/
public function checkAnswerComplete()
{
$bad_list = [];
foreach ($this->question_list as $q_no => $question) {
$answer_key = '';
if (!empty($this->answer_list[$q_no])) {
$answer_key = $this->answer_list[$q_no];
}
if ($answer_key === '') {
$bad_list[] = $q_no;
continue;
}
if (empty($question['options'][$answer_key])) {
$bad_list[] = $q_no;
}
}
return $bad_list;
}
/**
* 断言作答完整
* assertAnswerComplete
* @return void
*
* 缺题必须显式失败,不能静默补零:补零会把「没答」变成「答了中间值」
* 用户拿到一个人格类型却基于半份答案,是比报错严重得多的结果
* 因此这里保留一道防线,即使调用方漏了 checkAnswerComplete 也不会算出错误类型
*/
public function assertAnswerComplete()
{
$bad_list = $this->checkAnswerComplete();
if (empty($bad_list)) {
return;
}
$count = count($bad_list);
$preview = implode(',', array_slice($bad_list, 0, 10));
$tail = '';
if ($count > 10) {
$tail = ' …';
}
throw new BusinessException(
config('code.err.quiz_unfinished'),
'还有 ' . $count . ' 题未作答(第 ' . $preview . $tail . ' 题)'
);
}
/**
* 获取全部类型组合
* getAllTypeCodeList
* @return array 各轴极点的笛卡尔积,轴数变化时自动跟随
*/
public function getAllTypeCodeList()
{
$code_list = [''];
$not_empty = false;
foreach ($this->axis_list as $axis) {
$next_list = [];
foreach ($code_list as $prefix) {
$next_list[] = $prefix . $axis['a'];
$next_list[] = $prefix . $axis['b'];
}
$code_list = $next_list;
$not_empty = true;
}
if (!$not_empty) {
return [];
}
return $code_list;
}
}