/vendor/Supa/Comment/Comment.php

https://bitbucket.org/resourcemode/smodels · PHP · 250 lines · 96 code · 30 blank · 124 comment · 2 complexity · b49e2745ad1d814b28208e39ef9ed40b MD5 · raw file

  1. <?php
  2. /**
  3. * Smodels
  4. *
  5. * @copyright Copyright (c) 2012-2013 Daniel Latter.
  6. */
  7. namespace Supa\Comment;
  8. use Supa\User\User;
  9. /**
  10. * Concrete Comment class
  11. */
  12. class Comment implements \Supa\Comment\CommentInterface, \Supa\EntityInterface {
  13. /**
  14. * Unique id for review
  15. * @var int
  16. */
  17. protected $uid;
  18. /**
  19. * User who posted comment
  20. * @var \Supa\User\User
  21. */
  22. protected $createdBy;
  23. /**
  24. * Comment content
  25. * @var string
  26. */
  27. protected $content;
  28. /**
  29. * Timestamp of creation
  30. * @var string
  31. */
  32. protected $created;
  33. /**
  34. * Approval flag
  35. * @var int|bool
  36. */
  37. protected $approved;
  38. /**
  39. * Comment score e.g. vote count: up/down
  40. * @var int
  41. */
  42. protected $score;
  43. /**
  44. * Reviewer hostname/IP information
  45. * @var string
  46. */
  47. protected $addedFrom;
  48. public function __construct(array $data = array()) {
  49. $this->assignClassVariables($data);
  50. }
  51. /**
  52. * Comment create timestamp
  53. *
  54. * @param string $created
  55. */
  56. public function setCreated($created) {
  57. $this->created = $created;
  58. }
  59. /**
  60. * Set create timestamp
  61. *
  62. * @return string
  63. */
  64. public function getCreated() {
  65. return $this->created;
  66. }
  67. /**
  68. * Comment content
  69. *
  70. * @return string
  71. */
  72. public function getContent() {
  73. return $this->content;
  74. }
  75. /**
  76. * Set comment content
  77. *
  78. * @param string $content
  79. */
  80. public function setContent($content) {
  81. $this->content = $content;
  82. }
  83. /**
  84. * Get comment score
  85. *
  86. * @return int
  87. */
  88. public function getScore() {
  89. return (int)$this->score;
  90. }
  91. /**
  92. * Increment comment score, vote up
  93. *
  94. * @return void
  95. */
  96. public function incrementScore() {
  97. $score = $this->getScore();
  98. $score++;
  99. $this->setScore($score);
  100. lazy_load_by_mapper2($this, 'CommentMapper', 'updateScore');
  101. }
  102. /**
  103. * Helper method for increasing or decreasing a comment's score
  104. *
  105. * @param string $incOrDec Can be one of: "-", "+", "inc" or "dec"
  106. * @return bool True on success, false otherwise
  107. */
  108. public function score($incOrDec) {
  109. $bool = true;
  110. switch ($incOrDec) {
  111. case '-' :
  112. case 'dec' :
  113. $this->decrementScore();
  114. break;
  115. case '+' :
  116. case 'inc' :
  117. $this->incrementScore();
  118. break;
  119. default:
  120. $bool = false;
  121. }
  122. return $bool;
  123. }
  124. /**
  125. * Decrement comment score, vote down
  126. *
  127. * @return void
  128. */
  129. public function decrementScore() {
  130. $score = $this->getScore();
  131. $score--;
  132. $this->setScore($score);
  133. lazy_load_by_mapper2($this, 'CommentMapper', 'updateScore');
  134. }
  135. /**
  136. * Set comment score
  137. *
  138. * @param int $score
  139. */
  140. public function setScore($score) {
  141. $this->score = $score;
  142. }
  143. /**
  144. * Get Comment author
  145. *
  146. * @return \Supa\User\User
  147. */
  148. public function getCreatedBy() {
  149. return \Supa\lazy_load_user($this->createdBy, 'UserMapper', 'loadByUid');
  150. }
  151. /**
  152. * Set comment author
  153. *
  154. * @param \Supa\User\User $user
  155. */
  156. public function setCreatedBy(\Supa\User\User $user) {
  157. $this->createdBy = $user;
  158. }
  159. /**
  160. * Has comment been approved
  161. *
  162. * @return bool
  163. */
  164. public function isApproved() {
  165. return (bool)$this->approved;
  166. }
  167. /**
  168. * Set comment approval
  169. *
  170. * @param int|bool $flag
  171. */
  172. public function setApproved($flag) {
  173. $this->approved = (bool)$flag;
  174. lazy_load_by_mapper2($this, 'CommentMapper', 'updateApprovedStatus');
  175. }
  176. /**
  177. * Get the comment uid
  178. *
  179. * @return int
  180. */
  181. public function getUID() {
  182. return (int)$this->uid;
  183. }
  184. /**
  185. * Set the comment uid
  186. *
  187. * @param int $uid
  188. * @return \Supa\EntityInterface
  189. */
  190. public function setUID($uid) {
  191. $this->uid = (int)$uid;
  192. return $this;
  193. }
  194. /**
  195. * Get added from data
  196. *
  197. * @return string
  198. */
  199. public function getAddedFrom() {
  200. return $this->addedFrom;
  201. }
  202. /**
  203. * Set added from data
  204. *
  205. * @param string $addedFromData
  206. * @return \Supa\EntityInterface
  207. */
  208. public function setAddedFrom($addedFromData) {
  209. $this->addedFrom = $addedFromData;
  210. return $this;
  211. }
  212. protected function assignClassVariables(array $data = array()) {
  213. $vars = get_object_vars($this);
  214. foreach($data as $var => $value) {
  215. if(array_key_exists($var, $vars)) {
  216. $this->$var = $value;
  217. }
  218. }
  219. }
  220. }