1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
<?php
declare(strict_types=1);
namespace Nsfisis\TinyPhpHttpd\PhpConKagawa2025;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Server\RequestHandlerInterface;
final readonly class PostHandler implements RequestHandlerInterface
{
/**
* @var array<string, string>
*/
private const array ANSWERS = [
'ehime' => 'hiroshima',
'kagawa' => 'okayama',
'tokushima' => 'hyogo',
];
/**
* @var array<string, string>
*/
private const array PREFECTURE_NAMES = [
'okayama' => '岡山',
'hiroshima' => '広島',
'yamaguchi' => '山口',
'hyogo' => '兵庫',
'osaka' => '大阪',
];
private const string QUIZ_FORM = <<<'HTML'
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>本州四国連絡橋クイズ</title>
</head>
<body>
<main>
<h1>本州四国連絡橋クイズ</h1>
<p>四国の各県と橋でつながっている本州の県を選んでください。</p>
<form method="POST" action="/phpcon-kagawa-2025/post/">
<div>
<label>愛媛とつながっているのは?</label>
<select name="ehime">
<option value="">選択してください</option>
<option value="okayama">岡山</option>
<option value="hiroshima">広島</option>
<option value="yamaguchi">山口</option>
<option value="hyogo">兵庫</option>
<option value="osaka">大阪</option>
</select>
</div>
<div>
<label>香川とつながっているのは?</label>
<select name="kagawa">
<option value="">選択してください</option>
<option value="okayama">岡山</option>
<option value="hiroshima">広島</option>
<option value="yamaguchi">山口</option>
<option value="hyogo">兵庫</option>
<option value="osaka">大阪</option>
</select>
</div>
<div>
<label>徳島とつながっているのは?</label>
<select name="tokushima">
<option value="">選択してください</option>
<option value="okayama">岡山</option>
<option value="hiroshima">広島</option>
<option value="yamaguchi">山口</option>
<option value="hyogo">兵庫</option>
<option value="osaka">大阪</option>
</select>
</div>
<button type="submit">回答する</button>
</form>
</main>
</body>
</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[] = '<p>愛媛: ' . htmlspecialchars($userEhimeName) . ' ⭕ 正解!</p>';
$score++;
} else {
$results[] = '<p>愛媛: ' . htmlspecialchars($userEhimeName) . ' ❌ 不正解</p>';
}
// Kagawa
$userKagawaName = self::PREFECTURE_NAMES[$userKagawa] ?? $userKagawa;
if ($userKagawa === self::ANSWERS['kagawa']) {
$results[] = '<p>香川: ' . htmlspecialchars($userKagawaName) . ' ⭕ 正解!</p>';
$score++;
} else {
$results[] = '<p>香川: ' . htmlspecialchars($userKagawaName) . ' ❌ 不正解</p>';
}
// Tokushima
$userTokushimaName = self::PREFECTURE_NAMES[$userTokushima] ?? $userTokushima;
if ($userTokushima === self::ANSWERS['tokushima']) {
$results[] = '<p>徳島: ' . htmlspecialchars($userTokushimaName) . ' ⭕ 正解!</p>';
$score++;
} else {
$results[] = '<p>徳島: ' . htmlspecialchars($userTokushimaName) . ' ❌ 不正解</p>';
}
$resultHtml = implode("\n", $results);
$body = <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>クイズ結果</title>
</head>
<body>
<main>
<h1>クイズ結果</h1>
<p class="score">スコア: {$score} / 3</p>
{$resultHtml}
<p>
<a href="/phpcon-kagawa-2025/post/">もう一度挑戦する</a>
</p>
</main>
</body>
</html>
HTML;
} else {
$body = self::QUIZ_FORM;
}
return $this->responseFactory->createResponse(200)
->withHeader('Content-Type', 'text/html; charset=UTF-8')
->withBody($this->streamFactory->createStream($body));
}
}
|