You are viewing documentation for an outdated version. It is no longer supported!

Usage

There are two DTOs that you can choose from; Cookie and SetCookie.

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.orgopen in new window for additional information about what the Cookie DTO is intended to represent.

The SetCookie is an extended version of the Cookie DTO. It offers the following properties

  • name: Cookie name
  • value: Cookie value
  • expires: Maximum lifetime of the cookie
  • maxAge: Number of seconds until the cookie expires.
  • domain: Hosts to where the cookie will be sent
  • path: Path that must exist on the requested url
  • secure: State of whether the cookie should be sent via https
  • httpOnly: 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.orgopen in new window 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.