Advertisement
byVeR

Untitled

Jun 13th, 2024 (edited)
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. class StroopTest
  4. {
  5.     private const COLORS = ['red', 'blue', 'green', 'yellow', 'lime', 'magenta', 'black', 'gold', 'gray', 'tomato'];
  6.  
  7.     public function generate(int $lines = 5): string
  8.     {
  9.         $html = '';
  10.         for ($i = 0; $i < $lines; $i++) {
  11.             $data = $this->generateData();
  12.             $html .= '<p>';
  13.             $html .= $this->generateRow(data: $data);
  14.             $html .= '</p>';
  15.         }
  16.         return $html;
  17.     }
  18.  
  19.     private function generateRow(array $data): string
  20.     {
  21.         $html = '';
  22.         foreach ($data as $color => $word) {
  23.             $html .= "<span style='margin-left:5px; color: $color'>$word</span>";
  24.         }
  25.         return $html;
  26.     }
  27.  
  28.     private function generateData(): array
  29.     {
  30.         $sources = self::COLORS;
  31.         shuffle($sources);
  32.         $count = count($sources);
  33.         $partCount = $count / 2;
  34.         $parts = [
  35.             array_slice($sources, 0, $partCount),
  36.             array_slice($sources, $partCount, $count)
  37.         ];
  38.         shuffle($parts);
  39.         [$colors, $words] = $parts;
  40.         $data = array_combine($words, $colors);
  41.         return array_slice($data, 0, 5);
  42.     }
  43. }
  44.  
  45. ?>
  46. <!DOCTYPE html>
  47. <html lang="en">
  48. <head>
  49.     <meta charset="UTF-8">
  50.     <title>Stroop effect</title>
  51. </head>
  52. <body>
  53. <?= (new StroopTest())->generate() ?>
  54. </body>
  55. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement