GradingMarkBoxCounter.php 986 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Support;
  3. class GradingMarkBoxCounter
  4. {
  5. public function countFillBlanks(?string $text): int
  6. {
  7. $text = (string) $text;
  8. $count = 0;
  9. $count += preg_match_all('/_{2,}/u', $text, $m);
  10. $count += preg_match_all('/(\s*)/u', $text, $m);
  11. $count += preg_match_all('/\(\s*\)/', $text, $m);
  12. return max(1, $count);
  13. }
  14. public function countAnswerSteps(?string $text): int
  15. {
  16. $text = (string) $text;
  17. if (!preg_match('/(步骤\s*\d+|第\s*\d+\s*步)/u', $text)) {
  18. return 1;
  19. }
  20. $parts = preg_split('/(?=步骤\s*\d+|第\s*\d+\s*步)/u', $text, -1, PREG_SPLIT_NO_EMPTY) ?: [];
  21. $count = 0;
  22. foreach ($parts as $part) {
  23. $stepText = trim((string) $part);
  24. if ($stepText !== '' && preg_match('/^(步骤\s*\d+|第\s*\d+\s*步)/u', $stepText)) {
  25. $count++;
  26. }
  27. }
  28. return max(1, $count);
  29. }
  30. }