HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Teamleader/Base/ApiResponse.php
<?php

namespace App\Komma\Teamleader\Base;

use GuzzleHttp\Psr7\Response;

final class ApiResponse
{
    public string $resourceClassName;

    public string $apiType;

    private ?Response $response;

    protected object $initialData;

    public $data;

    private string $apiPath;

    public function __construct(string $className, string $apiType)
    {
        $this->resourceClassName = $className;
        $this->apiType = $apiType;
    }

    /**
     * Fill the Api Response as provided by the Api
     * and then parse the data.
     *
     * @param array $clientResponse
     * @return $this
     */
    public function fill(array $clientResponse): ?self
    {
        [$data, $response] = $clientResponse;
        $this->response = $response;
        $this->initialData = $data;

        $this->parse();

        return $this;
    }

    /**
     * Parse the response data into the defined Resource Class
     *
     * @return void
     */
    private function parse(): void
    {
        if ($this->apiType === 'list') {
            $this->data = ResourceFactory::createFromApiResults($this->initialData->data, $this->resourceClassName);
        } else { // get
            $this->data = ResourceFactory::createFromApiResult($this->initialData->data, new $this->resourceClassName());
        }
    }

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

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