Skip to content

Commit

Permalink
Updated codebase to new standards and php 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
shudd3r committed Mar 7, 2021
2 parents 749451a + 778a418 commit ed43209
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 342 deletions.
2 changes: 1 addition & 1 deletion cs-fixer.php.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand Down
3 changes: 3 additions & 0 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface Cookie
const NAME_EXTRA_CHARS = '!\#$%&\'*+\-.^_`|~';
const VALUE_EXTRA_CHARS = self::NAME_EXTRA_CHARS . '\/:=?\@()[\]{}<>';

/**
* @return string
*/
public function name(): string;

/**
Expand Down
15 changes: 9 additions & 6 deletions src/Cookie/CookieSetup.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand All @@ -20,9 +20,12 @@ class CookieSetup
{
private const FIVE_YEARS_IN_SEC = 157680000;

private $responseHeaders;
private $directives;
private ResponseHeaders $responseHeaders;
private array $directives;

/**
* @param ResponseHeaders $responseHeaders
*/
public function __construct(ResponseHeaders $responseHeaders)
{
$this->responseHeaders = $responseHeaders;
Expand Down Expand Up @@ -54,7 +57,7 @@ public function directives(array $directives = []): self
'MaxAge' => null,
'Secure' => false,
'HttpOnly' => false,
'SameSite' => false
'SameSite' => null
];

foreach (HeadersContextCookie::DIRECTIVE_NAMES as $name) {
Expand Down Expand Up @@ -93,7 +96,7 @@ public function cookie(string $name): Cookie
*
* @return Cookie
*/
public function permanentCookie($name): Cookie
public function permanentCookie(string $name): Cookie
{
$this->maxAge(self::FIVE_YEARS_IN_SEC);
return $this->cookie($name);
Expand All @@ -108,7 +111,7 @@ public function permanentCookie($name): Cookie
*
* @return Cookie
*/
public function sessionCookie($name): Cookie
public function sessionCookie(string $name): Cookie
{
$this->httpOnly();
$this->sameSite('Lax');
Expand Down
2 changes: 1 addition & 1 deletion src/Cookie/Exception/CookieAlreadySentException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand Down
2 changes: 1 addition & 1 deletion src/Cookie/Exception/IllegalCharactersException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand Down
15 changes: 10 additions & 5 deletions src/Cookie/HeadersContextCookie.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand All @@ -21,12 +21,17 @@ class HeadersContextCookie implements Cookie
{
const DIRECTIVE_NAMES = ['Domain', 'Path', 'Expires', 'MaxAge', 'Secure', 'HttpOnly', 'SameSite'];

private $name;
private $directives;
private $headers;
private string $name;
private array $directives;
private ResponseHeaders $headers;

private $sent = false;
private bool $sent = false;

/**
* @param string $name
* @param array $directives
* @param ResponseHeaders $headers
*/
public function __construct(string $name, array $directives, ResponseHeaders $headers)
{
$this->name = $this->validName($name);
Expand Down
5 changes: 5 additions & 0 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@

interface Header
{
/**
* @param MessageInterface $message
*
* @return MessageInterface New Message instance with modified headers
*/
public function addToMessage(MessageInterface $message): MessageInterface;
}
13 changes: 8 additions & 5 deletions src/Header/SetCookieHeader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand All @@ -17,15 +17,18 @@

class SetCookieHeader implements Header
{
private $header;
private string $headerValue;

public function __construct(string $header)
/**
* @param string $headerValue
*/
public function __construct(string $headerValue)
{
$this->header = $header;
$this->headerValue = $headerValue;
}

public function addToMessage(MessageInterface $message): MessageInterface
{
return $message->withAddedHeader('Set-Cookie', $this->header);
return $message->withAddedHeader('Set-Cookie', $this->headerValue);
}
}
15 changes: 13 additions & 2 deletions src/ResponseHeaders.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
Expand All @@ -20,7 +20,7 @@

final class ResponseHeaders implements MiddlewareInterface
{
private $headers = [];
private array $headers;

/**
* @param Header[] $headers
Expand All @@ -39,11 +39,22 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $response;
}

/**
* Adds Header that will modify server Response.
*
* @param Header $header
*/
public function push(Header $header): void
{
$this->headers[] = $header;
}

/**
* CookieSetup object can be used to configure Cookie header
* in server Response.
*
* @return CookieSetup
*/
public function cookieSetup(): CookieSetup
{
return new CookieSetup($this);
Expand Down
177 changes: 177 additions & 0 deletions tests/Doubles/DummyServerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php declare(strict_types=1);

/*
* This file is part of Polymorphine/Headers package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Polymorphine\Headers\Tests\Doubles;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;


class DummyServerRequest implements ServerRequestInterface
{
public ?UriInterface $uri;

public function __construct(UriInterface $uri = null)
{
$this->uri = $uri;
}

public function getMethod(): string
{
return 'GET';
}

public function getUri(): UriInterface
{
return $this->uri;
}

public function getRequestTarget(): string
{
return 'foo?bar=baz';
}

public function getProtocolVersion(): string
{
return '1.1';
}

public function withProtocolVersion($version): self
{
return $this;
}

public function getHeaders(): array
{
return [];
}

public function hasHeader($name): bool
{
return false;
}

public function getHeader($name): array
{
return [];
}

public function getHeaderLine($name): string
{
return '';
}

public function withHeader($name, $value): self
{
return $this;
}

public function withAddedHeader($name, $value): self
{
return $this;
}

public function withoutHeader($name): self
{
return $this;
}

public function getBody(): StreamInterface
{
return new DummyStream();
}

public function withBody(StreamInterface $body): self
{
return $this;
}

public function withRequestTarget($requestTarget): self
{
return $this;
}

public function withMethod($method): self
{
return $this;
}

public function withUri(UriInterface $uri, $preserveHost = false): self
{
return $this;
}

public function getServerParams(): array
{
return [];
}

public function getCookieParams(): array
{
return [];
}

public function withCookieParams(array $cookies): self
{
return $this;
}

public function getQueryParams(): array
{
return [];
}

public function withQueryParams(array $query): self
{
return $this;
}

public function getUploadedFiles(): array
{
return [];
}

public function withUploadedFiles(array $uploadedFiles): self
{
return $this;
}

public function getParsedBody(): array
{
return [];
}

public function withParsedBody($data): self
{
return $this;
}

public function getAttributes(): array
{
return [];
}

public function getAttribute($name, $default = null)
{
return $default;
}

public function withAttribute($name, $value): self
{
return $this;
}

public function withoutAttribute($name): self
{
return $this;
}
}
Loading

0 comments on commit ed43209

Please sign in to comment.