diff --git a/composer.json b/composer.json index a14a9d6..5a49afa 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 5d8aea9..f16e5cc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "53328bf5f9f456c2cd1108f6749632c2", - "content-hash": "b8ee3b75ad43c5b79f71f391211a1940", + "content-hash": "94c9e920b26efccebee49bdea1d64c4a", "packages": [ { "name": "delight-im/http", @@ -41,7 +40,57 @@ "http", "https" ], - "time": "2016-07-21 15:05:01" + "time": "2016-07-21T15:05:01+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" } ], "packages-dev": [], diff --git a/src/Cookie.php b/src/Cookie.php index 99ec6b2..3bd02e5 100644 --- a/src/Cookie.php +++ b/src/Cookie.php @@ -10,6 +10,8 @@ namespace ParagonIE\Cookie; +use Psr\Http\Message\ResponseInterface; + /** * Modern cookie management for PHP * @@ -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 * @@ -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'); } @@ -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; @@ -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'; }