comments.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/19
  6. * Time: 下午3:16
  7. */
  8. namespace ugc;
  9. use algorithm;
  10. class comments
  11. {
  12. const max_response = 11;
  13. private $counter;
  14. private $users;
  15. private $comments;
  16. private $special_id;
  17. public function __construct($comments,$special_id)
  18. {
  19. $this->counter = [];
  20. $this->users = [];
  21. $this->comments = [];
  22. $this->special_id = intval($special_id);
  23. $this->parse($comments);
  24. }
  25. public function users() {
  26. return $this->users;
  27. }
  28. public function comments() {
  29. return $this->comments;
  30. }
  31. private function parse($comments)
  32. {
  33. $comment_map = [];
  34. foreach ($comments as $comment) {
  35. $comment_id = intval($comment['comment_id']);
  36. $comment_map[$comment_id] = $comment;
  37. }
  38. foreach ($comments as $comment)
  39. {
  40. $response_id = intval($comment['response_id']);
  41. $top_id = intval($comment['top_id']);
  42. if (!array_key_exists($top_id,$this->counter)) {
  43. $this->counter[$top_id] = 0;
  44. }
  45. if($this->counter[$top_id] < self::max_response)
  46. {
  47. if($response_id != 0) {
  48. $resp_comment = $comment_map[$response_id];
  49. $to_user = intval($resp_comment['user_id']);
  50. } else {
  51. $to_user = 0;
  52. }
  53. $this->counter[$top_id] += 1;
  54. $this->comments[] = $this->format_comment($comment,$to_user);
  55. $this->add_user($comment);
  56. }
  57. }
  58. }
  59. private function add_user($comment)
  60. {
  61. $user_id = intval($comment['user_id']);
  62. if(!algorithm::binary_search($this->users,$user_id)) {
  63. $pos = algorithm::lower_bonud($this->users,$user_id);
  64. algorithm::array_insert($this->users,$pos,$user_id);
  65. }
  66. }
  67. private function format_comment($comment,$to_user)
  68. {
  69. $result['comment_id'] = $comment['comment_id'];
  70. $result['to_user'] = $to_user;
  71. $result['content'] = $comment['content'];
  72. $result['sub_comments'] = $comment['sub_comments'];
  73. $result['likes'] = $comment['likes'];
  74. $result['addtime'] = $comment['addtime'];
  75. $result['user_id'] = $comment['user_id'];
  76. $result['response_id'] = $comment['response_id'];
  77. $comment_id = intval($comment['comment_id']);
  78. $supporter = new special_support($this->special_id,$comment_id);
  79. $result['supported'] = $supporter->supported();
  80. return $result;
  81. }
  82. }