示例#1
0
 /**
  * Build comments tree from doctrine given their source
  *
  * cost O(n)
  *
  * @param string $source  Source of comments
  * @param string $context Context of comment
  *
  * @return Array Comments tree given the source
  */
 private function buildCommentTree($source, $context)
 {
     $comments = $this->commentRepository->getAllCommentsSortedByParentAndIdAsc($source, $context);
     $commentTree = [0 => null, 'children' => []];
     /**
      * @var CommentInterface $comment
      */
     foreach ($comments as $comment) {
         $parentCommentId = 0;
         $commentId = $comment->getId();
         if ($comment->getParent() instanceof CommentInterface) {
             $parentCommentId = $comment->getParent()->getId();
         }
         if ($parentCommentId && !isset($commentTree[$parentCommentId])) {
             $commentTree[$parentCommentId] = ['entity' => null, 'children' => []];
         }
         if (!isset($commentTree[$commentId])) {
             $commentTree[$commentId] = ['entity' => null, 'children' => []];
         }
         $commentVotePackage = $this->voteManager->getCommentVotes($comment);
         $commentTree[$commentId]['entity'] = $this->createCommentStructure($comment, $commentVotePackage);
         $commentTree[$parentCommentId]['children'][] =& $commentTree[$commentId];
     }
     return $commentTree[0]['children'] ?: [];
 }
示例#2
0
 /**
  * Test load.
  */
 public function testLoad()
 {
     $this->commentRepository->expects($this->any())->method('getAllCommentsSortedByParentAndIdAsc')->with($this->equalTo('source'))->will($this->returnValue($this->getCommentsStructure()));
     $comments = $this->commentCache->load('source', 'context');
     $this->assertEquals([['entity' => ['id' => 1, 'authorName' => 'Marc Morera', 'authorEmail' => '*****@*****.**', 'context' => 'admin', 'content' => 'comment1', 'nbVotes' => 2, 'nbUpVotes' => 1, 'nbDownVotes' => 1, 'createdAt' => '2015-01-01 00:00:00', 'updatedAt' => '2015-01-01 00:00:00'], 'children' => [['entity' => ['id' => 2, 'authorName' => 'Marc Morera', 'authorEmail' => '*****@*****.**', 'context' => 'admin', 'content' => 'comment2', 'nbVotes' => 2, 'nbUpVotes' => 1, 'nbDownVotes' => 1, 'createdAt' => '2015-01-01 00:00:00', 'updatedAt' => '2015-01-01 00:00:00'], 'children' => []], ['entity' => ['id' => 3, 'authorName' => 'Another guy', 'authorEmail' => '*****@*****.**', 'context' => 'admin', 'content' => 'comment3', 'nbVotes' => 2, 'nbUpVotes' => 1, 'nbDownVotes' => 1, 'createdAt' => '2015-01-01 00:00:00', 'updatedAt' => '2015-01-01 00:00:00'], 'children' => []]]], ['entity' => ['id' => 4, 'authorName' => 'Marc Morera', 'authorEmail' => '*****@*****.**', 'context' => 'admin', 'content' => 'comment4', 'nbVotes' => 2, 'nbUpVotes' => 1, 'nbDownVotes' => 1, 'createdAt' => '2015-01-01 00:00:00', 'updatedAt' => '2015-01-01 00:00:00'], 'children' => [['entity' => ['id' => 5, 'authorName' => 'Marc Morera', 'authorEmail' => '*****@*****.**', 'context' => 'admin', 'content' => 'comment5', 'nbVotes' => 2, 'nbUpVotes' => 1, 'nbDownVotes' => 1, 'createdAt' => '2015-01-01 00:00:00', 'updatedAt' => '2015-01-01 00:00:00'], 'children' => []]]]], $comments);
 }