mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-03-02 19:33:14 +03:00
202 lines
5.2 KiB
PHP
202 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
*
|
|
* @author DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
|
|
* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
|
|
* @license https://opensource.org/licenses/MIT The MIT License
|
|
*
|
|
* Don't forget to prefix your containers with your own identifier
|
|
* to avoid any conflicts with others containers.
|
|
*/
|
|
|
|
class RetailcrmApiResponse implements \ArrayAccess
|
|
{
|
|
// HTTP response status code
|
|
private $statusCode;
|
|
|
|
private $response;
|
|
|
|
private $rawResponse;
|
|
|
|
/**
|
|
* ApiResponse constructor.
|
|
*
|
|
* @param int $statusCode HTTP status code
|
|
* @param mixed $responseBody HTTP body
|
|
*
|
|
* @throws InvalidJsonException
|
|
*/
|
|
public function __construct($statusCode, $responseBody = null)
|
|
{
|
|
$this->statusCode = (int) $statusCode;
|
|
$this->rawResponse = $responseBody;
|
|
|
|
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 mixed|null
|
|
*/
|
|
public function getRawResponse()
|
|
{
|
|
return $this->rawResponse;
|
|
}
|
|
|
|
/**
|
|
* Return HTTP response status code
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getStatusCode()
|
|
{
|
|
return $this->statusCode;
|
|
}
|
|
|
|
/**
|
|
* HTTP request was successful
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isSuccessful()
|
|
{
|
|
return 400 > $this->statusCode;
|
|
}
|
|
|
|
/**
|
|
* Allow to access for the property throw class method
|
|
*
|
|
* @param string $name method name
|
|
* @param mixed $arguments method parameters
|
|
*
|
|
* @return mixed
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
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 property name
|
|
*
|
|
* @return mixed
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
public function __get($name)
|
|
{
|
|
if (!isset($this->response[$name])) {
|
|
throw new \InvalidArgumentException("Property \"$name\" not found");
|
|
}
|
|
|
|
return $this->response[$name];
|
|
}
|
|
|
|
/**
|
|
* Offset set
|
|
*
|
|
* @param mixed $offset offset
|
|
* @param mixed $value value
|
|
*
|
|
* @return void
|
|
*
|
|
* @throws \BadMethodCallException
|
|
*/
|
|
public function offsetSet($offset, $value)
|
|
{
|
|
throw new \BadMethodCallException('This activity not allowed');
|
|
}
|
|
|
|
/**
|
|
* Offset unset
|
|
*
|
|
* @param mixed $offset offset
|
|
*
|
|
* @return void
|
|
*
|
|
* @throws \BadMethodCallException
|
|
*/
|
|
public function offsetUnset($offset)
|
|
{
|
|
throw new \BadMethodCallException('This call not allowed');
|
|
}
|
|
|
|
/**
|
|
* Check offset
|
|
*
|
|
* @param mixed $offset offset
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function offsetExists($offset)
|
|
{
|
|
return isset($this->response[$offset]);
|
|
}
|
|
|
|
/**
|
|
* Get offset
|
|
*
|
|
* @param mixed $offset offset
|
|
*
|
|
* @return mixed
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
public function offsetGet($offset)
|
|
{
|
|
if (!isset($this->response[$offset])) {
|
|
throw new \InvalidArgumentException("Property \"$offset\" not found");
|
|
}
|
|
|
|
return $this->response[$offset];
|
|
}
|
|
}
|