Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Response.php

class Response

Same name in this branch
  1. 11.1.x vendor/composer/composer/src/Composer/Util/Http/Response.php \Composer\Util\Http\Response
  2. 11.1.x vendor/guzzlehttp/psr7/src/Response.php \GuzzleHttp\Psr7\Response
  3. 11.1.x vendor/symfony/http-foundation/Response.php \Symfony\Component\HttpFoundation\Response

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\BrowserKit\Response

Expanded class hierarchy of Response

2 files declare their use of Response
BrowserKitDriver.php in vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php
HttpKernelBrowser.php in vendor/symfony/http-kernel/HttpKernelBrowser.php
3 string references to 'Response'
DynamicPageCacheSubscriber::onRequest in core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
Sets a response in case of a Dynamic Page Cache hit.
DynamicPageCacheSubscriber::onResponse in core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
Stores a response in case of a Dynamic Page Cache miss, if cacheable.
MessageFormatter::format in vendor/guzzlehttp/guzzle/src/MessageFormatter.php
Returns a formatted message string.

File

vendor/symfony/browser-kit/Response.php, line 19

Namespace

Symfony\Component\BrowserKit
View source
final class Response {
    private array $jsonData;
    
    /**
     * The headers array is a set of key/value pairs. If a header is present multiple times
     * then the value is an array of all the values.
     *
     * @param string $content The content of the response
     * @param int    $status  The response status code (302 "Found" by default)
     * @param array  $headers An array of headers
     */
    public function __construct(string $content = '', int $status = 200, array $headers = []) {
    }
    
    /**
     * Converts the response object to string containing all headers and the response content.
     */
    public function __toString() : string {
        $headers = '';
        foreach ($this->headers as $name => $value) {
            if (\is_string($value)) {
                $headers .= \sprintf("%s: %s\n", $name, $value);
            }
            else {
                foreach ($value as $headerValue) {
                    $headers .= \sprintf("%s: %s\n", $name, $headerValue);
                }
            }
        }
        return $headers . "\n" . $this->content;
    }
    public function getContent() : string {
        return $this->content;
    }
    public function getStatusCode() : int {
        return $this->status;
    }
    public function getHeaders() : array {
        return $this->headers;
    }
    
    /**
     * @return string|array|null The first header value if $first is true, an array of values otherwise
     */
    public function getHeader(string $header, bool $first = true) : string|array|null {
        $normalizedHeader = str_replace('-', '_', strtolower($header));
        foreach ($this->headers as $key => $value) {
            if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
                if ($first) {
                    return \is_array($value) ? \count($value) ? $value[0] : '' : $value;
                }
                return \is_array($value) ? $value : [
                    $value,
                ];
            }
        }
        return $first ? null : [];
    }
    public function toArray() : array {
        if (isset($this->jsonData)) {
            return $this->jsonData;
        }
        try {
            $content = json_decode($this->content, true, flags: \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR);
        } catch (\JsonException $e) {
            throw new JsonException($e->getMessage(), $e->getCode(), $e);
        }
        if (!\is_array($content)) {
            throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
        }
        return $this->jsonData = $content;
    }

}

Members

Title Sort descending Modifiers Object type Summary
Response::$jsonData private property
Response::getContent public function
Response::getHeader public function
Response::getHeaders public function
Response::getStatusCode public function
Response::toArray public function
Response::__construct public function The headers array is a set of key/value pairs. If a header is present multiple times
then the value is an array of all the values.
Response::__toString public function Converts the response object to string containing all headers and the response content.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal