PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Twitter/Users.php

https://github.com/piotr-cz/joomla-framework
PHP | 377 lines | 167 code | 50 blank | 160 comment | 21 complexity | 0c8a0b6c22e22f1cffa7ee0a16cbe3b7 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 Users class for the Joomla Framework.
  11. *
  12. * @since 1.0
  13. */
  14. class Users extends Object
  15. {
  16. /**
  17. * Method to get up to 100 users worth of extended information, specified by either ID, screen name, or combination of the two.
  18. *
  19. * @param string $screen_name A comma separated list of screen names, up to 100 are allowed in a single request.
  20. * @param string $id A comma separated list of user IDs, up to 100 are allowed in a single request.
  21. * @param boolean $entities When set to either true, t or 1, each tweet will include a node called "entities,". This node offers a variety of
  22. * metadata about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  23. *
  24. * @return array The decoded JSON response
  25. *
  26. * @since 1.0
  27. * @throws \RuntimeException
  28. */
  29. public function getUsersLookup($screen_name = null, $id = null, $entities = null)
  30. {
  31. // Check the rate limit for remaining hits
  32. $this->checkRateLimit('users', 'lookup');
  33. $data = array();
  34. // Set user IDs and screen names.
  35. if ($id)
  36. {
  37. $data['user_id'] = $id;
  38. }
  39. if ($screen_name)
  40. {
  41. $data['screen_name'] = $screen_name;
  42. }
  43. if ($id == null && $screen_name == null)
  44. {
  45. // We don't have a valid entry
  46. throw new \RuntimeException('You must specify either a comma separated list of screen names, user IDs, or a combination of the two');
  47. }
  48. // Set the API path
  49. $path = '/users/lookup.json';
  50. // Check if string_ids is specified
  51. if (!is_null($entities))
  52. {
  53. $data['include_entities'] = $entities;
  54. }
  55. // Send the request.
  56. return $this->sendRequest($path, 'POST', $data);
  57. }
  58. /**
  59. * Method to access the profile banner in various sizes for the user with the indicated screen_name.
  60. *
  61. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  62. *
  63. * @return array The decoded JSON response
  64. *
  65. * @since 1.0
  66. * @throws \RuntimeException
  67. */
  68. public function getUserProfileBanner($user)
  69. {
  70. // Check the rate limit for remaining hits
  71. $this->checkRateLimit('users', 'profile_banner');
  72. // Set the API path
  73. $path = '/users/profile_banner.json';
  74. // Determine which type of data was passed for $user
  75. if (is_numeric($user))
  76. {
  77. $data['user_id'] = $user;
  78. }
  79. elseif (is_string($user))
  80. {
  81. $data['screen_name'] = $user;
  82. }
  83. else
  84. {
  85. // We don't have a valid entry
  86. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  87. }
  88. // Send the request.
  89. return $this->sendRequest($path, 'GET', $data);
  90. }
  91. /**
  92. * Method used to search for users
  93. *
  94. * @param string $query The search query to run against people search.
  95. * @param integer $page Specifies the page of results to retrieve.
  96. * @param integer $count The number of people to retrieve. Maximum of 20 allowed per page.
  97. * @param boolean $entities When set to either true, t or 1, each tweet will include a node called "entities,". This node offers a
  98. * variety of metadata about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  99. *
  100. * @return array The decoded JSON response
  101. *
  102. * @since 1.0
  103. * @throws \RuntimeException
  104. */
  105. public function searchUsers($query, $page = 0, $count = 0, $entities = null)
  106. {
  107. // Check the rate limit for remaining hits
  108. $this->checkRateLimit('users', 'search');
  109. $data['q'] = rawurlencode($query);
  110. // Check if page is specified.
  111. if ($page > 0 )
  112. {
  113. $data['page'] = $page;
  114. }
  115. // Check if per_page is specified
  116. if ($count > 0)
  117. {
  118. $data['count'] = $count;
  119. }
  120. // Check if entities is specified.
  121. if (!is_null($entities))
  122. {
  123. $data['include_entities'] = $entities;
  124. }
  125. // Set the API path
  126. $path = '/users/search.json';
  127. // Send the request.
  128. return $this->sendRequest($path, 'GET', $data);
  129. }
  130. /**
  131. * Method to get extended information of a given user, specified by ID or screen name as per the required id parameter.
  132. *
  133. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  134. * @param boolean $entities Set to true to return IDs as strings, false to return as integers.
  135. *
  136. * @return array The decoded JSON response
  137. *
  138. * @since 1.0
  139. * @throws \RuntimeException
  140. */
  141. public function getUser($user, $entities = null)
  142. {
  143. // Check the rate limit for remaining hits
  144. $this->checkRateLimit('users', 'show');
  145. // Determine which type of data was passed for $user
  146. if (is_numeric($user))
  147. {
  148. $data['user_id'] = $user;
  149. }
  150. elseif (is_string($user))
  151. {
  152. $data['screen_name'] = $user;
  153. }
  154. else
  155. {
  156. // We don't have a valid entry
  157. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  158. }
  159. // Set the API path
  160. $path = '/users/show.json';
  161. // Check if entities is specified
  162. if (!is_null($entities))
  163. {
  164. $data['include_entities'] = $entities;
  165. }
  166. // Send the request.
  167. return $this->sendRequest($path, 'GET', $data);
  168. }
  169. /**
  170. * Method to get an array of users that the specified user can contribute to.
  171. *
  172. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  173. * @param boolean $entities Set to true to return IDs as strings, false to return as integers.
  174. * @param boolean $skip_status When set to either true, t or 1 statuses will not be included in the returned user objects.
  175. *
  176. * @return array The decoded JSON response
  177. *
  178. * @since 1.0
  179. * @throws \RuntimeException
  180. */
  181. public function getContributees($user, $entities = null, $skip_status = null)
  182. {
  183. // Check the rate limit for remaining hits
  184. $this->checkRateLimit('users', 'contributees');
  185. // Determine which type of data was passed for $user
  186. if (is_numeric($user))
  187. {
  188. $data['user_id'] = $user;
  189. }
  190. elseif (is_string($user))
  191. {
  192. $data['screen_name'] = $user;
  193. }
  194. else
  195. {
  196. // We don't have a valid entry
  197. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  198. }
  199. // Set the API path
  200. $path = '/users/contributees.json';
  201. // Check if entities is specified
  202. if (!is_null($entities))
  203. {
  204. $data['include_entities'] = $entities;
  205. }
  206. // Check if skip_status is specified
  207. if (!is_null($skip_status))
  208. {
  209. $data['skip_status'] = $skip_status;
  210. }
  211. // Send the request.
  212. return $this->sendRequest($path, 'GET', $data);
  213. }
  214. /**
  215. * Method to get an array of users who can contribute to the specified account.
  216. *
  217. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  218. * @param boolean $entities Set to true to return IDs as strings, false to return as integers.
  219. * @param boolean $skip_status When set to either true, t or 1 statuses will not be included in the returned user objects.
  220. *
  221. * @return array The decoded JSON response
  222. *
  223. * @since 1.0
  224. * @throws \RuntimeException
  225. */
  226. public function getContributors($user, $entities = null, $skip_status = null)
  227. {
  228. // Check the rate limit for remaining hits
  229. $this->checkRateLimit('users', 'contributors');
  230. // Determine which type of data was passed for $user
  231. if (is_numeric($user))
  232. {
  233. $data['user_id'] = $user;
  234. }
  235. elseif (is_string($user))
  236. {
  237. $data['screen_name'] = $user;
  238. }
  239. else
  240. {
  241. // We don't have a valid entry
  242. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  243. }
  244. // Set the API path
  245. $path = '/users/contributors.json';
  246. // Check if entities is specified
  247. if (!is_null($entities))
  248. {
  249. $data['include_entities'] = $entities;
  250. }
  251. // Check if skip_status is specified
  252. if (!is_null($skip_status))
  253. {
  254. $data['skip_status'] = $skip_status;
  255. }
  256. // Send the request.
  257. return $this->sendRequest($path, 'GET', $data);
  258. }
  259. /**
  260. * Method access to Twitter's suggested user list.
  261. *
  262. * @param boolean $lang Restricts the suggested categories to the requested language.
  263. *
  264. * @return array The decoded JSON response
  265. *
  266. * @since 1.0
  267. */
  268. public function getSuggestions($lang = null)
  269. {
  270. // Check the rate limit for remaining hits
  271. $this->checkRateLimit('users', 'suggestions');
  272. // Set the API path
  273. $path = '/users/suggestions.json';
  274. $data = array();
  275. // Check if entities is true
  276. if ($lang)
  277. {
  278. $data['lang'] = $lang;
  279. }
  280. // Send the request.
  281. return $this->sendRequest($path, 'GET', $data);
  282. }
  283. /**
  284. * method to access the users in a given category of the Twitter suggested user list.
  285. *
  286. * @param string $slug The short name of list or a category.
  287. * @param boolean $lang Restricts the suggested categories to the requested language.
  288. *
  289. * @return array The decoded JSON response
  290. *
  291. * @since 1.0
  292. */
  293. public function getSuggestionsSlug($slug, $lang = null)
  294. {
  295. // Check the rate limit for remaining hits
  296. $this->checkRateLimit('users', 'suggestions/:slug');
  297. // Set the API path
  298. $path = '/users/suggestions/' . $slug . '.json';
  299. $data = array();
  300. // Check if entities is true
  301. if ($lang)
  302. {
  303. $data['lang'] = $lang;
  304. }
  305. // Send the request.
  306. return $this->sendRequest($path, 'GET', $data);
  307. }
  308. /**
  309. * Method to access the users in a given category of the Twitter suggested user list and return
  310. * their most recent status if they are not a protected user.
  311. *
  312. * @param string $slug The short name of list or a category.
  313. *
  314. * @return array The decoded JSON response
  315. *
  316. * @since 1.0
  317. */
  318. public function getSuggestionsSlugMembers($slug)
  319. {
  320. // Check the rate limit for remaining hits
  321. $this->checkRateLimit('users', 'suggestions/:slug/members');
  322. // Set the API path
  323. $path = '/users/suggestions/' . $slug . '/members.json';
  324. // Send the request.
  325. return $this->sendRequest($path);
  326. }
  327. }