Post.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class Post {
  19. private string $Id;
  20. private string $UserId;
  21. private string $Title;
  22. private string $Date;
  23. private string $Image;
  24. private string $Content;
  25. private array $Comments;
  26. public function __construct(string $Id, string $UserId, string $Title, string $Date, string $Image, string $Content, array $Comments) {
  27. $this->Id = $Id;
  28. $this->UserId = $UserId;
  29. $this->Title = $Title;
  30. $this->Date = $Date;
  31. $this->Image = $Image;
  32. $this->Content = $Content;
  33. $this->Comments = $Comments;
  34. }
  35. public function GetId(): string {
  36. return $this->Id;
  37. }
  38. public function GetUserId(): string {
  39. return $this->Id;
  40. }
  41. public function GetTitle(): string {
  42. return $this->Title;
  43. }
  44. public function GetDate(): string {
  45. return $this->Date;
  46. }
  47. public function GetImage(): string {
  48. return $this->Image;
  49. }
  50. public function GetContent(): string {
  51. return $this->Content;
  52. }
  53. public function GetComments(): array {
  54. return $this->Comments;
  55. }
  56. public function SetId(string $Id): void {
  57. $this->Id = $Id;
  58. }
  59. public function SetUserId(string $Id): void {
  60. $this->Id = $Id;
  61. }
  62. public function SetTitle(string $Title): void {
  63. $this->Title = $Title;
  64. }
  65. function SetDate(string $Date): void {
  66. $this->Date = $Date;
  67. }
  68. public function SetImage(string $Image): void {
  69. $this->Image = $Image;
  70. }
  71. public function SetContent(string $Content): void {
  72. $this->Content = $Content;
  73. }
  74. public function SetComments(array $Comments): void {
  75. $this->Comments = $Comments;
  76. }
  77. public function RenderCard(): string {
  78. $HTML = '';
  79. $HTML .= '<div>' . PHP_EOL;
  80. $HTML .= '<strong><a href=".?post=' . $this->Id . '">' . $this->Title . '</a></strong>' . PHP_EOL;
  81. $HTML .= '<span>' . $this->Date . '</span>' . PHP_EOL;
  82. $HTML .= '<div>' . PHP_EOL;
  83. $HTML .= substr($this->Content, 0, 60) . ' ...' . PHP_EOL;
  84. $HTML .= '</div>' . PHP_EOL;
  85. $HTML .= '</div>' . PHP_EOL;
  86. return $HTML;
  87. }
  88. public function RenderComplete(string $UserName, array $UserNamesList, array $CommentsList): string {
  89. $HTML = '';
  90. $HTML .= '<div>' . PHP_EOL;
  91. $HTML .= '<h2>' . $this->Title . '</h2>' . PHP_EOL;
  92. $HTML .= '<span><a href=".?user=' . $this->UserId . '">' . $UserName . '</a></span>' . PHP_EOL;
  93. $HTML .= '<span>' . $this->Date . '</span>' . PHP_EOL;
  94. $HTML .= '<img src="' . $this->Image . '" alt="Post image"/>' . PHP_EOL;
  95. $HTML .= '<div>' . PHP_EOL;
  96. $HTML .= $this->Content . PHP_EOL;
  97. $HTML .= '</div>' . PHP_EOL;
  98. $HTML .= '<div>' . PHP_EOL;
  99. $HTML .= '<h3>Comments</h3>' . PHP_EOL;
  100. if (count($CommentsList) !== 0) {
  101. for ($Position = 0; $Position < count($CommentsList); $Position++) {
  102. $Comment = $CommentsList[$Position];
  103. $UserName = $UserNamesList[$Position];
  104. $HTML .= $Comment->Render($UserName);
  105. }
  106. } else {
  107. $HTML .= '<span>' . $CommentsList . '</span>'. PHP_EOL;
  108. }
  109. $HTML .= '</div>' . PHP_EOL;
  110. $HTML .= '</div>' . PHP_EOL;
  111. return $HTML;
  112. }
  113. }