Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
data.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Google
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 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Google API data class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Google
17  * @since 12.3
18  */
19 abstract class JGoogleData
20 {
21  /**
22  * @var JRegistry Options for the Google data object.
23  * @since 12.3
24  */
25  protected $options;
26 
27  /**
28  * @var JGoogleAuth Authentication client for the Google data object.
29  * @since 12.3
30  */
31  protected $auth;
32 
33  /**
34  * Constructor.
35  *
36  * @param JRegistry $options Google options object.
37  * @param JGoogleAuth $auth Google data http client object.
38  *
39  * @since 12.3
40  */
41  public function __construct(JRegistry $options = null, JGoogleAuth $auth = null)
42  {
43  $this->options = isset($options) ? $options : new JRegistry;
44  $this->auth = isset($auth) ? $auth : new JGoogleAuthOauth2($this->options);
45  }
46 
47  /**
48  * Method to authenticate to Google
49  *
50  * @return boolean True on success.
51  *
52  * @since 12.3
53  */
54  public function authenticate()
55  {
56  return $this->auth->authenticate();
57  }
58 
59  /**
60  * Check authentication
61  *
62  * @return boolean True if authenticated.
63  *
64  * @since 12.3
65  */
66  public function isAuthenticated()
67  {
68  return $this->auth->isAuthenticated();
69  }
70 
71  /**
72  * Method to validate XML
73  *
74  * @param string $data XML data to be parsed
75  *
76  * @return SimpleXMLElement XMLElement of parsed data
77  *
78  * @since 12.3
79  * @throws UnexpectedValueException
80  */
81  protected static function safeXML($data)
82  {
83  try
84  {
85  return new SimpleXMLElement($data, LIBXML_NOWARNING | LIBXML_NOERROR);
86  }
87  catch (Exception $e)
88  {
89  throw new UnexpectedValueException("Unexpected data received from Google: `$data`.");
90  }
91  }
92 
93  /**
94  * Method to retrieve a list of data
95  *
96  * @param array $url URL to GET
97  * @param int $maxpages Maximum number of pages to return
98  * @param string $token Next page token
99  *
100  * @return mixed Data from Google
101  *
102  * @since 12.3
103  * @throws UnexpectedValueException
104  */
105  protected function listGetData($url, $maxpages = 1, $token = null)
106  {
107  $qurl = $url;
108 
109  if (strpos($url, '&') && isset($token))
110  {
111  $qurl .= '&pageToken=' . $token;
112  }
113  elseif (isset($token))
114  {
115  $qurl .= 'pageToken=' . $token;
116  }
117  $jdata = $this->query($qurl);
118  $data = json_decode($jdata->body, true);
119 
120  if ($data && array_key_exists('items', $data))
121  {
122  if ($maxpages != 1 && array_key_exists('nextPageToken', $data))
123  {
124  $data['items'] = array_merge($data['items'], $this->listGetData($url, $maxpages - 1, $data['nextPageToken']));
125  }
126  return $data['items'];
127  }
128  elseif ($data)
129  {
130  return array();
131  }
132  else
133  {
134  throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
135  }
136  }
137 
138  /**
139  * Method to retrieve data from Google
140  *
141  * @param string $url The URL for the request.
142  * @param mixed $data The data to include in the request.
143  * @param array $headers The headers to send with the request.
144  * @param string $method The type of http request to send.
145  *
146  * @return mixed Data from Google.
147  *
148  * @since 12.3
149  */
150  protected function query($url, $data = null, $headers = null, $method = 'get')
151  {
152  return $this->auth->query($url, $data, $headers, $method);
153  }
154 
155  /**
156  * Get an option from the JGoogleData instance.
157  *
158  * @param string $key The name of the option to get.
159  *
160  * @return mixed The option value.
161  *
162  * @since 12.3
163  */
164  public function getOption($key)
165  {
166  return $this->options->get($key);
167  }
168 
169  /**
170  * Set an option for the JGoogleData instance.
171  *
172  * @param string $key The name of the option to set.
173  * @param mixed $value The option value to set.
174  *
175  * @return JGoogleData This object for method chaining.
176  *
177  * @since 12.3
178  */
179  public function setOption($key, $value)
180  {
181  $this->options->set($key, $value);
182 
183  return $this;
184  }
185 }