12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/7/19
- * Time: 下午3:16
- */
- namespace ugc;
- use algorithm;
- class comments
- {
- const max_response = 11;
- private $counter;
- private $users;
- private $comments;
- private $special_id;
- public function __construct($comments,$special_id)
- {
- $this->counter = [];
- $this->users = [];
- $this->comments = [];
- $this->special_id = intval($special_id);
- $this->parse($comments);
- }
- public function users() {
- return $this->users;
- }
- public function comments() {
- return $this->comments;
- }
- private function parse($comments)
- {
- $comment_map = [];
- foreach ($comments as $comment) {
- $comment_id = intval($comment['comment_id']);
- $comment_map[$comment_id] = $comment;
- }
- foreach ($comments as $comment)
- {
- $response_id = intval($comment['response_id']);
- $top_id = intval($comment['top_id']);
- if (!array_key_exists($top_id,$this->counter)) {
- $this->counter[$top_id] = 0;
- }
- if($this->counter[$top_id] < self::max_response)
- {
- if($response_id != 0) {
- $resp_comment = $comment_map[$response_id];
- $to_user = intval($resp_comment['user_id']);
- } else {
- $to_user = 0;
- }
- $this->counter[$top_id] += 1;
- $this->comments[] = $this->format_comment($comment,$to_user);
- $this->add_user($comment);
- }
- }
- }
- private function add_user($comment)
- {
- $user_id = intval($comment['user_id']);
- if(!algorithm::binary_search($this->users,$user_id)) {
- $pos = algorithm::lower_bonud($this->users,$user_id);
- algorithm::array_insert($this->users,$pos,$user_id);
- }
- }
- private function format_comment($comment,$to_user)
- {
- $result['comment_id'] = $comment['comment_id'];
- $result['to_user'] = $to_user;
- $result['content'] = $comment['content'];
- $result['sub_comments'] = $comment['sub_comments'];
- $result['likes'] = $comment['likes'];
- $result['addtime'] = $comment['addtime'];
- $result['user_id'] = $comment['user_id'];
- $result['response_id'] = $comment['response_id'];
-
- $comment_id = intval($comment['comment_id']);
- $supporter = new special_support($this->special_id,$comment_id);
- $result['supported'] = $supporter->supported();
- return $result;
- }
- }
|