miniflux-legacy/vendor/fguillot/json-rpc/src/JsonRPC/Client.php

202 lines
4.1 KiB
PHP
Raw Normal View History

2013-07-28 21:44:51 +02:00
<?php
namespace JsonRPC;
2015-08-15 03:33:39 +02:00
use Exception;
2016-05-08 21:53:45 +02:00
use JsonRPC\Request\RequestBuilder;
use JsonRPC\Response\ResponseParser;
2015-08-15 03:33:39 +02:00
/**
* JsonRPC client class
*
* @package JsonRPC
2015-08-15 03:33:39 +02:00
* @author Frederic Guillot
*/
2013-07-28 21:44:51 +02:00
class Client
{
/**
2016-05-08 21:53:45 +02:00
* If the only argument passed to a function is an array
* assume it contains named arguments
*
* @access private
2016-05-08 21:53:45 +02:00
* @var boolean
*/
2016-05-08 21:53:45 +02:00
private $isNamedArguments = true;
2015-06-21 15:56:36 +02:00
/**
2016-05-08 21:53:45 +02:00
* Do not immediately throw an exception on error. Return it instead.
2015-06-21 15:56:36 +02:00
*
* @access public
* @var boolean
*/
2016-05-08 21:53:45 +02:00
private $returnException = false;
2015-06-21 15:56:36 +02:00
/**
2016-05-08 21:53:45 +02:00
* True for a batch request
*
* @access private
2016-05-08 21:53:45 +02:00
* @var boolean
*/
2016-05-08 21:53:45 +02:00
private $isBatch = false;
/**
2016-05-08 21:53:45 +02:00
* Batch payload
*
* @access private
2016-05-08 21:53:45 +02:00
* @var array
*/
2016-05-08 21:53:45 +02:00
private $batch = array();
/**
2016-05-08 21:53:45 +02:00
* Http Client
*
* @access private
2016-05-08 21:53:45 +02:00
* @var HttpClient
*/
2016-05-08 21:53:45 +02:00
private $httpClient;
/**
2016-05-08 21:53:45 +02:00
* Constructor
*
* @access public
2016-05-08 21:53:45 +02:00
* @param string $url Server URL
* @param bool $returnException Return exceptions
* @param HttpClient $httpClient HTTP client object
*/
2016-05-08 21:53:45 +02:00
public function __construct($url = '', $returnException = false, HttpClient $httpClient = null)
{
$this->httpClient = $httpClient ?: new HttpClient($url);
$this->returnException = $returnException;
}
/**
2016-05-08 21:53:45 +02:00
* Arguments passed are always positional
*
* @access public
2016-05-08 21:53:45 +02:00
* @return $this
*/
2016-05-08 21:53:45 +02:00
public function withPositionalArguments()
{
$this->isNamedArguments = false;
return $this;
}
2015-08-15 03:33:39 +02:00
/**
2016-05-08 21:53:45 +02:00
* Get HTTP Client
2015-08-15 03:33:39 +02:00
*
* @access public
2016-05-08 21:53:45 +02:00
* @return HttpClient
*/
2016-05-08 21:53:45 +02:00
public function getHttpClient()
{
return $this->httpClient;
}
2013-07-28 21:44:51 +02:00
/**
2016-05-08 21:53:45 +02:00
* Set username and password
*
* @access public
2016-05-08 21:53:45 +02:00
* @param string $username
* @param string $password
* @return $this
*/
2016-05-08 21:53:45 +02:00
public function authentication($username, $password)
2013-07-28 21:44:51 +02:00
{
2016-05-08 21:53:45 +02:00
$this->httpClient
->withUsername($username)
->withPassword($password);
return $this;
2013-07-28 21:44:51 +02:00
}
/**
* Automatic mapping of procedures
*
* @access public
* @param string $method Procedure name
* @param array $params Procedure arguments
* @return mixed
*/
public function __call($method, array $params)
2014-02-08 20:13:14 +01:00
{
2016-05-08 21:53:45 +02:00
if ($this->isNamedArguments && count($params) === 1 && is_array($params[0])) {
$params = $params[0];
}
2014-02-08 20:13:14 +01:00
return $this->execute($method, $params);
}
/**
* Start a batch request
*
* @access public
* @return Client
*/
public function batch()
{
2016-05-08 21:53:45 +02:00
$this->isBatch = true;
$this->batch = array();
return $this;
}
/**
* Send a batch request
*
* @access public
* @return array
*/
public function send()
{
2016-05-08 21:53:45 +02:00
$this->isBatch = false;
return $this->sendPayload('['.implode(', ', $this->batch).']');
}
/**
* Execute a procedure
*
* @access public
* @param string $procedure Procedure name
* @param array $params Procedure arguments
* @return mixed
*/
2013-07-28 21:44:51 +02:00
public function execute($procedure, array $params = array())
{
2016-05-08 21:53:45 +02:00
$payload = RequestBuilder::create()
->withProcedure($procedure)
->withParams($params)
->build();
2013-07-28 21:44:51 +02:00
2016-05-08 21:53:45 +02:00
if ($this->isBatch) {
$this->batch[] = $payload;
return $this;
}
2016-05-08 21:53:45 +02:00
return $this->sendPayload($payload);
2013-07-28 21:44:51 +02:00
}
2015-08-15 03:33:39 +02:00
/**
2016-05-08 21:53:45 +02:00
* Send payload
2015-08-15 03:33:39 +02:00
*
* @access private
2016-05-08 21:53:45 +02:00
* @throws Exception
* @param string $payload
* @return Exception|Client
2015-08-15 03:33:39 +02:00
*/
2016-05-08 21:53:45 +02:00
private function sendPayload($payload)
2015-08-15 03:33:39 +02:00
{
2016-05-08 21:53:45 +02:00
try {
2015-08-15 03:33:39 +02:00
2016-05-08 21:53:45 +02:00
return ResponseParser::create()
->withPayload($this->httpClient->execute($payload))
->parse();
2015-08-15 03:33:39 +02:00
2016-05-08 21:53:45 +02:00
} catch (Exception $e) {
if ($this->returnException) {
return $e;
}
2015-08-15 03:33:39 +02:00
2016-05-08 21:53:45 +02:00
throw $e;
2015-08-15 03:33:39 +02:00
}
}
2013-07-28 21:44:51 +02:00
}