2020-01-11 06:30:25 +00:00
|
|
|
|
<?php
|
2021-11-18 04:24:30 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020-2021 Антон Аксенов (Anthony Axenov)
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*
|
|
|
|
|
* This code is licensed under MIT.
|
|
|
|
|
* Этот код распространяется по лицензии MIT.
|
|
|
|
|
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
|
|
|
|
|
*/
|
|
|
|
|
|
2021-11-18 04:24:30 +00:00
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
namespace AtolOnline\Api;
|
|
|
|
|
|
|
|
|
|
use JsonSerializable;
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
use stdClass;
|
2021-11-17 16:01:53 +00:00
|
|
|
|
use Stringable;
|
2020-01-11 06:30:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Класс AtolResponse, описывающий ответ от ККТ
|
|
|
|
|
*
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @property mixed $error
|
2020-01-11 06:30:25 +00:00
|
|
|
|
* @package AtolOnline\Api
|
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
class KktResponse implements JsonSerializable, Stringable
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @var int Код ответа сервера
|
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
protected int $code;
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
2021-11-17 16:01:53 +00:00
|
|
|
|
* @var stdClass|array|null Содержимое ответа сервера
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
protected stdClass|array|null $content;
|
2020-01-11 06:30:25 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Заголовки ответа
|
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
protected array $headers;
|
|
|
|
|
|
2020-01-11 06:30:25 +00:00
|
|
|
|
/**
|
|
|
|
|
* AtolResponse constructor.
|
|
|
|
|
*
|
2021-11-18 04:24:30 +00:00
|
|
|
|
* @param ResponseInterface $response
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
|
|
|
|
public function __construct(ResponseInterface $response)
|
|
|
|
|
{
|
|
|
|
|
$this->code = $response->getStatusCode();
|
|
|
|
|
$this->headers = $response->getHeaders();
|
2021-11-18 04:24:30 +00:00
|
|
|
|
$this->content = json_decode((string)$response->getBody());
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает заголовки ответа
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getHeaders(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает запрошенный параметр из декодированного объекта результата
|
|
|
|
|
*
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
public function __get($name): mixed
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
2021-11-17 16:01:53 +00:00
|
|
|
|
return $this->getContent()?->$name;
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает код ответа
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getCode(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает объект результата запроса
|
|
|
|
|
*
|
2021-11-17 16:01:53 +00:00
|
|
|
|
* @return mixed
|
2020-01-11 06:30:25 +00:00
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
public function getContent(): mixed
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
|
|
|
|
return $this->content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Проверяет успешность запроса по соержимому результата
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-11-18 04:24:30 +00:00
|
|
|
|
public function isValid(): bool
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
|
|
|
|
return !empty($this->getCode())
|
|
|
|
|
&& !empty($this->getContent())
|
|
|
|
|
&& empty($this->getContent()->error)
|
2021-11-18 04:24:30 +00:00
|
|
|
|
&& $this->getCode() < 400;
|
2020-01-11 06:30:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Возвращает текстовое представление
|
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
public function __toString(): string
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
|
|
|
|
return json_encode($this->jsonSerialize(), JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
2021-11-17 16:01:53 +00:00
|
|
|
|
public function jsonSerialize(): array
|
2020-01-11 06:30:25 +00:00
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'code' => $this->code,
|
|
|
|
|
'headers' => $this->headers,
|
|
|
|
|
'body' => $this->content,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|