Revision.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\Poll;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Poll\Poll;
  12. use InvalidArgumentException;
  13. use function \ForkBB\__;
  14. class Revision extends Action
  15. {
  16. protected $error;
  17. protected $question;
  18. protected $answer;
  19. protected $type;
  20. /**
  21. * Проверяет/нормализует опрос
  22. */
  23. public function revision(Poll $poll, bool $normalize = false) /* : true|string */
  24. {
  25. $this->error = null;
  26. $this->question = [];
  27. $this->answer = [];
  28. $this->type = [];
  29. if (
  30. empty($poll->question)
  31. || ! \is_array($poll->question)
  32. ) {
  33. $this->error = __('The poll structure is broken');
  34. } else {
  35. $this->test($poll);
  36. }
  37. if (
  38. $normalize
  39. && null === $this->error
  40. ) {
  41. $poll->__question = $this->question;
  42. $poll->__answer = $this->answer;
  43. $poll->__type = $this->type;
  44. }
  45. return $this->error ?? true;
  46. }
  47. protected function test(Poll $poll): void
  48. {
  49. $questions = $poll->question;
  50. $answers = $poll->answer;
  51. $types = $poll->type;
  52. $countQ = 0;
  53. $emptyQ = false;
  54. for ($qid = 1; $qid <= $this->c->config->i_poll_max_questions; $qid++) {
  55. if (
  56. ! isset($questions[$qid])
  57. || '' == $questions[$qid]
  58. ) {
  59. $emptyQ = true;
  60. unset($questions[$qid], $answers[$qid]);
  61. continue;
  62. } elseif ($emptyQ) {
  63. $this->error = __(['Question number %s is preceded by an empty question', $qid]);
  64. return;
  65. }
  66. if (
  67. empty($answers[$qid])
  68. || ! \is_array($answers[$qid])
  69. ) {
  70. $this->error = __(['For question number %s, the structure of answers is broken', $qid]);
  71. return;
  72. }
  73. $countA = 0;
  74. $emptyA = false;
  75. for ($fid = 1; $fid <= $this->c->config->i_poll_max_fields; $fid++) {
  76. if (
  77. ! isset($answers[$qid][$fid])
  78. || '' == $answers[$qid][$fid]
  79. ) {
  80. $emptyA = true;
  81. unset($answers[$qid][$fid]);
  82. continue;
  83. } elseif ($emptyA) {
  84. $this->error = __('Answer number %1$s is preceded by an empty answer (question number %2$s)', $fid, $qid);
  85. return;
  86. }
  87. ++$countA;
  88. $this->answer[$qid][$fid] = $answers[$qid][$fid];
  89. unset($answers[$qid][$fid]);
  90. }
  91. if (! empty($answers[$qid])) {
  92. $this->error = __(['For question number %s, the structure of answers is broken', $qid]);
  93. return;
  94. } elseif ($countA < 2) {
  95. $this->error = __('Requires at least two answers per question (%s)', $qid);
  96. return;
  97. } elseif (! isset($types[$qid])) {
  98. $this->error = __(['For question number %s, there is no value for the maximum number of answers for voting', $qid]);
  99. return;
  100. } elseif ($types[$qid] > $countA) {
  101. $this->error = __(['For question number %s, the maximum number of answers for voting more answers', $qid]);
  102. return;
  103. }
  104. ++$countQ;
  105. $this->question[$qid] = $questions[$qid];
  106. $this->type[$qid] = $types[$qid];
  107. unset($questions[$qid], $answers[$qid]);
  108. }
  109. if (
  110. 0 === $countQ
  111. || ! empty($questions)
  112. ) {
  113. $this->error = __('The poll structure is broken');
  114. }
  115. }
  116. }