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; } }