DataService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Elgg\Likes;
  3. /**
  4. * @access private
  5. */
  6. class DataService {
  7. /**
  8. * @var array [GUID => boolean]
  9. */
  10. protected $current_user_likes = array();
  11. /**
  12. * @var array [GUID => int]
  13. */
  14. protected $num_likes = array();
  15. /**
  16. * @param int $guid
  17. * @param int $num
  18. */
  19. public function setNumLikes($guid, $num) {
  20. $this->num_likes[$guid] = (int)$num;
  21. }
  22. /**
  23. * @param int $guid
  24. * @param bool $is_liked
  25. */
  26. public function setLikedByCurrentUser($guid, $is_liked) {
  27. $this->current_user_likes[$guid] = (bool)$is_liked;
  28. }
  29. /**
  30. * @param int $entity_guid
  31. * @return bool
  32. */
  33. public function currentUserLikesEntity($entity_guid) {
  34. if (!isset($this->current_user_likes[$entity_guid])) {
  35. $this->current_user_likes[$entity_guid] = elgg_annotation_exists($entity_guid, 'likes');
  36. }
  37. return $this->current_user_likes[$entity_guid];
  38. }
  39. /**
  40. * @param \ElggEntity $entity
  41. * @return int
  42. */
  43. public function getNumLikes(\ElggEntity $entity) {
  44. $guid = $entity->guid;
  45. if (!isset($this->num_likes[$guid])) {
  46. $this->num_likes[$guid] = likes_count($entity);
  47. }
  48. return $this->num_likes[$guid];
  49. }
  50. /**
  51. * @return DataService
  52. */
  53. public static function instance() {
  54. static $inst;
  55. if ($inst === null) {
  56. $inst = new self();
  57. }
  58. return $inst;
  59. }
  60. }