success = $success; $this->statusCode = $statusCode; $this->data = $data; $this->error = $error; $this->message = $message; $this->raw = $raw; } /** * Check if the request was successful */ public function isSuccess(): bool { return $this->success; } /** * Check if the request failed */ public function isFailed(): bool { return !$this->success; } /** * Get data value by key * * @param string $key * @param mixed $default * @return mixed */ public function get(string $key, $default = null) { return $this->data[$key] ?? $default; } /** * Get the message UUID (for sendMessage response) */ public function getMessageUuid(): ?string { return $this->data['message_uuid'] ?? null; } /** * Get credits consumed (for sendMessage response) */ public function getCreditsConsumed(): ?int { return isset($this->data['credits_consumed']) ? (int) $this->data['credits_consumed'] : null; } /** * Get credits remaining */ public function getCreditsRemaining(): ?int { return isset($this->data['credits_remaining']) ? (int) $this->data['credits_remaining'] : null; } /** * Get available credits (for getCredits response) */ public function getCreditsAvailable(): ?int { return isset($this->data['credits_available']) ? (int) $this->data['credits_available'] : null; } /** * Get message status (for getMessageStatus response) */ public function getStatus(): ?string { return $this->data['status'] ?? null; } /** * Get templates list (for getTemplates response) */ public function getTemplates(): array { // When using getTemplates(), the data IS the templates array return $this->data; } /** * Check if message has response (for getMessageStatus response) */ public function hasResponse(): bool { return $this->data['has_response'] ?? false; } /** * Get error message */ public function getErrorMessage(): ?string { return $this->message ?? $this->error; } /** * Check if error is due to insufficient credits */ public function isInsufficientCredits(): bool { return $this->error === 'insufficient_credits' || $this->statusCode === 402; } /** * Check if error is due to invalid token */ public function isUnauthorized(): bool { return $this->error === 'unauthorized' || $this->statusCode === 401; } /** * Check if template was not found */ public function isTemplateNotFound(): bool { return $this->error === 'template_not_found' || $this->statusCode === 404; } /** * Check if template is not usable */ public function isTemplateNotUsable(): bool { return $this->error === 'template_not_usable'; } /** * Convert to array */ public function toArray(): array { return [ 'success' => $this->success, 'status_code' => $this->statusCode, 'data' => $this->data, 'error' => $this->error, 'message' => $this->message, ]; } /** * Convert to JSON */ public function toJson(): string { return json_encode($this->toArray()); } /** * Create a failed response * * @param string $error * @param string $message * @param int $statusCode * @return static */ public static function error(string $error, string $message, int $statusCode = 0): static { return new static( success: false, statusCode: $statusCode, data: [], error: $error, message: $message, raw: [] ); } /** * Create a successful response * * @param array $data * @param int $statusCode * @return static */ public static function success(array $data = [], int $statusCode = 200): static { return new static( success: true, statusCode: $statusCode, data: $data, error: null, message: null, raw: $data ); } }