Elgg  Version master
DataService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Comments;
4 
11 class DataService {
12 
16  protected $counts = [];
17 
26  public function setCommentsCount(int $guid, int $num): void {
27  $this->counts[$guid] = $num;
28  }
29 
37  public function getCommentsCount(\ElggEntity $entity): int {
38  $guid = $entity->guid;
39  if (!isset($this->counts[$guid])) {
40  $this->counts[$guid] = elgg_count_entities([
41  'type' => 'object',
42  'subtype' => 'comment',
43  'container_guid' => $entity->guid,
44  'distinct' => false,
45  ]);
46  }
47 
48  return $this->counts[$guid];
49  }
50 
58  public function filterGuids(array $guids): array {
59  foreach ($guids as $key => $guid) {
60  if (!isset($this->counts[$guid])) {
61  continue;
62  }
63 
64  unset($guids[$key]);
65  }
66 
67  return array_values($guids);
68  }
69 
75  public static function instance(): self {
76  static $inst;
77  if ($inst === null) {
78  $inst = new self();
79  }
80 
81  return $inst;
82  }
83 }
$entity
Definition: reset.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9
foreach( $paths as $path)
Definition: autoloader.php:12
Comments dataservice.
Definition: DataService.php:11
getCommentsCount(\ElggEntity $entity)
Get the number of comments for an entity.
Definition: DataService.php:37
static instance()
Get a DataService instance.
Definition: DataService.php:75
filterGuids(array $guids)
Removes already counted comments from list of guids.
Definition: DataService.php:58
setCommentsCount(int $guid, int $num)
Set number of comments.
Definition: DataService.php:26
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44