Response.php 1.1 KB
<?php

namespace Lackoxygen\MinPayment;

use Lackoxygen\MinPayment\Constant\Status;
use Lackoxygen\MinPayment\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;

class Response implements ResponseInterface
{
    /**
     * @var PsrResponseInterface $response
     */
    protected $response;

    /**
     * @param PsrResponseInterface $response
     */
    public function __construct(PsrResponseInterface $response)
    {
        $this->response = $response;
    }

    /**
     * @return bool
     */
    public function isSuccess(): bool
    {
        return Status::SUCCESS === (string)\Arr::get($this->toArray(), 'code');
    }

    /**
     * @return array
     */
    public function toArray(): array
    {
        return (array)\json_decode($this->getContent(), true);
    }

    /**
     * @return string
     */
    public function toString(): string
    {
        return $this->getContent();
    }

    /**
     * @return string
     */
    public function getContent(): string
    {
        $this->response->getBody()->rewind();

        return $this->response->getBody()->getContents();
    }
}