*/ private $headers; /** * Parsed response body. An associative array for JSON responses, * otherwise the raw body string. * * @var array|string */ private $body; /** * Constructs an HTTP_Response. * * @param int $status The HTTP status code. * @param array $headers The response headers. * @param array|string $body The parsed body. */ public function __construct( int $status, array $headers, $body ) { $this->status = $status; $this->headers = $headers; $this->body = $body; } /** * Returns the HTTP status code. * * @return int The HTTP status code (0 on transport failure). */ public function get_status(): int { return $this->status; } /** * Returns the response headers. * * @return array The response headers. */ public function get_headers(): array { return $this->headers; } /** * Returns the parsed response body. * * @return array|string The parsed body. */ public function get_body() { return $this->body; } /** * Whether the body is an associative array (decoded JSON). * * @return bool True if the body is an associative array. */ public function has_array_body(): bool { return \is_array( $this->body ); } /** * Returns a value from an array body by key, or a fallback when the * body is not an array or the key is missing. * * @param string $key The key to look up. * @param mixed $fallback The fallback value. * * @return mixed The looked-up value or the fallback. */ public function get_body_value( string $key, $fallback = null ) { if ( ! \is_array( $this->body ) ) { return $fallback; } return ( $this->body[ $key ] ?? $fallback ); } /** * Whether the status indicates a successful response (2xx). * * @return bool True if the status is 2xx. */ public function is_successful(): bool { return ( $this->status >= 200 && $this->status < 300 ); } /** * Whether no response was received (transport failure). * * @return bool True if the status is 0. */ public function is_transport_failure(): bool { return ( $this->status === 0 ); } }