2014-11-06 02:44:52 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\Response;
|
|
|
|
|
|
|
|
use RetailCrm\Exception\InvalidJsonException;
|
|
|
|
|
|
|
|
/**
|
2016-03-09 02:31:29 +03:00
|
|
|
* PHP version 5.3
|
|
|
|
*
|
2014-11-06 02:44:52 +03:00
|
|
|
* Response from retailCRM API
|
2016-03-09 02:31:29 +03:00
|
|
|
*
|
|
|
|
* @category RetailCrm
|
|
|
|
* @package RetailCrm
|
|
|
|
* @author RetailCrm <integration@retailcrm.ru>
|
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
|
|
* @link http://www.retailcrm.ru/docs/Developers/ApiVersion3
|
2014-11-06 02:44:52 +03:00
|
|
|
*/
|
|
|
|
class ApiResponse implements \ArrayAccess
|
|
|
|
{
|
|
|
|
// HTTP response status code
|
|
|
|
protected $statusCode;
|
|
|
|
|
|
|
|
// response assoc array
|
|
|
|
protected $response;
|
|
|
|
|
2016-03-09 02:31:29 +03:00
|
|
|
/**
|
|
|
|
* ApiResponse constructor.
|
|
|
|
*
|
|
|
|
* @param int $statusCode HTTP status code
|
|
|
|
* @param mixed $responseBody HTTP body
|
|
|
|
*/
|
2014-11-06 02:44:52 +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
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($name)
|
|
|
|
{
|
|
|
|
if (!isset($this->response[$name])) {
|
|
|
|
throw new \InvalidArgumentException("Property \"$name\" not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response[$name];
|
|
|
|
}
|
|
|
|
|
2014-11-10 03:12:50 +03:00
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('This activity not allowed');
|
|
|
|
}
|
|
|
|
|
2014-11-10 03:12:50 +03:00
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('This call not allowed');
|
|
|
|
}
|
|
|
|
|
2014-11-10 03:12:50 +03:00
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return isset($this->response[$offset]);
|
|
|
|
}
|
|
|
|
|
2014-11-10 03:12:50 +03:00
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2014-11-06 02:44:52 +03:00
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
if (!isset($this->response[$offset])) {
|
|
|
|
throw new \InvalidArgumentException("Property \"$offset\" not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response[$offset];
|
|
|
|
}
|
2014-11-10 03:12:50 +03:00
|
|
|
}
|