Skip to content

Commit

Permalink
[Cookie] New Component & Contract
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Dec 1, 2023
1 parent 4219657 commit c6eb5ce
Show file tree
Hide file tree
Showing 21 changed files with 568 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ test-cache: phpunit
test-clock: PHPUNIT_TESTSUITE=clock
test-clock: phpunit

test-cookie: PHPUNIT_TESTSUITE=cookie
test-cookie: phpunit

test-cqrs: PHPUNIT_TESTSUITE=cqrs
test-cqrs: phpunit

Expand Down
8 changes: 8 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"path": "src/SonsOfPHP/Component/Clock",
"repository": "[email protected]:SonsOfPHP/clock.git"
},
{
"path": "src/SonsOfPHP/Component/Cookie",
"repository": "[email protected]:SonsOfPHP/cookie.git"
},
{
"path": "src/SonsOfPHP/Component/Cqrs",
"repository": "[email protected]:SonsOfPHP/cqrs.git"
Expand Down Expand Up @@ -101,6 +105,10 @@
"path": "src/SonsOfPHP/Contract/Common",
"repository": "[email protected]:SonsOfPHP/common-contract.git"
},
{
"path": "src/SonsOfPHP/Contract/Cookie",
"repository": "[email protected]:SonsOfPHP/cookie-contract.git"
},
{
"path": "src/SonsOfPHP/Contract/Cqrs",
"repository": "[email protected]:SonsOfPHP/cqrs-contract.git"
Expand Down
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"psr/log-implementation": "^1.0 || ^2.0 || ^3.0",
"sonsofphp/logger-implementation": "0.3.x-dev",
"sonsofphp/pager-implementation": "0.3.x-dev",
"psr/link-implementation": "^1.0 || ^2.0"
"psr/link-implementation": "^1.0 || ^2.0",
"sonsofphp/cookie-implementation": "0.3.x-dev"
},
"require": {
"php": ">=8.1",
Expand All @@ -74,7 +75,8 @@
"twig/twig": "^3.0",
"ext-intl": "*",
"doctrine/collections": "^2",
"doctrine/orm": "^2"
"doctrine/orm": "^2",
"sonsofphp/cookie-contract": "0.3.x-dev"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -110,13 +112,16 @@
"sonsofphp/money-twig": "self.version",
"sonsofphp/pager-doctrine-collections": "self.version",
"sonsofphp/pager-doctrine-dbal": "self.version",
"sonsofphp/pager-doctrine-orm": "self.version"
"sonsofphp/pager-doctrine-orm": "self.version",
"sonsofphp/cookie": "self.version",
"sonsofphp/cookie-contract": "self.version"
},
"autoload": {
"psr-4": {
"SonsOfPHP\\Bard\\": "src/SonsOfPHP/Bard/src",
"SonsOfPHP\\Component\\Cache\\": "src/SonsOfPHP/Component/Cache",
"SonsOfPHP\\Component\\Clock\\": "src/SonsOfPHP/Component/Clock",
"SonsOfPHP\\Component\\Cookie\\": "src/SonsOfPHP/Component/Cookie",
"SonsOfPHP\\Component\\Cqrs\\": "src/SonsOfPHP/Component/Cqrs",
"SonsOfPHP\\Bundle\\Cqrs\\": "src/SonsOfPHP/Bundle/Cqrs",
"SonsOfPHP\\Bridge\\Symfony\\Cqrs\\": "src/SonsOfPHP/Bridge/Symfony/Cqrs",
Expand All @@ -139,6 +144,7 @@
"SonsOfPHP\\Bridge\\Doctrine\\ORM\\Pager\\": "src/SonsOfPHP/Bridge/Doctrine/ORM/Pager",
"SonsOfPHP\\Component\\Version\\": "src/SonsOfPHP/Component/Version",
"SonsOfPHP\\Contract\\Common\\": "src/SonsOfPHP/Contract/Common",
"SonsOfPHP\\Contract\\Cookie\\": "src/SonsOfPHP/Contract/Cookie",
"SonsOfPHP\\Contract\\Cqrs\\": "src/SonsOfPHP/Contract/Cqrs",
"SonsOfPHP\\Contract\\EventSourcing\\": "src/SonsOfPHP/Contract/EventSourcing",
"SonsOfPHP\\Contract\\FeatureToggle\\": "src/SonsOfPHP/Contract/FeatureToggle",
Expand All @@ -152,6 +158,7 @@
"src/SonsOfPHP/Bard/Tests",
"src/SonsOfPHP/Component/Cache/Tests",
"src/SonsOfPHP/Component/Clock/Tests",
"src/SonsOfPHP/Component/Cookie/Tests",
"src/SonsOfPHP/Component/Cqrs/Tests",
"src/SonsOfPHP/Bundle/Cqrs/Tests",
"src/SonsOfPHP/Bridge/Symfony/Cqrs/Tests",
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ nav:
- components/index.md
- Cache: components/cache/index.md
- Clock: components/clock/index.md
- Cookie: components/cookie/index.md
- CQRS:
- components/cqrs/index.md
- Event Dispatcher: components/event-dispatcher/index.md
Expand Down Expand Up @@ -118,5 +119,6 @@ nav:
- Contracts:
- contracts/index.md
- Common: contracts/common/index.md
- Cookie: contracts/cookie/index.md
- Cqrs: contracts/cqrs/index.md
- Pager: contracts/pager/index.md
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<directory>src/SonsOfPHP/Component/Clock/Tests</directory>
</testsuite>

<testsuite name="cookie">
<directory>src/SonsOfPHP/Component/Cookie/Tests</directory>
</testsuite>

<testsuite name="cqrs">
<directory>src/SonsOfPHP/Bridge/*/Cqrs/Tests</directory>
<!--<directory>src/SonsOfPHP/Component/Cqrs/Tests</directory>-->
Expand Down
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/Cookie/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/Cookie/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
163 changes: 163 additions & 0 deletions src/SonsOfPHP/Component/Cookie/Cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

declare(strict_types=1);

namespace SonsOfPHP\Component\Cookie;

use SonsOfPHP\Contract\Cookie\CookieInterface;

/**
* @author Joshua Estes <[email protected]>
*/
class Cookie implements CookieInterface
{
public function __construct(
private string $name,
private string $value = '',
private array $options = ['expires' => 0, 'secure' => false, 'httponly' => false],
) {}

/**
* {@inheritdoc}
*/
public function withName(string $name): static
{
if ($name === $this->name) {
return $this;
}

$that = clone $this;
$that->name = $name;

return $that;
}

/**
* {@inheritdoc}
*/
public function withValue(string $value): static
{
if ($value === $this->value) {
return $this;
}

$that = clone $this;
$that->value = $value;

return $that;
}

/**
* {@inheritdoc}
*/
public function withPath(string $path): static
{
if (array_key_exists('path', $this->options) && $path === $this->options['path']) {
return $this;
}

$that = clone $this;
$that->options['path'] = $path;

return $that;
}

/**
* {@inheritdoc}
*/
public function withDomain(string $domain): static
{
if (array_key_exists('domain', $this->options) && $domain === $this->options['domain']) {
return $this;
}

$that = clone $this;
$that->options['domain'] = $domain;

return $that;
}

/**
* {@inheritdoc}
*/
public function withSecure(bool $secure): static
{
if (array_key_exists('secure', $this->options) && $secure === $this->options['secure']) {
return $this;
}

$that = clone $this;
$that->options['secure'] = $secure;

return $that;
}

/**
* {@inheritdoc}
*/
public function withHttpOnly(bool $httpOnly): static
{
if (array_key_exists('httponly', $this->options) && $httpOnly === $this->options['httponly']) {
return $this;
}

$that = clone $this;
$that->options['httponly'] = $httpOnly;

return $that;
}

/**
* {@inheritdoc}
*/
public function withSameSite(string $sameSite): static
{
if (array_key_exists('samesite', $this->options) && $sameSite === $this->options['samesite']) {
return $this;
}

if (!in_array(strtolower($sameSite), ['none', 'lax', 'strict'])) {
throw new \InvalidArgumentException('Invalid value for $sameSite');
}

$that = clone $this;
$that->options['samesite'] = $sameSite;

return $that;
}

/**
* {@inheritdoc}
*/
public function withExpires(\DateTimeImmutable|int|string $expires): static
{
if (is_string($expires)) {
$expires = (new \DateTimeImmutable($expires))->format('U');
} elseif ($expires instanceof \DateTimeImmutable) {
$expires = $expires->format('U');
}

if (array_key_exists('expires', $this->options) && $expires === $this->options['expires']) {
return $this;
}

$that = clone $this;
$that->options['expires'] = $expires;

return $that;
}

/**
* {@inheritdoc}
*/
public function send(bool $raw = false): void
{
if (true === $raw) {
// raw dog those values
setrawcookie($this->name, $this->value, $this->options);
return;
}

setcookie($this->name, $this->value, $this->options);
}
}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/Cookie/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2022 to Present Joshua Estes

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions src/SonsOfPHP/Component/Cookie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Sons of PHP - Cookie
====================

## Learn More

* [Documentation][docs]
* [Contributing][contributing]
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the [Mother Repository][mother-repo]
* Get Help & Support using [Discussions][discussions]

[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/cookie/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3ACookie
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3ACookie
Loading

0 comments on commit c6eb5ce

Please sign in to comment.