Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to add cookie to a PSR-7 response object #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Modern cookie management for PHP 7",
"require": {
"php": "^7",
"psr/http-message": "~1.0",
"delight-im/http": "^2.0"
},
"type": "library",
Expand Down
55 changes: 52 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 45 additions & 3 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace ParagonIE\Cookie;

use Psr\Http\Message\ResponseInterface;

/**
* Modern cookie management for PHP
*
Expand Down Expand Up @@ -229,6 +231,25 @@ public function save(): bool
return self::addHttpHeader((string) $this);
}

/**
* @param ResponseInterface $response
* @return ResponseInterface
* @throws \Exception
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
{
return $response->withHeader('Set-Cookie', self::buildCookieHeaderValue(
$this->name,
$this->value,
$this->expiryTime,
$this->path,
$this->domain,
$this->secureOnly,
$this->httpOnly,
$this->sameSiteRestriction
));
}

/**
* Deletes the cookie
*
Expand Down Expand Up @@ -365,6 +386,28 @@ public static function buildCookieHeader(
bool $httpOnly = true,
string $sameSiteRestriction = self::SAME_SITE_RESTRICTION_STRICT
): string {
return 'Set-Cookie: ' . self::buildCookieHeaderValue(
$name,
$value,
$expiryTime,
$path,
$domain,
$secureOnly,
$httpOnly,
$sameSiteRestriction
);
}

public static function buildCookieHeaderValue(
string $name,
$value = null,
int $expiryTime = 0,
string $path = '',
string $domain = '',
bool $secureOnly = true,
bool $httpOnly = true,
string $sameSiteRestriction = self::SAME_SITE_RESTRICTION_STRICT
) {
if (!self::isNameValid($name)) {
throw new \Exception('Invalid cookie name');
}
Expand All @@ -390,7 +433,7 @@ public static function buildCookieHeader(
$forceShowExpiry
);

$headerStr = 'Set-Cookie: ' . $name . '=' . urlencode($value);
$headerStr = $name . '=' . urlencode($value);

if (!empty($expiryTimeStr)) {
$headerStr .= '; expires=' . $expiryTimeStr;
Expand Down Expand Up @@ -418,8 +461,7 @@ public static function buildCookieHeader(

if ($sameSiteRestriction === self::SAME_SITE_RESTRICTION_LAX) {
$headerStr .= '; SameSite=Lax';
}
elseif ($sameSiteRestriction === self::SAME_SITE_RESTRICTION_STRICT) {
} elseif ($sameSiteRestriction === self::SAME_SITE_RESTRICTION_STRICT) {
$headerStr .= '; SameSite=Strict';
}

Expand Down