Load.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Post;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Post\Post;
  12. use ForkBB\Models\Topic\Topic;
  13. use InvalidArgumentException;
  14. use RuntimeException;
  15. class Load extends Action
  16. {
  17. /**
  18. * Создает текст запрос
  19. */
  20. protected function getSql(string $where): string
  21. {
  22. $query = 'SELECT p.*
  23. FROM ::posts AS p
  24. WHERE ' . $where;
  25. return $query;
  26. }
  27. /**
  28. * Загружает сообщение из БД с проверкой вхождения в указанную тему
  29. * Загружает сообщение из БД
  30. * Загружает тему этого сообщения в репозиторий topics
  31. * Проверка доступности
  32. */
  33. public function load(int $id, ?int $tid): ?Post
  34. {
  35. if ($id < 1) {
  36. throw new InvalidArgumentException('Expected a positive post id');
  37. }
  38. if (
  39. null !== $tid
  40. && $tid < 1
  41. ) {
  42. throw new InvalidArgumentException('Expected a positive topic id');
  43. }
  44. $vars = [
  45. ':pid' => $id,
  46. ':tid' => $tid,
  47. ];
  48. $query = $this->getSql(null !== $tid ? 'p.id=?i:pid AND p.topic_id=?i:tid' : 'p.id=?i:pid');
  49. $data = $this->c->DB->query($query, $vars)->fetch();
  50. if (empty($data)) {
  51. return null;
  52. }
  53. $post = $this->manager->create($data);
  54. $topic = $post->parent;
  55. if (! $topic instanceof Topic) {
  56. return null;
  57. }
  58. if (
  59. null !== $tid
  60. && $topic->id !== $tid
  61. ) {
  62. return null;
  63. }
  64. return $post;
  65. }
  66. /**
  67. * Загружает список сообщений из БД
  68. */
  69. public function loadByIds(array $ids, bool $withTopics): array
  70. {
  71. foreach ($ids as $id) {
  72. if (
  73. ! \is_int($id)
  74. || $id < 1
  75. ) {
  76. throw new InvalidArgumentException('Expected a positive topic id');
  77. }
  78. }
  79. $vars = [
  80. ':ids' => $ids,
  81. ];
  82. $query = $this->getSql('p.id IN (?ai:ids)');
  83. $stmt = $this->c->DB->query($query, $vars);
  84. $result = [];
  85. $topicIds = [];
  86. while ($row = $stmt->fetch()) {
  87. $post = $this->manager->create($row);
  88. $topicIds[$post->topic_id] = $post->topic_id;
  89. $result[] = $post;
  90. }
  91. if ($withTopics) {
  92. $this->c->topics->loadByIds($topicIds, true);
  93. foreach ($result as &$post) {
  94. if (! $post->parent instanceof Topic) {
  95. $post = null;
  96. }
  97. }
  98. unset($post);
  99. } else {
  100. foreach ($topicIds as $id) {
  101. if (
  102. ! $this->c->topics->isset($id)
  103. || ! $this->c->topics->get($id) instanceof Topic
  104. ) {
  105. throw new RuntimeException("Topic number {$id} not loaded");
  106. }
  107. }
  108. }
  109. return $result;
  110. }
  111. }