Usage
There are two DTOs that you can choose from; Cookie
and SetCookie
.
Cookie
The Cookie
object only contains a name
and value
property.
<?php
use Aedart\Http\Cookies\Cookie;
$cookie = new Cookie([
'name' => 'my_cookie',
'value' => 'sweet'
]);
See mozilla.org for additional information about what the Cookie
DTO is intended to represent.
Set-Cookie
The SetCookie
is an extended version of the Cookie
DTO. It offers the following properties
name
: Cookie namevalue
: Cookie valueexpires
: Maximum lifetime of the cookiemaxAge
: Number of seconds until the cookie expires.domain
: Hosts to where the cookie will be sentpath
: Path that must exist on the requested urlsecure
: State of whether the cookie should be sent via httpshttpOnly
: Whether or not accessing the cookie is forbidden via JavaScript.sameSite
: whether cookie should be available for cross-site requests
<?php
use Aedart\Http\Cookies\SetCookie;
$cookie = new SetCookie([
'name' => 'my_cookie',
'value' => 'sweet',
'expires' => null, // null, timestamp or RFC7231 Formatted string date
'maxAge' => 60 * 5,
'domain' => null,
'path' => '/',
'secure' => true,
'httpOnly' => false,
'sameSite' => SetCookie::SAME_SITE_LAX
]);
See mozilla.org for additional information about what the SetCookie
DTO is intended to represent.
Populate
The Cookie DTOs can be populated from an array, via the popualte()
method.
$cookie->populate([
'name' => 'my_other_cookie'
]);
echo $cookie->getName(); // "my_other_cookie"
Export to Array
Both DTOs are able to export their properties to an array, via the toArray()
method.
$data = $cookie->toArray();
Onward
Please review the source code of these DTOs, for additional information. Feel free to extend these components and offer more functionality, should your application require such.