# Class HttpSoft\Cookie\CookieCreator

Helper for creating HttpSoft\Cookie\Cookie.

Source code on GitHub.

use HttpSoft\Cookie\CookieCreator;

// From attributes
$cookie = CookieCreator::create('name', 'value', '+1 hour', '.example.com', '/path', true, true, 'Lax');

// From raw `Set-Cookie` header
$cookie = CookieCreator::createFromString('name=value; Path=/; Secure; HttpOnly; SameSite=Lax; ...');

$cookie->__toString(); // 'name=value; Path=/; Secure; HttpOnly; SameSite=Lax; ...'
// equivalently to:
(string) $cookie;

# Public methods

# create

Creates a new HttpSoft\Cookie\Cookie instance.

 * @param string $name the name of the cookie.
 * @param string $value the value of the cookie.
 * @param DateTimeInterface|int|string|null $expire the time the cookie expire.
 * @param string|null $path the set of paths for the cookie.
 * @param string|null $domain the set of domains for the cookie.
 * @param bool|null $secure whether the cookie should only be transmitted over a secure HTTPS connection.
 * @param bool|null $httpOnly whether the cookie can be accessed only through the HTTP protocol.
 * @param string|null $sameSite whether the cookie will be available for cross-site requests.
 * @return CookieInterface
public static function create(
    string $name,
    string $value = '',
    $expire = null,
    ?string $domain = null,
    ?string $path = '/',
    ?bool $secure = true,
    ?bool $httpOnly = true,
    ?string $sameSite = Cookie::SAME_SITE_LAX
): CookieInterface;

if one or more arguments are not valid, the \InvalidArgumentException exception will be thrown.

$cookie = CookieCreator::create('name', 'value', '+1 hour', '.example.com', '/path', true, true, 'Lax');

$cookie->getName(); // 'name'
$cookie->getValue(); // 'value'
$cookie->getMaxAge(); // time() + 3600
$cookie->getExpires(); // 3600
$cookie->getDomain(); // '.example.com'
$cookie->getPath(); // '/path'
$cookie->isExpired(); // false
$cookie->isSession(); // false
$cookie->isSecure(); // true
$cookie->isHttpOnly(); // true
$cookie->getSameSite(); // 'Lax'

# createFromString

Creates a new HttpSoft\Cookie\Cookie instance from raw Set-Cookie header.

public static function createFromString(string $string): CookieInterface;

If $string is not a valid raw Set-Cookie header, the \InvalidArgumentException exception will be thrown.

$cookie = CookieCreator::createFromString('name=value; Path=/; Secure; HttpOnly; SameSite=Lax');

$cookie->getName(); // 'name'
$cookie->getValue(); // 'value'
$cookie->getMaxAge(); // 0
$cookie->getExpires(); // 0
$cookie->getDomain(); // null
$cookie->getPath(); // null
$cookie->isExpired(); // false
$cookie->isSession(); // true
$cookie->isSecure(); // true
$cookie->isHttpOnly(); // true
$cookie->getSameSite(); // 'Lax'

CookieCreator::createFromString(''); // throws InvalidArgumentException
CookieCreator::createFromString('name[]'); // throws InvalidArgumentException

For more information about the Set-Cookie header, see here.