@@ -9,7 +9,7 @@ class CommentTree
99{
1010 /**
1111 * The built nested tree structure array.
12- * @var array{comment: Comment, depth: int, children: array} []
12+ * @var CommentTreeNode []
1313 */
1414 protected array $ tree ;
1515 protected array $ comments ;
@@ -36,9 +36,25 @@ public function count(): int
3636 return count ($ this ->comments );
3737 }
3838
39- public function get (): array
39+ public function getActive (): array
4040 {
41- return $ this ->tree ;
41+ return array_filter ($ this ->tree , fn (CommentTreeNode $ node ) => !$ node ->comment ->archived );
42+ }
43+
44+ public function getArchived (): array
45+ {
46+ return array_filter ($ this ->tree , fn (CommentTreeNode $ node ) => $ node ->comment ->archived );
47+ }
48+
49+ public function getCommentNodeForId (int $ commentId ): ?CommentTreeNode
50+ {
51+ foreach ($ this ->tree as $ node ) {
52+ if ($ node ->comment ->id === $ commentId ) {
53+ return $ node ;
54+ }
55+ }
56+
57+ return null ;
4258 }
4359
4460 public function canUpdateAny (): bool
@@ -54,6 +70,7 @@ public function canUpdateAny(): bool
5470
5571 /**
5672 * @param Comment[] $comments
73+ * @return CommentTreeNode[]
5774 */
5875 protected function createTree (array $ comments ): array
5976 {
@@ -77,26 +94,22 @@ protected function createTree(array $comments): array
7794
7895 $ tree = [];
7996 foreach ($ childMap [0 ] ?? [] as $ childId ) {
80- $ tree [] = $ this ->createTreeForId ($ childId , 0 , $ byId , $ childMap );
97+ $ tree [] = $ this ->createTreeNodeForId ($ childId , 0 , $ byId , $ childMap );
8198 }
8299
83100 return $ tree ;
84101 }
85102
86- protected function createTreeForId (int $ id , int $ depth , array &$ byId , array &$ childMap ): array
103+ protected function createTreeNodeForId (int $ id , int $ depth , array &$ byId , array &$ childMap ): CommentTreeNode
87104 {
88105 $ childIds = $ childMap [$ id ] ?? [];
89106 $ children = [];
90107
91108 foreach ($ childIds as $ childId ) {
92- $ children [] = $ this ->createTreeForId ($ childId , $ depth + 1 , $ byId , $ childMap );
109+ $ children [] = $ this ->createTreeNodeForId ($ childId , $ depth + 1 , $ byId , $ childMap );
93110 }
94111
95- return [
96- 'comment ' => $ byId [$ id ],
97- 'depth ' => $ depth ,
98- 'children ' => $ children ,
99- ];
112+ return new CommentTreeNode ($ byId [$ id ], $ depth , $ children );
100113 }
101114
102115 protected function loadComments (): array
0 commit comments