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 Openstreetmap
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  * Openstreetmap API object class for the Joomla Platform
14  *
15  * @package Joomla.Platform
16  * @subpackage Openstreetmap
17  * @since 13.1
18  */
19 abstract class JOpenstreetmapObject
20 {
21  /**
22  * Options for the Openstreetmap object.
23  *
24  * @var JRegistry
25  * @since 13.1
26  */
27  protected $options;
28 
29  /**
30  * The HTTP client object to use in sending HTTP requests.
31  *
32  * @var JHttp
33  * @since 13.1
34  */
35  protected $client;
36 
37  /**
38  * The OAuth client.
39  *
40  * @var JOpenstreetmapOauth
41  * @since 13.1
42  */
43  protected $oauth;
44 
45  /**
46  * Constructor
47  *
48  * @param JRegistry &$options Openstreetmap options object.
49  * @param JHttp $client The HTTP client object.
50  * @param JOpenstreetmapOauth $oauth Openstreetmap oauth client
51  *
52  * @since 13.1
53  */
54  public function __construct(JRegistry &$options = null, JHttp $client = null, JOpenstreetmapOauth $oauth = null)
55  {
56  $this->options = isset($options) ? $options : new JRegistry;
57  $this->client = isset($client) ? $client : new JHttp($this->options);
58  $this->oauth = $oauth;
59  }
60 
61  /**
62  * Get an option from the JOpenstreetmapObject instance.
63  *
64  * @param string $key The name of the option to get.
65  *
66  * @return mixed The option value.
67  *
68  * @since 13.1
69  */
70  public function getOption($key)
71  {
72  return $this->options->get($key);
73  }
74 
75  /**
76  * Set an option for the JOpenstreetmapObject instance.
77  *
78  * @param string $key The name of the option to set.
79  * @param mixed $value The option value to set.
80  *
81  * @return JOpenstreetmapObject This object for method chaining.
82  *
83  * @since 13.1
84  */
85  public function setOption($key, $value)
86  {
87  $this->options->set($key, $value);
88 
89  return $this;
90  }
91 
92  /**
93  * Method to send the request which does not require authentication.
94  *
95  * @param string $path The path of the request to make
96  * @param string $method The request method.
97  * @param array $headers The headers passed in the request.
98  * @param mixed $data Either an associative array or a string to be sent with the post request.
99  *
100  * @return SimpleXMLElement The XML response
101  *
102  * @since 13.1
103  * @throws DomainException
104  */
105  public function sendRequest($path, $method = 'GET', $headers = array(), $data = '')
106  {
107  // Send the request.
108  switch ($method)
109  {
110  case 'GET':
111  $response = $this->client->get($path, $headers);
112  break;
113 
114  case 'POST':
115  $response = $this->client->post($path, $data, $headers);
116  break;
117  }
118 
119  // Validate the response code.
120  if ($response->code != 200)
121  {
122  $error = htmlspecialchars($response->body);
123 
124  throw new DomainException($error, $response->code);
125  }
126 
127  $xml_string = simplexml_load_string($response->body);
128 
129  return $xml_string;
130  }
131 }