<?php
 declare(strict_types=1); namespace Square\Http; class ApiResponse { private $request; private $statusCode = null; private $reasonPhrase = null; private $headers = null; private $result = null; private $body = null; private $errors = null; private $cursor = null; public static function createFromContext($decodedBody, $result, HttpContext $context): self { $request = $context->getRequest(); $statusCode = $context->getResponse()->getStatusCode(); $reasonPhrase = null; $headers = $context->getResponse()->getHeaders(); $body = $context->getResponse()->getRawBody(); if (!\is_array($decodedBody)) { $decodedBody = (array) $decodedBody; } $cursor = $decodedBody['cursor'] ?? null; $errors = []; if ($statusCode >= 400 && $statusCode < 600) { if (isset($decodedBody['errors'])) { $mapper = new \apimatic\jsonmapper\JsonMapper(); $errors = $mapper->mapClassArray($decodedBody['errors'], '\\Square\\Models\\Error'); } else { $error = new \Square\Models\Error('V1_ERROR', $decodedBody['type'] ?? 'Unknown'); $error->setDetail($decodedBody['message'] ?? null); $error->setField($decodedBody['field'] ?? null); $errors = [$error]; } } return new self($request, $statusCode, $reasonPhrase, $headers, $result, $body, $errors, $cursor); } public function __construct( HttpRequest $request, ?int $statusCode, ?string $reasonPhrase, ?array $headers, $result, $body, array $errors, $cursor ) { $this->request = $request; $this->statusCode = $statusCode; $this->reasonPhrase = $reasonPhrase; $this->headers = $headers; $this->result = $result; $this->body = $body; $this->errors = $errors; $this->cursor = $cursor; } public function getRequest(): HttpRequest { return $this->request; } public function getStatusCode(): ?int { return $this->statusCode; } public function getReasonPhrase(): ?string { return $this->reasonPhrase; } public function getHeaders(): ?array { return $this->headers; } public function getResult() { return $this->result; } public function getBody() { return $this->body; } public function getErrors(): array { return $this->errors; } public function getCursor() { return $this->cursor; } public function isSuccess(): bool { return isset($this->statusCode) && $this->statusCode >= 200 && $this->statusCode < 300; } public function isError(): bool { return !$this->isSuccess(); } } 