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