fork download
  1. <?php
  2.  
  3.  
  4. class ExternalPost
  5. {
  6. private $reactions;
  7.  
  8. private $comments;
  9.  
  10. private $shares;
  11.  
  12. public function __construct(Reactions $reactions = null, Comments $comments = null, Shares $shares = null)
  13. {
  14. $this->reactions = $reactions;
  15. $this->comments = $comments;
  16. $this->shares = $shares;
  17. }
  18.  
  19. public function getReactions()
  20. {
  21. if (null === $this->reactions) {
  22. $this->reactions = new Reactions;
  23. }
  24. return $this->reactions;
  25. }
  26.  
  27. public function getComments()
  28. {
  29. if(null === $this->comments) {
  30. $this->comments = new Comments;
  31. }
  32. return $this->comments;
  33. }
  34.  
  35. public function getShares()
  36. {
  37. if (null === $this->shares) {
  38. $this->shares = new Shares;
  39. }
  40. return $this->shares;
  41. }
  42.  
  43. public function getTotal()
  44. {
  45. return $this->getReactions()->getTotal() +
  46. $this->getComments()->getTotal() +
  47. $this->getShares()->getCount();
  48. }
  49. }
  50.  
  51. class Summary
  52. {
  53. private $total_count = 0;
  54.  
  55. public function getTotalCount()
  56. {
  57. return $this->total_count;
  58. }
  59.  
  60. public function setTotalCount($total)
  61. {
  62. $this->total_count = $total;
  63.  
  64. return $this;
  65. }
  66. }
  67.  
  68. abstract class Summation
  69. {
  70. protected $summary;
  71.  
  72. public function __construct(Summary $summary = null)
  73. {
  74. if (null === $summary) {
  75. $summary = new Summary;
  76. }
  77. $this->summary = $summary;
  78. }
  79.  
  80. public function getTotal()
  81. {
  82. return $this->summary->getTotalCount();
  83. }
  84.  
  85. public function setTotal($total)
  86. {
  87. $this->summary->setTotalCount($total);
  88.  
  89. return $this;
  90. }
  91. }
  92.  
  93. class Reactions extends Summation{}
  94.  
  95. class Comments extends Summation{}
  96.  
  97. class Shares
  98. {
  99. private $count = 0;
  100.  
  101. public function getCount()
  102. {
  103. return $this->count;
  104. }
  105.  
  106. public function setCount($count)
  107. {
  108. $this->count = $count;
  109.  
  110. return $this;
  111. }
  112. }
  113.  
  114.  
  115. $reactions = new Reactions;
  116. $reactions->setTotal(1);
  117.  
  118. $comments = new Comments;
  119. $reactions->setTotal(2);
  120.  
  121. $ExternalPost = new ExternalPost($reactions, $comments);
  122. $ExternalPost->getShares()->setCount(3);
  123.  
  124. var_dump($ExternalPost->getTotal());
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
int(5)