Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class StroopTest
- {
- private const COLORS = ['red', 'blue', 'green', 'yellow', 'lime', 'magenta', 'black', 'gold', 'gray', 'tomato'];
- public function generate(int $lines = 5): string
- {
- $html = '';
- for ($i = 0; $i < $lines; $i++) {
- $data = $this->generateData();
- $html .= '<p>';
- $html .= $this->generateRow(data: $data);
- $html .= '</p>';
- }
- return $html;
- }
- private function generateRow(array $data): string
- {
- $html = '';
- foreach ($data as $color => $word) {
- $html .= "<span style='margin-left:5px; color: $color'>$word</span>";
- }
- return $html;
- }
- private function generateData(): array
- {
- $sources = self::COLORS;
- shuffle($sources);
- $count = count($sources);
- $partCount = $count / 2;
- $parts = [
- array_slice($sources, 0, $partCount),
- array_slice($sources, $partCount, $count)
- ];
- shuffle($parts);
- [$colors, $words] = $parts;
- $data = array_combine($words, $colors);
- return array_slice($data, 0, 5);
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Stroop effect</title>
- </head>
- <body>
- <?= (new StroopTest())->generate() ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement