From 67094790d2d9db5c99e7c136f49061a78698e57d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 24 Nov 2025 04:58:38 +0900 Subject: Add vhosts/t/phpcon-kagawa-2025/ --- .../src/PhpConKagawa2025/PostHandler.php | 164 +++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 vhosts/t/phpcon-kagawa-2025/src/PhpConKagawa2025/PostHandler.php (limited to 'vhosts/t/phpcon-kagawa-2025/src/PhpConKagawa2025/PostHandler.php') diff --git a/vhosts/t/phpcon-kagawa-2025/src/PhpConKagawa2025/PostHandler.php b/vhosts/t/phpcon-kagawa-2025/src/PhpConKagawa2025/PostHandler.php new file mode 100644 index 0000000..2653e3e --- /dev/null +++ b/vhosts/t/phpcon-kagawa-2025/src/PhpConKagawa2025/PostHandler.php @@ -0,0 +1,164 @@ + + */ + private const array ANSWERS = [ + 'ehime' => 'hiroshima', + 'kagawa' => 'okayama', + 'tokushima' => 'hyogo', + ]; + + /** + * @var array + */ + private const array PREFECTURE_NAMES = [ + 'okayama' => '岡山', + 'hiroshima' => '広島', + 'yamaguchi' => '山口', + 'hyogo' => '兵庫', + 'osaka' => '大阪', + ]; + + private const string QUIZ_FORM = <<<'HTML' + + + + + + 本州四国連絡橋クイズ + + +
+

本州四国連絡橋クイズ

+

四国の各県と橋でつながっている中国地方の県を選んでください。

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
+ + +HTML; + + public function __construct( + private ResponseFactoryInterface $responseFactory, + private StreamFactoryInterface $streamFactory, + ) { + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + if ($request->getMethod() === 'POST') { + $postData = []; + parse_str((string) $request->getBody(), $postData); + + $userEhime = $postData['ehime'] ?? ''; + $userKagawa = $postData['kagawa'] ?? ''; + $userTokushima = $postData['tokushima'] ?? ''; + + $results = []; + $score = 0; + + // Ehime + $userEhimeName = self::PREFECTURE_NAMES[$userEhime] ?? $userEhime; + if ($userEhime === self::ANSWERS['ehime']) { + $results[] = '

愛媛: ' . htmlspecialchars($userEhimeName) . ' ⭕ 正解!

'; + $score++; + } else { + $results[] = '

愛媛: ' . htmlspecialchars($userEhimeName) . ' ❌ 不正解

'; + } + + // Kagawa + $userKagawaName = self::PREFECTURE_NAMES[$userKagawa] ?? $userKagawa; + if ($userKagawa === self::ANSWERS['kagawa']) { + $results[] = '

香川: ' . htmlspecialchars($userKagawaName) . ' ⭕ 正解!

'; + $score++; + } else { + $results[] = '

香川: ' . htmlspecialchars($userKagawaName) . ' ❌ 不正解

'; + } + + // Tokushima + $userTokushimaName = self::PREFECTURE_NAMES[$userTokushima] ?? $userTokushima; + if ($userTokushima === self::ANSWERS['tokushima']) { + $results[] = '

徳島: ' . htmlspecialchars($userTokushimaName) . ' ⭕ 正解!

'; + $score++; + } else { + $results[] = '

徳島: ' . htmlspecialchars($userTokushimaName) . ' ❌ 不正解

'; + } + + $resultHtml = implode("\n", $results); + + $body = << + + + + + クイズ結果 + + +
+

クイズ結果

+

スコア: {$score} / 3

+ {$resultHtml} +

+ もう一度挑戦する +

+
+ + +HTML; + } else { + $body = self::QUIZ_FORM; + } + + return $this->responseFactory->createResponse(200) + ->withHeader('Content-Type', 'text/html; charset=UTF-8') + ->withBody($this->streamFactory->createStream($body)); + } +} -- cgit v1.2.3-70-g09d2