PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Twitter/Friends.php

https://github.com/piotr-cz/joomla-framework
PHP | 460 lines | 218 code | 56 blank | 186 comment | 27 complexity | 247dc18d61e137adde0d7e17e6395805 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Part of the Joomla Framework Twitter Package
  4. *
  5. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\Twitter;
  9. /**
  10. * Twitter API Friends class for the Joomla Framework.
  11. *
  12. * @since 1.0
  13. */
  14. class Friends extends Object
  15. {
  16. /**
  17. * Method to get an array of user IDs the specified user follows.
  18. *
  19. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  20. * @param integer $cursor Causes the list of connections to be broken into pages of no more than 5000 IDs at a time.
  21. * The number of IDs returned is not guaranteed to be 5000 as suspended users are filtered out
  22. * after connections are queried. If no cursor is provided, a value of -1 will be assumed, which is the first "page."
  23. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  24. * @param integer $count Specifies the number of IDs attempt retrieval of, up to a maximum of 5,000 per distinct request.
  25. *
  26. * @return array The decoded JSON response
  27. *
  28. * @since 1.0
  29. * @throws \RuntimeException
  30. */
  31. public function getFriendIds($user, $cursor = null, $string_ids = null, $count = 0)
  32. {
  33. // Check the rate limit for remaining hits
  34. $this->checkRateLimit('friends', 'ids');
  35. // Determine which type of data was passed for $user
  36. if (is_numeric($user))
  37. {
  38. $data['user_id'] = $user;
  39. }
  40. elseif (is_string($user))
  41. {
  42. $data['screen_name'] = $user;
  43. }
  44. else
  45. {
  46. // We don't have a valid entry
  47. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  48. }
  49. // Check if cursor is specified
  50. if (!is_null($cursor))
  51. {
  52. $data['cursor'] = $cursor;
  53. }
  54. // Check if string_ids is true
  55. if ($string_ids)
  56. {
  57. $data['stringify_ids'] = $string_ids;
  58. }
  59. // Check if count is specified
  60. if ($count > 0)
  61. {
  62. $data['count'] = $count;
  63. }
  64. // Set the API path
  65. $path = '/friends/ids.json';
  66. // Send the request.
  67. return $this->sendRequest($path, 'GET', $data);
  68. }
  69. /**
  70. * Method to display detailed friend information between two users.
  71. *
  72. * @param mixed $user_a Either an integer containing the user ID or a string containing the screen name of the first user.
  73. * @param mixed $user_b Either an integer containing the user ID or a string containing the screen name of the second user.
  74. *
  75. * @return array The decoded JSON response
  76. *
  77. * @since 1.0
  78. * @throws \RuntimeException
  79. */
  80. public function getFriendshipDetails($user_a, $user_b)
  81. {
  82. // Check the rate limit for remaining hits
  83. $this->checkRateLimit('friendships', 'show');
  84. // Determine which type of data was passed for $user_a
  85. if (is_numeric($user_a))
  86. {
  87. $data['source_id'] = $user_a;
  88. }
  89. elseif (is_string($user_a))
  90. {
  91. $data['source_screen_name'] = $user_a;
  92. }
  93. else
  94. {
  95. // We don't have a valid entry
  96. throw new \RuntimeException('The first specified username is not in the correct format; must use integer or string');
  97. }
  98. // Determine which type of data was passed for $user_b
  99. if (is_numeric($user_b))
  100. {
  101. $data['target_id'] = $user_b;
  102. }
  103. elseif (is_string($user_b))
  104. {
  105. $data['target_screen_name'] = $user_b;
  106. }
  107. else
  108. {
  109. // We don't have a valid entry
  110. throw new \RuntimeException('The second specified username is not in the correct format; must use integer or string');
  111. }
  112. // Set the API path
  113. $path = '/friendships/show.json';
  114. // Send the request.
  115. return $this->sendRequest($path, 'GET', $data);
  116. }
  117. /**
  118. * Method to get an array of user IDs the specified user is followed by.
  119. *
  120. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  121. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  122. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  123. * is provided, a value of -1 will be assumed, which is the first "page."
  124. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  125. * @param integer $count Specifies the number of IDs attempt retrieval of, up to a maximum of 5,000 per distinct request.
  126. *
  127. * @return array The decoded JSON response
  128. *
  129. * @since 1.0
  130. * @throws \RuntimeException
  131. */
  132. public function getFollowerIds($user, $cursor = null, $string_ids = null, $count = 0)
  133. {
  134. // Check the rate limit for remaining hits
  135. $this->checkRateLimit('followers', 'ids');
  136. // Determine which type of data was passed for $user
  137. if (is_numeric($user))
  138. {
  139. $data['user_id'] = $user;
  140. }
  141. elseif (is_string($user))
  142. {
  143. $data['screen_name'] = $user;
  144. }
  145. else
  146. {
  147. // We don't have a valid entry
  148. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  149. }
  150. // Set the API path
  151. $path = '/followers/ids.json';
  152. // Check if cursor is specified
  153. if (!is_null($cursor))
  154. {
  155. $data['cursor'] = $cursor;
  156. }
  157. // Check if string_ids is specified
  158. if (!is_null($string_ids))
  159. {
  160. $data['stringify_ids'] = $string_ids;
  161. }
  162. // Check if count is specified
  163. if (!is_null($count))
  164. {
  165. $data['count'] = $count;
  166. }
  167. // Send the request.
  168. return $this->sendRequest($path, 'GET', $data);
  169. }
  170. /**
  171. * Method to determine pending requests to follow the authenticating user.
  172. *
  173. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  174. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  175. * is provided, a value of -1 will be assumed, which is the first "page."
  176. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  177. *
  178. * @return array The decoded JSON response
  179. *
  180. * @since 1.0
  181. */
  182. public function getFriendshipsIncoming($cursor = null, $string_ids = null)
  183. {
  184. // Check the rate limit for remaining hits
  185. $this->checkRateLimit('friendships', 'incoming');
  186. $data = array();
  187. // Check if cursor is specified
  188. if (!is_null($cursor))
  189. {
  190. $data['cursor'] = $cursor;
  191. }
  192. // Check if string_ids is specified
  193. if (!is_null($string_ids))
  194. {
  195. $data['stringify_ids'] = $string_ids;
  196. }
  197. // Set the API path
  198. $path = '/friendships/incoming.json';
  199. // Send the request.
  200. return $this->sendRequest($path, 'GET', $data);
  201. }
  202. /**
  203. * Method to determine every protected user for whom the authenticating user has a pending follow request.
  204. *
  205. * @param integer $cursor Causes the list of IDs to be broken into pages of no more than 5000 IDs at a time. The number of IDs returned
  206. * is not guaranteed to be 5000 as suspended users are filtered out after connections are queried. If no cursor
  207. * is provided, a value of -1 will be assumed, which is the first "page."
  208. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  209. *
  210. * @return array The decoded JSON response
  211. *
  212. * @since 1.0
  213. */
  214. public function getFriendshipsOutgoing($cursor = null, $string_ids = null)
  215. {
  216. // Check the rate limit for remaining hits
  217. $this->checkRateLimit('friendships', 'outgoing');
  218. $data = array();
  219. // Check if cursor is specified
  220. if (!is_null($cursor))
  221. {
  222. $data['cursor'] = $cursor;
  223. }
  224. // Check if string_ids is specified
  225. if (!is_null($string_ids))
  226. {
  227. $data['stringify_ids'] = $string_ids;
  228. }
  229. // Set the API path
  230. $path = '/friendships/outgoing.json';
  231. // Send the request.
  232. return $this->sendRequest($path, 'GET', $data);
  233. }
  234. /**
  235. * Allows the authenticating users to follow the user specified in the ID parameter.
  236. *
  237. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  238. * @param boolean $follow Enable notifications for the target user.
  239. *
  240. * @return array The decoded JSON response
  241. *
  242. * @since 1.0
  243. * @throws \RuntimeException
  244. */
  245. public function follow($user, $follow = false)
  246. {
  247. // Determine which type of data was passed for $user
  248. if (is_numeric($user))
  249. {
  250. $data['user_id'] = $user;
  251. }
  252. elseif (is_string($user))
  253. {
  254. $data['screen_name'] = $user;
  255. }
  256. else
  257. {
  258. // We don't have a valid entry
  259. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  260. }
  261. // Check if follow is true
  262. if ($follow)
  263. {
  264. $data['follow'] = $follow;
  265. }
  266. // Set the API path
  267. $path = '/friendships/create.json';
  268. // Send the request.
  269. return $this->sendRequest($path, 'POST', $data);
  270. }
  271. /**
  272. * Allows the authenticating users to unfollow the user specified in the ID parameter.
  273. *
  274. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  275. *
  276. * @return array The decoded JSON response
  277. *
  278. * @since 1.0
  279. * @throws \RuntimeException
  280. */
  281. public function unfollow($user)
  282. {
  283. // Determine which type of data was passed for $user
  284. if (is_numeric($user))
  285. {
  286. $data['user_id'] = $user;
  287. }
  288. elseif (is_string($user))
  289. {
  290. $data['screen_name'] = $user;
  291. }
  292. else
  293. {
  294. // We don't have a valid entry
  295. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  296. }
  297. // Set the API path
  298. $path = '/friendships/destroy.json';
  299. // Send the request.
  300. return $this->sendRequest($path, 'POST', $data);
  301. }
  302. /**
  303. * Method to get the relationship of the authenticating user to the comma separated list of up to 100 screen_names or user_ids provided.
  304. *
  305. * @param string $screen_name A comma separated list of screen names, up to 100 are allowed in a single request.
  306. * @param string $id A comma separated list of user IDs, up to 100 are allowed in a single request.
  307. *
  308. * @return array The decoded JSON response
  309. *
  310. * @since 1.0
  311. * @throws \RuntimeException
  312. */
  313. public function getFriendshipsLookup($screen_name = null, $id = null)
  314. {
  315. // Check the rate limit for remaining hits
  316. $this->checkRateLimit('friendships', 'lookup');
  317. $data = array();
  318. // Set user IDs and screen names.
  319. if ($id)
  320. {
  321. $data['user_id'] = $id;
  322. }
  323. if ($screen_name)
  324. {
  325. $data['screen_name'] = $screen_name;
  326. }
  327. if ($id == null && $screen_name == null)
  328. {
  329. // We don't have a valid entry
  330. throw new \RuntimeException('You must specify either a comma separated list of screen names, user IDs, or a combination of the two');
  331. }
  332. // Set the API path
  333. $path = '/friendships/lookup.json';
  334. // Send the request.
  335. return $this->sendRequest($path, 'GET', $data);
  336. }
  337. /**
  338. * Allows one to enable or disable retweets and device notifications from the specified user.
  339. *
  340. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  341. * @param boolean $device Enable/disable device notifications from the target user.
  342. * @param boolean $retweets Enable/disable retweets from the target user.
  343. *
  344. * @return array The decoded JSON response
  345. *
  346. * @since 1.0
  347. * @throws \RuntimeException
  348. */
  349. public function updateFriendship($user, $device = null, $retweets = null)
  350. {
  351. // Determine which type of data was passed for $user
  352. if (is_numeric($user))
  353. {
  354. $data['user_id'] = $user;
  355. }
  356. elseif (is_string($user))
  357. {
  358. $data['screen_name'] = $user;
  359. }
  360. else
  361. {
  362. // We don't have a valid entry
  363. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  364. }
  365. // Check if device is specified.
  366. if (!is_null($device))
  367. {
  368. $data['device'] = $device;
  369. }
  370. // Check if retweets is specified.
  371. if (!is_null($retweets))
  372. {
  373. $data['retweets'] = $retweets;
  374. }
  375. // Set the API path
  376. $path = '/friendships/update.json';
  377. // Send the request.
  378. return $this->sendRequest($path, 'POST', $data);
  379. }
  380. /**
  381. * Method to get the user ids that currently authenticated user does not want to see retweets from.
  382. *
  383. * @param boolean $string_ids Set to true to return IDs as strings, false to return as integers.
  384. *
  385. * @return array The decoded JSON response
  386. *
  387. * @since 1.0
  388. */
  389. public function getFriendshipNoRetweetIds($string_ids = null)
  390. {
  391. // Check the rate limit for remaining hits
  392. $this->checkRateLimit('friendships', 'no_retweets/ids');
  393. $data = array();
  394. // Check if string_ids is specified
  395. if (!is_null($string_ids))
  396. {
  397. $data['stringify_ids'] = $string_ids;
  398. }
  399. // Set the API path
  400. $path = '/friendships/no_retweets/ids.json';
  401. // Send the request.
  402. return $this->sendRequest($path, 'GET', $data);
  403. }
  404. }