summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2017-10-11 17:08:45 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2017-10-22 14:13:37 +0200
commit8f0a9ae51fdc92e7c33f7a48b9d84da6c6b196e1 (patch)
tree22f40cb4b109391ca84352febcebeb4d86689411 /lib
parentfd6daf8d195b985fcdec82c0c53e8ba230765f41 (diff)
downloadnextcloud-server-8f0a9ae51fdc92e7c33f7a48b9d84da6c6b196e1.tar.gz
nextcloud-server-8f0a9ae51fdc92e7c33f7a48b9d84da6c6b196e1.zip
split walking the tree from operating on it
so walking it is reusable Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/Comments/Manager.php39
1 files changed, 25 insertions, 14 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php
index a6b2102da67..3b5eb23844c 100644
--- a/lib/private/Comments/Manager.php
+++ b/lib/private/Comments/Manager.php
@@ -340,12 +340,12 @@ class Manager implements ICommentsManager {
public function getActorsInTree($id) {
$tree = $this->getTree($id);
$actors = [];
- $this->extractActor($tree, $actors);
+ $this->walkTree($tree, $actors, [$this, 'extractActor']);
return $actors;
}
/**
- * @param array $node
+ * @param IComment $comment
* @param array &$actors
*
* build an array that looks like:
@@ -362,23 +362,34 @@ class Manager implements ICommentsManager {
* ]
*
*/
- protected function extractActor(array $node, array &$actors) {
+ protected function extractActor(IComment $comment, &$actors) {
+ if(!isset($actors[$comment->getActorType()])) {
+ $actors[$comment->getActorType()] = [];
+ }
+ if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
+ $actors[$comment->getActorType()][$comment->getActorId()] = 1;
+ } else {
+ $actors[$comment->getActorType()][$comment->getActorId()] += 1;
+ }
+ }
+
+ /**
+ * walks through a comment tree (as returned by getTree() and invokes a callback
+ * with the current IComment instance (and optionally custom parameters)
+ *
+ * @param array $node
+ * @param array &$results
+ * @param callable $callback
+ * @param array|null $parameters
+ */
+ protected function walkTree($node, array &$results, callable $callback, array $parameters = null) {
if(isset($node['replies'])) {
foreach ($node['replies'] as $subNode) {
- $this->extractActor($subNode, $actors);
+ $this->walkTree($subNode, $results, $callback, $parameters);
}
}
if(isset($node['comment']) && $node['comment'] instanceof IComment) {
- /** @var IComment $comment */
- $comment = $node['comment'];
- if(!isset($actors[$comment->getActorType()])) {
- $actors[$comment->getActorType()] = [];
- }
- if(!isset($actors[$comment->getActorType()][$comment->getActorId()])) {
- $actors[$comment->getActorType()][$comment->getActorId()] = 1;
- } else {
- $actors[$comment->getActorType()][$comment->getActorId()] += 1;
- }
+ $callback($node['comment'], $results, $parameters);
}
}