0 follower

Final Class Yiisoft\RequestProvider\RequestHeaderProvider

InheritanceYiisoft\RequestProvider\RequestHeaderProvider

The RequestHeaderProvider class provides utility methods for retrieving HTTP headers from a request. It uses a RequestProviderInterface implementation to access the current request.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Initializes the instance with a request provider. Yiisoft\RequestProvider\RequestHeaderProvider
get() Retrieves the value(s) of a specific header as an array. Yiisoft\RequestProvider\RequestHeaderProvider
getAll() Retrieves all headers as an associative array where the key is the header name and the value is an array of its values. Yiisoft\RequestProvider\RequestHeaderProvider
getFirstHeaders() Retrieves the first value of each header as an associative array where the key is the header name and the value is the first header value. Yiisoft\RequestProvider\RequestHeaderProvider
getLine() Retrieves the value of a specific header as a string. If the header does not exist, returns default value. Yiisoft\RequestProvider\RequestHeaderProvider
has() Checks if a specific header is present in the request. Yiisoft\RequestProvider\RequestHeaderProvider

Method Details

Hide inherited methods

__construct() public method

Initializes the instance with a request provider.

public __construct( Yiisoft\RequestProvider\RequestProviderInterface $requestProvider ): mixed
$requestProvider Yiisoft\RequestProvider\RequestProviderInterface

The request provider to access the request.

                public function __construct(
    private readonly RequestProviderInterface $requestProvider,
) {}

            
get() public method

Retrieves the value(s) of a specific header as an array.

public get( string $name ): string[]
$name string

The name of the header to retrieve.

return string[]

An array of header values, or an empty array if the header is not present.

                public function get(string $name): array
{
    return $this->requestProvider->get()->getHeader($name);
}

            
getAll() public method

Retrieves all headers as an associative array where the key is the header name and the value is an array of its values.

public getAll( ): string[][]
return string[][]

An associative array of all headers.

                public function getAll(): array
{
    return $this->requestProvider->get()->getHeaders();
}

            
getFirstHeaders() public method

Retrieves the first value of each header as an associative array where the key is the header name and the value is the first header value.

public getFirstHeaders( ): string[]
return string[]

An associative array of the first values of all headers.

                public function getFirstHeaders(): array
{
    return array_map(static fn(array $lines) => $lines[0], $this->getAll());
}

            
getLine() public method

Retrieves the value of a specific header as a string. If the header does not exist, returns default value.

public getLine( string $name, string|null $default null ): string|null
$name string

The name of the header to retrieve.

$default string|null
return string|null

The header value as a string, or default value if the header is not present.

                public function getLine(string $name, ?string $default = null): ?string
{
    $request = $this->requestProvider->get();
    return $request->hasHeader($name) ? $request->getHeaderLine($name) : $default;
}

            
has() public method

Checks if a specific header is present in the request.

public has( string $name ): boolean
$name string

The name of the header to check.

return boolean

True if the header is present, false otherwise.

                public function has(string $name): bool
{
    return $this->requestProvider->get()->hasHeader($name);
}