{ $this->structure = $structure; $this->answers = $answers; } public function validate() { return $this->structure->check($this->answers); } } if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { $postdata = file_get_contents("php://input"); $request = json_decode($postdata, true); $structure = $request['structure']; $structureId = $request['structureId']; $path = MAIN_PATH . BARANEK_PATH . "php/structures/" . ucfirst($structure) . "StructureStrategy.php"; require_once $path; } catch (Exception $e) { echo "reject"; exit; } $strategy = ucfirst($structure) . "StructureStrategy"; $validator = new AnswerValidator(new $strategy($structureId), (array) $request['answers']); if ($validator->validate()) { echo "resolve"; } else { echo "reject"; } } else { echo "reject"; }
/** * 一つのお題を表す配列から、正規表現を除いた解答をShift_JISに直列化可能に文字列に変換して返します。 * @param (string|string[]|float)[][] $word * @param callable|null $convert 変換器。 * @return string[] 選択肢必須の問題、またはShift_JISに直列化不能な解答しか存在しなければ、空の配列を返します。 */ protected function getAnswers(array $word, callable $convert = null) : array { $answerValidator = new AnswerValidator(); foreach (isset($word['answer'][0]) ? $word['answer'] : $word['text'] as $answer) { if (!$answerValidator->isRegExp($answer)) { $shiftJISable = $convert ? call_user_func($convert, $answer) : $this->convertToShiftJISableInAnswer($answer); if ($this->isShiftJISable($shiftJISable)) { $answers[] = $shiftJISable; } } } return $answers ?? []; }