Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
object.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Facebook
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 
11 defined('JPATH_PLATFORM') or die();
12 
13 
14 /**
15  * Facebook API object class for the Joomla Platform.
16  *
17  * @package Joomla.Platform
18  * @subpackage Facebook
19  *
20  * @since 13.1
21  */
22 abstract class JFacebookObject
23 {
24  /**
25  * @var JRegistry Options for the Facebook object.
26  * @since 13.1
27  */
28  protected $options;
29 
30  /**
31  * @var JHttp The HTTP client object to use in sending HTTP requests.
32  * @since 13.1
33  */
34  protected $client;
35 
36  /**
37  * @var JFacebookOAuth The OAuth client.
38  * @since 13.1
39  */
40  protected $oauth;
41 
42  /**
43  * Constructor.
44  *
45  * @param JRegistry $options Facebook options object.
46  * @param JHttp $client The HTTP client object.
47  * @param JFacebookOAuth $oauth The OAuth client.
48  *
49  * @since 13.1
50  */
51  public function __construct(JRegistry $options = null, JHttp $client = null, JFacebookOAuth $oauth = null)
52  {
53  $this->options = isset($options) ? $options : new JRegistry;
54  $this->client = isset($client) ? $client : new JHttp($this->options);
55  $this->oauth = $oauth;
56  }
57 
58  /**
59  * Method to build and return a full request URL for the request. This method will
60  * add appropriate pagination details if necessary and also prepend the API url
61  * to have a complete URL for the request.
62  *
63  * @param string $path URL to inflect.
64  * @param integer $limit The number of objects per page.
65  * @param integer $offset The object's number on the page.
66  * @param timestamp $until A unix timestamp or any date accepted by strtotime.
67  * @param timestamp $since A unix timestamp or any date accepted by strtotime.
68  *
69  * @return string The request URL.
70  *
71  * @since 13.1
72  */
73  protected function fetchUrl($path, $limit = 0, $offset = 0, $until = null, $since = null)
74  {
75  // Get a new JUri object fousing the api url and given path.
76  $uri = new JUri($this->options->get('api.url') . $path);
77 
78  if ($limit > 0)
79  {
80  $uri->setVar('limit', (int) $limit);
81  }
82 
83  if ($offset > 0)
84  {
85  $uri->setVar('offset', (int) $offset);
86  }
87 
88  if ($until != null)
89  {
90  $uri->setVar('until', $until);
91  }
92 
93  if ($since != null)
94  {
95  $uri->setVar('since', $since);
96  }
97 
98  return (string) $uri;
99  }
100 
101  /**
102  * Method to send the request.
103  *
104  * @param string $path The path of the request to make.
105  * @param mixed $data Either an associative array or a string to be sent with the post request.
106  * @param array $headers An array of name-value pairs to include in the header of the request
107  * @param integer $limit The number of objects per page.
108  * @param integer $offset The object's number on the page.
109  * @param string $until A unix timestamp or any date accepted by strtotime.
110  * @param string $since A unix timestamp or any date accepted by strtotime.
111  *
112  * @return mixed The request response.
113  *
114  * @since 13.1
115  * @throws DomainException
116  */
117  public function sendRequest($path, $data = '', array $headers = null, $limit = 0, $offset = 0, $until = null, $since = null)
118  {
119  // Send the request.
120  $response = $this->client->get($this->fetchUrl($path, $limit, $offset, $until, $since), $headers);
121 
122  $response = json_decode($response->body);
123 
124  // Validate the response.
125  if (property_exists($response, 'error'))
126  {
127  throw new RuntimeException($response->error->message);
128  }
129 
130  return $response;
131  }
132 
133  /**
134  * Method to get an object.
135  *
136  * @param string $object The object id.
137  *
138  * @return mixed The decoded JSON response or false if the client is not authenticated.
139  *
140  * @since 13.1
141  */
142  public function get($object)
143  {
144  if ($this->oauth != null)
145  {
146  if ($this->oauth->isAuthenticated())
147  {
148  $response = $this->oauth->query($this->fetchUrl($object));
149 
150  return json_decode($response->body);
151  }
152  else
153  {
154  return false;
155  }
156  }
157 
158  // Send the request.
159  return $this->sendRequest($object);
160  }
161 
162  /**
163  * Method to get object's connection.
164  *
165  * @param string $object The object id.
166  * @param string $connection The object's connection name.
167  * @param string $extra_fields URL fields.
168  * @param integer $limit The number of objects per page.
169  * @param integer $offset The object's number on the page.
170  * @param string $until A unix timestamp or any date accepted by strtotime.
171  * @param string $since A unix timestamp or any date accepted by strtotime.
172  *
173  * @return mixed The decoded JSON response or false if the client is not authenticated.
174  *
175  * @since 13.1
176  */
177  public function getConnection($object, $connection = null, $extra_fields = '', $limit = 0, $offset = 0, $until = null, $since = null)
178  {
179  $path = $object . '/' . $connection . $extra_fields;
180 
181  if ($this->oauth != null)
182  {
183  if ($this->oauth->isAuthenticated())
184  {
185  $response = $this->oauth->query($this->fetchUrl($path, $limit, $offset, $until, $since));
186 
187  if (strcmp($response->body, ''))
188  {
189  return json_decode($response->body);
190  }
191  else
192  {
193  return $response->headers['Location'];
194  }
195  }
196  else
197  {
198  return false;
199  }
200  }
201 
202  // Send the request.
203  return $this->sendRequest($path, '', null, $limit, $offset, $until, $since);
204  }
205 
206  /**
207  * Method to create a connection.
208  *
209  * @param string $object The object id.
210  * @param string $connection The object's connection name.
211  * @param array $parameters The POST request parameters.
212  * @param array $headers An array of name-value pairs to include in the header of the request
213  *
214  * @return mixed The decoded JSON response or false if the client is not authenticated.
215  *
216  * @since 13.1
217  */
218  public function createConnection($object, $connection = null, $parameters = null, array $headers = null)
219  {
220  if ($this->oauth->isAuthenticated())
221  {
222  // Build the request path.
223  if ($connection != null)
224  {
225  $path = $object . '/' . $connection;
226  }
227  else
228  {
229  $path = $object;
230  }
231 
232  // Send the post request.
233  $response = $this->oauth->query($this->fetchUrl($path), $parameters, $headers, 'post');
234 
235  return json_decode($response->body);
236  }
237  else
238  {
239  return false;
240  }
241  }
242 
243  /**
244  * Method to delete a connection.
245  *
246  * @param string $object The object id.
247  * @param string $connection The object's connection name.
248  * @param string $extra_fields URL fields.
249  *
250  * @return mixed The decoded JSON response or false if the client is not authenticated.
251  *
252  * @since 13.1
253  */
254  public function deleteConnection($object, $connection = null, $extra_fields = '')
255  {
256  if ($this->oauth->isAuthenticated())
257  {
258  // Build the request path.
259  if ($connection != null)
260  {
261  $path = $object . '/' . $connection . $extra_fields;
262  }
263  else
264  {
265  $path = $object . $extra_fields;
266  }
267 
268  // Send the delete request.
269  $response = $this->oauth->query($this->fetchUrl($path), null, array(), 'delete');
270 
271  return json_decode($response->body);
272  }
273  else
274  {
275  return false;
276  }
277  }
278 
279  /**
280  * Method used to set the OAuth client.
281  *
282  * @param JFacebookOAuth $oauth The OAuth client object.
283  *
284  * @return JFacebookObject This object for method chaining.
285  *
286  * @since 13.1
287  */
288  public function setOAuth($oauth)
289  {
290  $this->oauth = $oauth;
291 
292  return $this;
293  }
294 
295  /**
296  * Method used to get the OAuth client.
297  *
298  * @return JFacebookOAuth The OAuth client
299  *
300  * @since 13.1
301  */
302  public function getOAuth()
303  {
304  return $this->oauth;
305  }
306 }