PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/RedditApiClient/Link.php

https://github.com/jariz/ChannelBot
PHP | 398 lines | 133 code | 42 blank | 223 comment | 9 complexity | 44550cdd6603f1cc2daf96f4ee6bb8aa MD5 | raw file
  1. <?php
  2. namespace RedditApiClient;
  3. /**
  4. * Link
  5. *
  6. * Represents a reddit post or submission
  7. *
  8. * The following is an example of the sort of data links contain:
  9. *
  10. * [data] => Array
  11. * (
  12. * [domain] => code.reddit.com
  13. * [media_embed] => Array
  14. * (
  15. * )
  16. *
  17. * [levenshtein] =>
  18. * [subreddit] => programming
  19. * [selftext_html] =>
  20. * [selftext] =>
  21. * [likes] =>
  22. * [saved] =>
  23. * [id] => 6nw57
  24. * [clicked] =>
  25. * [title] => Reddit has gone Open Source !!
  26. * [media] =>
  27. * [score] => 1435
  28. * [over_18] =>
  29. * [hidden] =>
  30. * [thumbnail] =>
  31. * [subreddit_id] => t5_2fwo
  32. * [author_flair_css_class] =>
  33. * [downs] => 276
  34. * [is_self] =>
  35. * [permalink] => /r/programming/comments/6nw57/reddit_has_gone_open_source/
  36. * [name] => t3_6nw57
  37. * [created] => 1213794727
  38. * [url] => http://code.reddit.com/
  39. * [author_flair_text] =>
  40. * [author] => ropiku
  41. * [created_utc] => 1213794727
  42. * [num_comments] => 212
  43. * [ups] => 1711
  44. * )
  45. *
  46. *
  47. * @author Henry Smith <henry@henrysmith.org>
  48. * @copyright 2011 Henry Smith
  49. * @license GPLv2.0
  50. * @package Reddit API Client
  51. * @version 0.5.2
  52. */
  53. class Link extends Entity
  54. {
  55. /**
  56. * An array of the comments in reply to the link
  57. *
  58. * @access private
  59. * @var array
  60. */
  61. private $comments = null;
  62. /**
  63. * Returns the link's unique 't3_*' ID
  64. *
  65. * @access public
  66. * @return string
  67. */
  68. public function getThingId()
  69. {
  70. return $this['name'];
  71. }
  72. /**
  73. * Returns the unique ID of the link
  74. *
  75. * @access public
  76. * @return string
  77. */
  78. public function getId()
  79. {
  80. return $this['id'];
  81. }
  82. /**
  83. * Returns the number of upvotes received by the link
  84. *
  85. * @access public
  86. * @return integer
  87. */
  88. public function getUpvotes()
  89. {
  90. return $this['ups'];
  91. }
  92. /**
  93. * Returns the number of downvotes received by the link
  94. *
  95. * @access public
  96. * @return integer
  97. */
  98. public function getDownvotes()
  99. {
  100. return $this['downs'];
  101. }
  102. /**
  103. * Returns the score of the link
  104. *
  105. * Score is a function of upvotes, downvotes, and time since creation
  106. *
  107. * @access public
  108. * @return integer
  109. */
  110. public function getScore()
  111. {
  112. return $this['score'];
  113. }
  114. /**
  115. * Returns the number of comments made about the link
  116. *
  117. * @access public
  118. * @return integer
  119. */
  120. public function countComments()
  121. {
  122. return $this['num_comments'];
  123. }
  124. /**
  125. * Returns the username of the user who submitted the link
  126. *
  127. * @access public
  128. * @return string
  129. */
  130. public function getAuthorName()
  131. {
  132. return $this['author'];
  133. }
  134. /**
  135. * Returns the title of the link
  136. *
  137. * @access public
  138. * @return string
  139. */
  140. public function getTitle()
  141. {
  142. return $this['title'];
  143. }
  144. /**
  145. * Returns the URL of the link
  146. *
  147. * If the link is a self-post, the URL will contain the text body of the post
  148. * instead.
  149. *
  150. * It's worth emphasising here that this method *does not* return the URL of
  151. * the post's page on reddit.com. Use getPermalink() for that.
  152. *
  153. * @access public
  154. * @return string
  155. */
  156. public function getUrl()
  157. {
  158. return $this['url'];
  159. }
  160. /**
  161. * Returns the plain-text version of the text given as part of a self-post
  162. *
  163. * @access public
  164. * @return string
  165. */
  166. public function getSelfText()
  167. {
  168. return $this['selftext'];
  169. }
  170. /**
  171. * Returns the array of comments in reply to the link
  172. *
  173. * @access public
  174. * @return array
  175. */
  176. public function getComments()
  177. {
  178. if (!is_array($this->comments)) {
  179. $this->comments = array();
  180. $linkId = $this->getId();
  181. $link = $this->reddit->getLink($linkId, true);
  182. $this->comments = $link->getComments();
  183. }
  184. return $this->comments;
  185. }
  186. /**
  187. * Indicates whether the link is a self-post or not
  188. *
  189. * @access public
  190. * @return boolean
  191. */
  192. public function isSelfPost()
  193. {
  194. if ($this['is_self']) {
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }
  200. /**
  201. * Stores the given array of comments
  202. *
  203. * @access public
  204. * @param array $comments
  205. */
  206. public function setComments(array $comments)
  207. {
  208. $this->comments = $comments;
  209. }
  210. /**
  211. * Casts a vote on the link
  212. *
  213. * @access public
  214. * @param integer $direction 1 for an upvote, -1 for down, 0 to remove votes
  215. * @return boolean
  216. */
  217. public function vote($direction)
  218. {
  219. $thingId = $this->getThingId();
  220. return $this->reddit->vote($thingId, $direction);
  221. }
  222. /**
  223. * Posts a comment on the link
  224. *
  225. * @access public
  226. * @param string $text
  227. * @return boolean
  228. */
  229. public function reply($text)
  230. {
  231. $thingId = $this->getThingId();
  232. return $this->reddit->comment($thingId, $text);
  233. }
  234. /**
  235. * Saves the link
  236. *
  237. * @access public
  238. * @return boolean
  239. */
  240. public function save()
  241. {
  242. $thingId = $this->getThingId();
  243. return $this->reddit->save($thingId);
  244. }
  245. /**
  246. * Unsaves the link
  247. *
  248. * @access public
  249. * @return boolean
  250. */
  251. public function unsave()
  252. {
  253. $thingId = $this->getThingId();
  254. return $this->reddit->unsave($thingId);
  255. }
  256. /**
  257. * Hides the link
  258. *
  259. * @access public
  260. * @return boolean
  261. */
  262. public function hide()
  263. {
  264. $thingId = $this->getThingId();
  265. return $this->reddit->hide($thingId);
  266. }
  267. /**
  268. * Unhides the link
  269. *
  270. * @access public
  271. * @return boolean
  272. */
  273. public function unhide()
  274. {
  275. $thingId = $this->getThingId();
  276. return $this->reddit->unhide($thingId);
  277. }
  278. /**
  279. * Indicates whether the post has been saved by the logged in user
  280. *
  281. * Won't return anything meaningful if the post was retrieved from a Reddit
  282. * instance that wasn't logged in
  283. *
  284. * @access public
  285. * @return boolean
  286. */
  287. public function isSaved()
  288. {
  289. if (isset($this['saved'])) {
  290. return $this['saved'];
  291. } else {
  292. return false;
  293. }
  294. }
  295. /**
  296. * Indicates whether the post has been hidden by the logged in user
  297. *
  298. * Won't return anything meaningful if the post was retrieved from a Reddit
  299. * instance that wasn't logged in
  300. *
  301. * @access public
  302. * @return boolean
  303. */
  304. public function isHidden()
  305. {
  306. if (isset($this['hidden'])) {
  307. return $this['hidden'];
  308. } else {
  309. return false;
  310. }
  311. }
  312. /**
  313. * Returns the permalink to the link's page on reddit.com
  314. *
  315. * By default this will be a relative URL because this is what the API
  316. * returns, but setting $absolute to true sorts that out.
  317. *
  318. * @access public
  319. * @param boolean $absolute
  320. * @return string
  321. */
  322. public function getPermalink($absolute = false)
  323. {
  324. if (!isset($this['permalink'])) {
  325. return null;
  326. }
  327. $permalink = $this['permalink'];
  328. if ($absolute) {
  329. $permalink = 'http://www.reddit.com' . $permalink;
  330. }
  331. return $permalink;
  332. }
  333. /**
  334. * Indicates whether the link is restricted to those 18 years of age or more
  335. *
  336. * @access public
  337. * @return boolean
  338. */
  339. public function isAgeRestricted()
  340. {
  341. return isset($this['over18']) ? $this['over18'] : false;
  342. }
  343. /**
  344. * Indicates whether the logged-in user has clicked on the link before
  345. *
  346. * Won't return anything meaningful if retrieved by a reddit instance that
  347. * wasn't logged in
  348. *
  349. * @access public
  350. * @return boolean
  351. */
  352. public function isClicked()
  353. {
  354. return isset($this['clicked']) ? $this['clicked'] : false;
  355. }
  356. }