2015-03-03 16:37:34 +03:00
|
|
|
<?php
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
/**
|
2020-12-15 12:39:38 +03:00
|
|
|
* @category RetailCRM
|
2024-04-29 11:44:31 +03:00
|
|
|
* @package RetailCRM\Response
|
|
|
|
* @author RetailCRM <integration@retailcrm.ru>
|
|
|
|
* @license MIT
|
|
|
|
* @link http://retailcrm.ru
|
|
|
|
* @see http://retailcrm.ru/docs
|
2016-09-15 16:42:10 +03:00
|
|
|
*/
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
namespace RetailCrm\Response;
|
|
|
|
|
2021-08-19 15:46:03 +03:00
|
|
|
use InvalidArgumentException;
|
2015-03-03 16:37:34 +03:00
|
|
|
use RetailCrm\Exception\InvalidJsonException;
|
|
|
|
|
|
|
|
/**
|
2024-04-29 11:44:31 +03:00
|
|
|
* Class ApiResponse
|
2016-09-15 16:42:10 +03:00
|
|
|
*
|
2020-12-15 12:39:38 +03:00
|
|
|
* @category RetailCRM
|
2024-04-29 11:44:31 +03:00
|
|
|
* @package RetailCRM\Response
|
2015-03-03 16:37:34 +03:00
|
|
|
*/
|
|
|
|
class ApiResponse implements \ArrayAccess
|
|
|
|
{
|
|
|
|
// HTTP response status code
|
|
|
|
protected $statusCode;
|
|
|
|
|
|
|
|
// response assoc array
|
|
|
|
protected $response;
|
|
|
|
|
2016-09-15 16:42:10 +03:00
|
|
|
/**
|
|
|
|
* ApiResponse constructor.
|
|
|
|
*
|
|
|
|
* @param int $statusCode HTTP status code
|
|
|
|
* @param mixed $responseBody HTTP body
|
|
|
|
*
|
|
|
|
* @throws InvalidJsonException
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
public function __construct($statusCode, $responseBody = null)
|
|
|
|
{
|
|
|
|
$this->statusCode = (int) $statusCode;
|
|
|
|
|
|
|
|
if (!empty($responseBody)) {
|
|
|
|
$response = json_decode($responseBody, true);
|
|
|
|
|
|
|
|
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
|
|
|
|
throw new InvalidJsonException(
|
|
|
|
"Invalid JSON in the API response body. Error code #$error",
|
|
|
|
$error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return HTTP response status code
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStatusCode()
|
|
|
|
{
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HTTP request was successful
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isSuccessful()
|
|
|
|
{
|
|
|
|
return $this->statusCode < 400;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow to access for the property throw class method
|
|
|
|
*
|
2016-09-15 16:42:10 +03:00
|
|
|
* @param string $name method name
|
|
|
|
* @param mixed $arguments method parameters
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __call($name, $arguments)
|
|
|
|
{
|
|
|
|
// convert getSomeProperty to someProperty
|
|
|
|
$propertyName = strtolower(substr($name, 3, 1)) . substr($name, 4);
|
|
|
|
|
|
|
|
if (!isset($this->response[$propertyName])) {
|
|
|
|
throw new \InvalidArgumentException("Method \"$name\" not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response[$propertyName];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow to access for the property throw object property
|
|
|
|
*
|
2016-09-15 16:42:10 +03:00
|
|
|
* @param string $name property name
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->response[$name])) {
|
|
|
|
throw new \InvalidArgumentException("Property \"$name\" not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-15 16:42:10 +03:00
|
|
|
* Offset set
|
|
|
|
*
|
|
|
|
* @param mixed $offset offset
|
|
|
|
* @param mixed $value value
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
* @return void
|
2015-03-03 16:37:34 +03:00
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('This activity not allowed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-15 16:42:10 +03:00
|
|
|
* Offset unset
|
|
|
|
*
|
|
|
|
* @param mixed $offset offset
|
|
|
|
*
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
* @return void
|
2015-03-03 16:37:34 +03:00
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('This call not allowed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-15 16:42:10 +03:00
|
|
|
* Check offset
|
|
|
|
*
|
|
|
|
* @param mixed $offset offset
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return isset($this->response[$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-15 16:42:10 +03:00
|
|
|
* Get offset
|
|
|
|
*
|
|
|
|
* @param mixed $offset offset
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
if (!isset($this->response[$offset])) {
|
2021-08-19 15:46:03 +03:00
|
|
|
throw new InvalidArgumentException("Property \"$offset\" not found");
|
2015-03-03 16:37:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response[$offset];
|
|
|
|
}
|
2022-03-02 15:40:53 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getResponseBody()
|
|
|
|
{
|
|
|
|
return $this->response;
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
}
|