Request.php 2.3 KB
<?php

namespace Lackoxygen\TiktokOpen\Base\Client;

use GuzzleHttp\RequestOptions;

class Request
{
    protected string $method;

    protected string $format;

    protected string $path;

    protected array $data = [];

    protected array $headers = [];

    protected bool $withSession;

    public function __construct(
        string $method,
        string $format,
        string $path,
        array  $data,
        bool   $withSession
    )
    {
        $this->setMethod($method);

        $this->format = $format;
        $this->path = $path;
        $this->data = $data;
        $this->withSession = $withSession;
    }

    /**
     * @param string $format
     */
    public function setFormat(string $format): void
    {
        $this->format = $format;
    }


    /**
     * @param string $method
     */
    public function setMethod(string $method): void
    {
        $this->method = strtoupper($method);
    }

    /**
     * @param string $path
     */
    public function setPath(string $path): void
    {
        $this->path = $path;
    }

    /**
     * @param array $data
     */
    public function setData(array $data): void
    {
        $this->data = $data;
    }

    /**
     * @param array $headers
     */
    public function setHeaders(array $headers): void
    {
        $this->headers = $headers;
    }

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

    /**
     * @return array
     */
    public function getData(): array
    {
        return $this->data;
    }

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

    /**
     * @return array
     */
    public function getHeaders(): array
    {
        return $this->headers;
    }

    /**
     * @return bool
     */
    public function isWithSession(): bool
    {
        return $this->withSession;
    }

    protected function defaultFormat(): string
    {
        if ('GET' === $this->method) {
            return RequestOptions::QUERY;
        } else {
            return RequestOptions::FORM_PARAMS;
        }
    }

    public function toArray(): array
    {
        $format = $this->format ?: $this->defaultFormat();

        return [
            RequestOptions::HEADERS => $this->headers,
            $format => $this->data,
        ];
    }
}