PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Joomla/Twitter/Directmessages.php

https://github.com/piotr-cz/joomla-framework
PHP | 221 lines | 94 code | 31 blank | 96 comment | 12 complexity | a69274328d9c2c34f34dc89d6b5e43b5 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 Direct Messages class for the Joomla Framework.
  11. *
  12. * @since 1.0
  13. */
  14. class Directmessages extends Object
  15. {
  16. /**
  17. * Method to get the most recent direct messages sent to the authenticating user.
  18. *
  19. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  20. * @param integer $max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
  21. * @param integer $count Specifies the number of direct messages to try and retrieve, up to a maximum of 200.
  22. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  23. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  24. * @param boolean $skip_status When set to either true, t or 1 statuses will not be included in the returned user objects.
  25. *
  26. * @return array The decoded JSON response
  27. *
  28. * @since 1.0
  29. */
  30. public function getDirectMessages($since_id = 0, $max_id = 0, $count = 20, $entities = null, $skip_status = null)
  31. {
  32. // Check the rate limit for remaining hits
  33. $this->checkRateLimit('direct_messages');
  34. // Set the API path
  35. $path = '/direct_messages.json';
  36. $data = array();
  37. // Check if since_id is specified.
  38. if ($since_id)
  39. {
  40. $data['since_id'] = $since_id;
  41. }
  42. // Check if max_id is specified.
  43. if ($max_id)
  44. {
  45. $data['max_id'] = $max_id;
  46. }
  47. // Check if count is specified.
  48. if ($count)
  49. {
  50. $data['count'] = $count;
  51. }
  52. // Check if entities is specified.
  53. if (!is_null($entities))
  54. {
  55. $data['include_entities'] = $entities;
  56. }
  57. // Check if skip_status is specified.
  58. if (!is_null($skip_status))
  59. {
  60. $data['skip_status'] = $skip_status;
  61. }
  62. // Send the request.
  63. return $this->sendRequest($path, 'GET', $data);
  64. }
  65. /**
  66. * Method to get the most recent direct messages sent by the authenticating user.
  67. *
  68. * @param integer $since_id Returns results with an ID greater than (that is, more recent than) the specified ID.
  69. * @param integer $max_id Returns results with an ID less than (that is, older than) or equal to the specified ID.
  70. * @param integer $count Specifies the number of direct messages to try and retrieve, up to a maximum of 200.
  71. * @param integer $page Specifies the page of results to retrieve.
  72. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  73. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  74. *
  75. * @return array The decoded JSON response
  76. *
  77. * @since 1.0
  78. */
  79. public function getSentDirectMessages($since_id = 0, $max_id = 0, $count = 20, $page = 0, $entities = null)
  80. {
  81. // Check the rate limit for remaining hits
  82. $this->checkRateLimit('direct_messages', 'sent');
  83. // Set the API path
  84. $path = '/direct_messages/sent.json';
  85. $data = array();
  86. // Check if since_id is specified.
  87. if ($since_id)
  88. {
  89. $data['since_id'] = $since_id;
  90. }
  91. // Check if max_id is specified.
  92. if ($max_id)
  93. {
  94. $data['max_id'] = $max_id;
  95. }
  96. // Check if count is specified.
  97. if ($count)
  98. {
  99. $data['count'] = $count;
  100. }
  101. // Check if page is specified.
  102. if ($page)
  103. {
  104. $data['page'] = $page;
  105. }
  106. // Check if entities is specified.
  107. if (!is_null($entities))
  108. {
  109. $data['include_entities'] = $entities;
  110. }
  111. // Send the request.
  112. return $this->sendRequest($path, 'GET', $data);
  113. }
  114. /**
  115. * Method to send a new direct message to the specified user from the authenticating user.
  116. *
  117. * @param mixed $user Either an integer containing the user ID or a string containing the screen name.
  118. * @param string $text The text of your direct message. Be sure to keep the message under 140 characters.
  119. *
  120. * @return array The decoded JSON response
  121. *
  122. * @since 1.0
  123. * @throws \RuntimeException
  124. */
  125. public function sendDirectMessages($user, $text)
  126. {
  127. // Set the API path
  128. $path = '/direct_messages/new.json';
  129. // Determine which type of data was passed for $user
  130. if (is_numeric($user))
  131. {
  132. $data['user_id'] = $user;
  133. }
  134. elseif (is_string($user))
  135. {
  136. $data['screen_name'] = $user;
  137. }
  138. else
  139. {
  140. // We don't have a valid entry
  141. throw new \RuntimeException('The specified username is not in the correct format; must use integer or string');
  142. }
  143. $data['text'] = $text;
  144. // Send the request.
  145. return $this->sendRequest($path, 'POST', $data);
  146. }
  147. /**
  148. * Method to get a single direct message, specified by an id parameter.
  149. *
  150. * @param integer $id The ID of the direct message.
  151. *
  152. * @return array The decoded JSON response
  153. *
  154. * @since 1.0
  155. */
  156. public function getDirectMessagesById($id)
  157. {
  158. // Check the rate limit for remaining hits
  159. $this->checkRateLimit('direct_messages', 'show');
  160. // Set the API path
  161. $path = '/direct_messages/show.json';
  162. $data['id'] = $id;
  163. // Send the request.
  164. return $this->sendRequest($path, 'GET', $data);
  165. }
  166. /**
  167. * Method to delete the direct message specified in the required ID parameter.
  168. *
  169. * @param integer $id The ID of the direct message.
  170. * @param boolean $entities When set to true, each tweet will include a node called "entities,". This node offers a variety of metadata
  171. * about the tweet in a discreet structure, including: user_mentions, urls, and hashtags.
  172. *
  173. * @return array The decoded JSON response
  174. *
  175. * @since 1.0
  176. */
  177. public function deleteDirectMessages($id, $entities = null)
  178. {
  179. // Set the API path
  180. $path = '/direct_messages/destroy.json';
  181. $data['id'] = $id;
  182. // Check if entities is specified.
  183. if (!is_null($entities))
  184. {
  185. $data['include_entities'] = $entities;
  186. }
  187. // Send the request.
  188. return $this->sendRequest($path, 'POST', $data);
  189. }
  190. }