*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Rest / Client.php
blob127e5dd9214b31372ea0984aa0be8ca7b8e08f71
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Rest
17 * @subpackage Client
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Client.php 16541 2009-07-07 06:59:03Z bkarwin $
24 /** Zend_Service_Abstract */
25 require_once 'Zend/Service/Abstract.php';
27 /** Zend_Rest_Client_Result */
28 require_once 'Zend/Rest/Client/Result.php';
30 /** Zend_Uri */
31 require_once 'Zend/Uri.php';
33 /**
34 * @category Zend
35 * @package Zend_Rest
36 * @subpackage Client
37 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
38 * @license http://framework.zend.com/license/new-bsd New BSD License
40 class Zend_Rest_Client extends Zend_Service_Abstract
42 /**
43 * Data for the query
44 * @var array
46 protected $_data = array();
48 /**
49 * Zend_Uri of this web service
50 * @var Zend_Uri_Http
52 protected $_uri = null;
54 /**
55 * Constructor
57 * @param string|Zend_Uri_Http $uri URI for the web service
58 * @return void
60 public function __construct($uri = null)
62 if (!empty($uri)) {
63 $this->setUri($uri);
67 /**
68 * Set the URI to use in the request
70 * @param string|Zend_Uri_Http $uri URI for the web service
71 * @return Zend_Rest_Client
73 public function setUri($uri)
75 if ($uri instanceof Zend_Uri_Http) {
76 $this->_uri = $uri;
77 } else {
78 $this->_uri = Zend_Uri::factory($uri);
81 return $this;
84 /**
85 * Retrieve the current request URI object
87 * @return Zend_Uri_Http
89 public function getUri()
91 return $this->_uri;
94 /**
95 * Call a remote REST web service URI and return the Zend_Http_Response object
97 * @param string $path The path to append to the URI
98 * @throws Zend_Rest_Client_Exception
99 * @return void
101 final private function _prepareRest($path)
103 // Get the URI object and configure it
104 if (!$this->_uri instanceof Zend_Uri_Http) {
105 require_once 'Zend/Rest/Client/Exception.php';
106 throw new Zend_Rest_Client_Exception('URI object must be set before performing call');
109 $uri = $this->_uri->getUri();
111 if ($path[0] != '/' && $uri[strlen($uri)-1] != '/') {
112 $path = '/' . $path;
115 $this->_uri->setPath($path);
118 * Get the HTTP client and configure it for the endpoint URI. Do this each time
119 * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses.
121 self::getHttpClient()->resetParameters()->setUri($this->_uri);
125 * Performs an HTTP GET request to the $path.
127 * @param string $path
128 * @param array $query Array of GET parameters
129 * @return Zend_Http_Response
131 final public function restGet($path, array $query = null)
133 $this->_prepareRest($path);
134 $client = self::getHttpClient();
135 $client->setParameterGet($query);
136 return $client->request('GET');
140 * Perform a POST or PUT
142 * Performs a POST or PUT request. Any data provided is set in the HTTP
143 * client. String data is pushed in as raw POST data; array or object data
144 * is pushed in as POST parameters.
146 * @param mixed $method
147 * @param mixed $data
148 * @return Zend_Http_Response
150 protected function _performPost($method, $data = null)
152 $client = self::getHttpClient();
153 if (is_string($data)) {
154 $client->setRawData($data);
155 } elseif (is_array($data) || is_object($data)) {
156 $client->setParameterPost((array) $data);
158 return $client->request($method);
162 * Performs an HTTP POST request to $path.
164 * @param string $path
165 * @param mixed $data Raw data to send
166 * @return Zend_Http_Response
168 final public function restPost($path, $data = null)
170 $this->_prepareRest($path);
171 return $this->_performPost('POST', $data);
175 * Performs an HTTP PUT request to $path.
177 * @param string $path
178 * @param mixed $data Raw data to send in request
179 * @return Zend_Http_Response
181 final public function restPut($path, $data = null)
183 $this->_prepareRest($path);
184 return $this->_performPost('PUT', $data);
188 * Performs an HTTP DELETE request to $path.
190 * @param string $path
191 * @return Zend_Http_Response
193 final public function restDelete($path)
195 $this->_prepareRest($path);
196 return self::getHttpClient()->request('DELETE');
200 * Method call overload
202 * Allows calling REST actions as object methods; however, you must
203 * follow-up by chaining the request with a request to an HTTP request
204 * method (post, get, delete, put):
205 * <code>
206 * $response = $rest->sayHello('Foo', 'Manchu')->get();
207 * </code>
209 * Or use them together, but in sequential calls:
210 * <code>
211 * $rest->sayHello('Foo', 'Manchu');
212 * $response = $rest->get();
213 * </code>
215 * @param string $method Method name
216 * @param array $args Method args
217 * @return Zend_Rest_Client_Result|Zend_Rest_Client Zend_Rest_Client if using
218 * a remote method, Zend_Rest_Client_Result if using an HTTP request method
220 public function __call($method, $args)
222 $methods = array('post', 'get', 'delete', 'put');
224 if (in_array(strtolower($method), $methods)) {
225 if (!isset($args[0])) {
226 $args[0] = $this->_uri->getPath();
228 $this->_data['rest'] = 1;
229 $data = array_slice($args, 1) + $this->_data;
230 $response = $this->{'rest' . $method}($args[0], $data);
231 $this->_data = array();//Initializes for next Rest method.
232 return new Zend_Rest_Client_Result($response->getBody());
233 } else {
234 // More than one arg means it's definitely a Zend_Rest_Server
235 if (sizeof($args) == 1) {
236 // Uses first called function name as method name
237 if (!isset($this->_data['method'])) {
238 $this->_data['method'] = $method;
239 $this->_data['arg1'] = $args[0];
241 $this->_data[$method] = $args[0];
242 } else {
243 $this->_data['method'] = $method;
244 if (sizeof($args) > 0) {
245 foreach ($args as $key => $arg) {
246 $key = 'arg' . $key;
247 $this->_data[$key] = $arg;
251 return $this;