APIRequest.php 3.14 KB
<?php

namespace App\Service\Requests;

use App\Models\Tokens;
use App\Service\API\API;

class APIRequest implements \App\Service\Contract\APIRequest
{
    protected $api;
    protected $service;
    protected $method;
    protected $params;
    protected $token;

    protected function __construct($api)
    {
        $this->api = $api;
    }

    static function getInstance($type)
    {
        switch ($type) {
            case API::YANDEX:
                return new DirectRequest($type);
            default:
                null;
        }
    }

    function getApi(): string
    {
        return $this->api;
    }

    function setService(string $service)
    {
        $this->service = $service;
        return $this;
    }

    function getService()
    {
        return $this->service;
    }

    function setMethod(string $method)
    {
        $this->method = $method;
        return $this;
    }

    function getMethod()
    {
        return $this->method;
    }

    function setParams(array $params)
    {
        $this->params = $params;
        return $this;
    }

    function getParams()
    {
        return $this->params;
    }

    function setToken(Tokens $token)
    {
        $this->token = $token;
        return $this;
    }

    function getToken(): Tokens
    {
        return $this->token;
    }

    function sliceByKey($maxObjects, $key): ?\App\Service\Contract\APIRequest
    {
        $params = $this->getParams();

        $replication = clone $this;

        if (is_array($key)) {
            $params = $this->changeInArray($params, $key, $maxObjects);
        } else {
            $params_by_key = $params[$key];
            $params[$key] = $this->sliceArray($params_by_key, $maxObjects);
        }

        $replication->setParams($params);

        if (is_array($key)) {
            $params = $this->changeInArray($this->getParams(), $key, 0, $maxObjects);
        } else {
            $params_by_key = $params[$key];
            $params[$key] = $this->sliceArray($params_by_key, 0, $maxObjects);
        }

        $this->setParams($params);

        return $replication;
    }

    private function sliceArray($params, $offset, $length = null): array
    {
        return array_values(array_slice($params, $offset, $length));
    }

    private function changeInArray($params, $key, $offset, $maxObjects = null)
    {
        $params_i = &$params;
        for ($i = 0; $i < count($key); $i++) {
            $params_i = &$params_i[$key[$i]];
        }
        $objects = $this->sliceArray($params_i, $offset, $maxObjects);
        $params_i = $objects;
        return $params;
    }

    function slice($count): ?\App\Service\Contract\APIRequest
    {
        throw new Exception('Я не знаю формата запрсов, чтобы его разбить');
    }

    function next($count)
    {
        throw new Exception('Я не знаю формата запрсов, чтобы его разбить');
    }

    function call($params = null)
    {

    }

    function handle($response)
    {

    }

    function getObjectsCount()
    {
        throw new Exception('Не задано сколкьо бъектов обрабатывается');
    }
}