From 0043ff51dc1b805d808baf180efd56c88ecffd74 Mon Sep 17 00:00:00 2001 From: hacan359 <59626486+hacan359@users.noreply.github.com> Date: Sun, 27 Oct 2024 17:36:48 +0300 Subject: [PATCH] Add `RequestCookies` class (#11) Co-authored-by: Sergei Predvoditelev Co-authored-by: Alexander Makarov --- CHANGELOG.md | 1 + README.md | 19 ++++++++++++++++ src/RequestCookies.php | 31 ++++++++++++++++++++++++++ tests/RequestCookiesTest.php | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/RequestCookies.php create mode 100644 tests/RequestCookiesTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c51b7..8d46217 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.1.0 under development - Enh #10: Add `yiisoft/middleware-dispatcher` event listener to keep `Request` always available (@xepozz) +- New #11: Add `RequestCookies` class that provides convenient access to request cookies (@hacan359) ## 1.0.0 March 02, 2024 diff --git a/README.md b/README.md index e954f6d..b068a5c 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,25 @@ final class MyService } ``` +### Request cookies collection + +You can work with cookies like the following: + +```php +class MyClass +{ + public function __construct( + private \Yiisoft\RequestProvider\RequestCookies $cookies + ) {} + + public function go(): void + { + $this->cookies->has('foo'); + $this->cookies->get('bar'); + } +} +``` + ## Documentation - [Internals](docs/internals.md) diff --git a/src/RequestCookies.php b/src/RequestCookies.php new file mode 100644 index 0000000..aef5f96 --- /dev/null +++ b/src/RequestCookies.php @@ -0,0 +1,31 @@ + + */ + private array $cookies; + + public function __construct(RequestProviderInterface $requestProvider) + { + /** @psalm-var array */ + $this->cookies = $requestProvider->get()->getCookieParams(); + } + + public function get(string $name): ?string + { + return $this->cookies[$name] ?? null; + } + + public function has(string $name): bool + { + return array_key_exists($name, $this->cookies); + } +} diff --git a/tests/RequestCookiesTest.php b/tests/RequestCookiesTest.php new file mode 100644 index 0000000..989b1b3 --- /dev/null +++ b/tests/RequestCookiesTest.php @@ -0,0 +1,43 @@ +createRequestCookies(['test' => 'value']); + + $this->assertSame('value', $requestCookies->get('test')); + } + + public function testHas(): void + { + $requestCookies = $this->createRequestCookies(['test' => 'value']); + + $this->assertTrue($requestCookies->has('test')); + $this->assertFalse($requestCookies->has('non-exist')); + } + + private function createRequestCookies(array $cookies = []): RequestCookies + { + /** @var ServerRequestInterface $serverRequestMock */ + $serverRequestMock = $this->createMock(ServerRequestInterface::class); + $serverRequestMock + ->method('getCookieParams') + ->willReturn($cookies); + + /** @var RequestProviderInterface $requestProvider */ + $requestProvider = $this->createMock(RequestProviderInterface::class); + $requestProvider->method('get')->willReturn($serverRequestMock); + + return new RequestCookies($requestProvider); + } +}