Skip to content

Commit

Permalink
Add RequestCookies class (#11)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2024
1 parent bd57043 commit 0043ff5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions src/RequestCookies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider;

final class RequestCookies
{
/**
* @var array The cookies in this collection (indexed by the cookie name).
*
* @psalm-var array<string, string>
*/
private array $cookies;

public function __construct(RequestProviderInterface $requestProvider)
{
/** @psalm-var array<string, string> */
$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);
}
}
43 changes: 43 additions & 0 deletions tests/RequestCookiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestProvider\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\RequestProvider\RequestCookies;
use Yiisoft\RequestProvider\RequestProviderInterface;

final class RequestCookiesTest extends TestCase
{
public function testGet(): void
{
$requestCookies = $this->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);
}
}

0 comments on commit 0043ff5

Please sign in to comment.