-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from Lemon-Framework/cookies
[Http] CookieJar
- Loading branch information
Showing
10 changed files
with
153 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lemon\Contracts\Http; | ||
|
||
interface CookieJar | ||
{ | ||
public function get(string $name): ?string; | ||
|
||
public function set(string $name, string $value, int $expires = 0): static; | ||
|
||
public function delete(string $name): static; | ||
|
||
public function has(string $name): bool; | ||
|
||
public function cookies(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lemon\Http; | ||
|
||
use Lemon\Contracts\Http\CookieJar as CookieJarContract; | ||
use Lemon\Kernel\Application; | ||
|
||
class CookieJar implements CookieJarContract | ||
{ | ||
private array $set_cookies = []; | ||
|
||
public function __construct( | ||
private Request $request, | ||
) { | ||
|
||
} | ||
|
||
public function get(string $name): ?string | ||
{ | ||
return $this->request->getCookie($name); | ||
} | ||
|
||
public function set(string $name, string $value, int $expires = 0): static | ||
{ | ||
$this->set_cookies[] = [$name, $value, $expires]; | ||
return $this; | ||
} | ||
|
||
public function delete(string $name): static | ||
{ | ||
if (!$this->request->hasCookie($name)) { | ||
return $this; | ||
} | ||
$this->set_cookies[] = [$name, '', -1]; | ||
return $this; | ||
} | ||
|
||
public function has(string $name): bool | ||
{ | ||
return $this->request->hasCookie($name); | ||
} | ||
|
||
public function cookies(): array | ||
{ | ||
return $this->set_cookies; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lemon\Tests\Http; | ||
|
||
use Lemon\Http\CookieJar; | ||
use Lemon\Http\Request; | ||
use Lemon\Tests\TestCase; | ||
|
||
class CookieJarTest extends TestCase | ||
{ | ||
public function getJar(array $cookies = []): CookieJar | ||
{ | ||
return new CookieJar(new Request('', '', '', [], '', $cookies, [], '')); | ||
} | ||
|
||
public function testGet() | ||
{ | ||
$jar = $this->getJar(['foo' => 'bar']); | ||
$this->assertSame('bar', $jar->get('foo')); | ||
$this->assertNull($jar->get('parek')); | ||
} | ||
|
||
public function testSet() | ||
{ | ||
$jar = $this->getJar(); | ||
$jar->set('foo', 'bar'); | ||
$jar->set('bar', 'baz', 3600); | ||
|
||
$this->assertSame([['foo', 'bar', 0], ['bar', 'baz', 3600]], $jar->cookies()); | ||
} | ||
|
||
public function getDelete() | ||
{ | ||
$jar = $this->getJar(['foo' => 'bar']); | ||
$jar->delete('foo'); | ||
$this->assertSame([['foo', '', -1]], $jar->cookies()); | ||
$jar->delete('bar'); | ||
$this->assertSame([['foo', '', -1]], $jar->cookies()); | ||
} | ||
|
||
public function testHas() | ||
{ | ||
$jar = $this->getJar(['foo' => 'bar']); | ||
$this->assertTrue($jar->has('foo')); | ||
$this->assertFalse($jar->has('parek')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters