# Class HttpSoft\Cookie\CookieManager

Class for managing cookies, that implements HttpSoft\Cookie\CookieManagerInterface.

Source code on GitHub.

use HttpSoft\Cookie\Cookie;
use HttpSoft\Cookie\CookieManager;

/**
 * @var HttpSoft\Emitter\EmitterInterface $emitter
 * @var Psr\Http\Message\ResponseInterface $response
 */

// Create cookie
$cookie1 = new Cookie('test', 'value', '+1 hour');
$cookie2 = new Cookie('test2', 'value', time() + 3600, '.example.com', '/path');

// Set cookies to the manager
$manager = new CookieManager([$cookie1, $cookie2]);
// or
$manager = new CookieManager();
$manager->setMultiple([$cookie1, $cookie2]);
// or
$manager->set($cookie1);
$manager->set($cookie2);

// Set all cookie to the response for sending
$response = $manager->send($response);
// If do not need to remove previously set cookies from the response
$response = $manager->send($response, false);

// Emit a response to the client
$emitter->emit($response);

// Alternative if not using PSR-7
// Set all cookie for sending using `setcookie()` function
foreach ($manager->getAll() as $cookie) {
    setcookie($cookie->getName(), $cookie->getValue(), [
        'expires' => $cookie->getExpires(),
        'domain' => $cookie->getDomain(),
        'path' => $cookie->getPath(),
        'secure' => $cookie->isSecure(),
        'httpOnly' => $cookie->isHttpOnly(),
        'sameSite' => $cookie->getSameSite(),
    ]);
}

# Public methods

See the original detailed description of the methods in the HttpSoft\Cookie\CookieManagerInterface.

/**
 * @param CookieInterface[] $cookies
 */
public function __construct(array $cookies = [])

If $cookies is not a valid, the \InvalidArgumentException exception will be thrown.

# set

Sets a cookie.

public function set(CookieInterface $cookie): void;

# setMultiple

Sets multiple cookies.

/**
 * @param CookieInterface[] $cookies
 */
public function setMultiple(array $cookies): void;

If $cookies is not a valid, the \InvalidArgumentException exception will be thrown.

# get

Gets the cookie with the specified name.

public function get(string $name): ?CookieInterface;

Returns an instance of HttpSoft\Cookie\CookieInterface or null if the cookie does not exist.

# getAll

Gets all cookies, or an empty array if no cookies exist.

/**
 * @return CookieInterface[]
 */
public function getAll(): array;

# getValue

Gets the value of the named cookie.

public function getValue(string $name): ?string;

Returns a value of the named cookie or null if the cookie does not exist.

# getIterator

Gets an external iterator with cookies.

public function getIterator(): ArrayIterator;

# has

Checks whether a cookie with the specified name exists.

public function has(string $name): bool;

# remove

Removes and returns the removed cookie, or null if the cookie does not exist.

public function remove(string $name): ?CookieInterface;

# clear

Removes all cookies.

public function clear(): void;

# count

Returns the number of cookies.

public function count(): int;

# send

Sets all cookie to the response and returns a clone instance of the response with the cookies set.

/**
 * @param Psr\Http\Message\ResponseInterface $response a response instance that implements PSR-7.
 * @param bool $removeResponseCookies whether to remove previously set cookies from the response.
 * @return Psr\Http\Message\ResponseInterface response with cookies set.
 */
public function send(
    ResponseInterface $response, 
    bool $removeResponseCookies = true
): ResponseInterface;

This method must be called before emitting the response. See Psr\Http\Message\ResponseInterface.