Response mapping covers decoded data, raw PSR responses, entities, collections, envelopes, and hydration context.
Response wraps decoded response data and the raw PSR response.
data(): mixedReturns response data.
When responses()->json() is enabled on the API, JSON bodies are decoded into arrays, empty bodies become null, and invalid JSON throws JsonException.
When responses()->xml() is enabled, XML bodies are decoded into SimpleXMLElement, empty bodies become null, and invalid XML throws RuntimeException.
When responses()->custom() is enabled, the configured callable receives the raw PSR response and returns the value used as Response::data().
When no response format is configured, this returns the raw response body string.
$data = $response->data();raw(): ResponseInterfaceReturns the raw PSR response.
$status = $response->raw()->getStatusCode();entity(string $class, ?string $key = null): EntityInterfaceMaps decoded response data to an entity class.
return $this
->endpoint()
->get('/users/{id}', ['id' => $id])
->entity(User::class, key: 'data');The class must implement EntityInterface.
collection(string $class, ?string $key = null): arrayMaps list data to a plain array of entities.
return $this
->endpoint()
->get('/users')
->collection(User::class, key: 'data');envelope(string $class): EnvelopeInterfaceMaps the response to a custom envelope.
return $this
->endpoint()
->get('/users/{id}', ['id' => $id])
->envelope(UserEnvelope::class);The class must implement EnvelopeInterface.
Entities used by response mapping must implement:
public static function fromArray(array $data, ?Context $context = null): static;fromArray() is the mapping boundary for an entity. The package passes decoded response data to it; the SDK author decides how payload keys become constructor arguments, value objects, or derived values.
Envelopes used by Response::envelope() must implement:
public static function fromResponse(Response $response, ?Context $context = null): static;Context carries SDK config and response resolution into response mapping.
SDK users do not fetch context from Response. The package passes context into entity and envelope hydration methods:
EntityInterface::fromArray(array $data, ?Context $context = null)
EnvelopeInterface::fromResponse(Response $response, ?Context $context = null)config(): ConfigReturns the SDK config available while hydrating entities or envelopes.
$timezone = $context?->config()->get('timezone');When a request is executed through a resource configured with withConfig(),
this returns the effective API configuration plus its resource-local overrides.
The same effective configuration is available to hooks and error handlers for
that request. See Resource-Local Configuration.
resolver(): ResolverInterfaceReturns the response-graph resolver provided by the API runtime. It can follow
linked entities, collections, and pagination through the configured SDK runtime.
Calling it outside an API runtime request throws RuntimeException.
See Resolver for linked-resource authoring, request behavior, and memoization scope.
ErrorContext is passed to configured error handlers.
$this->errors()->status(404, NotFoundException::class);$this->errors()->statuses([
401 => UnauthorizedException::class,
404 => NotFoundException::class,
]);$this->errors()->status(404, function (ErrorContext $context): Throwable {
return new NotFoundException($context->response()->data()['message']);
});$this->errors()->when(function (ErrorContext $context): ?Throwable {
if (($context->response()->data()['code'] ?? null) !== 'invalid_api_key') {
return null;
}
return new InvalidApiKeyException($context->response()->data()['message']);
});It exposes:
response(): ResponseapiContext(): ContextstatusCode(): int