-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14 from tiny-blocks/release/2.1.3
Release/2.1.3
- Loading branch information
Showing
8 changed files
with
164 additions
and
60 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,29 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http; | ||
|
||
use TinyBlocks\Http\Internal\Header; | ||
|
||
/** | ||
* The Content-Type representation header is used to indicate the original media type | ||
* of the resource (prior to any content encoding applied for sending). | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type | ||
*/ | ||
enum HttpContentType: string implements Header | ||
{ | ||
case TEXT_HTML = 'text/html'; | ||
case TEXT_PLAIN = 'text/plain'; | ||
case APPLICATION_PDF = 'application/pdf'; | ||
case APPLICATION_JSON = 'application/json'; | ||
|
||
public function key(): string | ||
{ | ||
return 'Content-Type'; | ||
} | ||
|
||
public function value(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http; | ||
|
||
use TinyBlocks\Http\Internal\Header; | ||
|
||
/** | ||
* HTTP headers let the client and the server pass additional information with an HTTP request or response. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers | ||
*/ | ||
final class HttpHeaders | ||
{ | ||
private array $values = []; | ||
|
||
public function add(Header $header): HttpHeaders | ||
{ | ||
$key = $header->key(); | ||
$this->values['header'][$key] = sprintf('%s: %s', $key, $header->value()); | ||
|
||
return $this; | ||
} | ||
|
||
public function getHeader(): array | ||
{ | ||
return $this->values['header'] ?? []; | ||
} | ||
|
||
public function hasHeaders(): bool | ||
{ | ||
return !empty($this->values); | ||
} | ||
|
||
public function hasHeader(string $key): bool | ||
{ | ||
return !empty($this->getHeader()[$key]); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return $this->values; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal; | ||
|
||
interface Header | ||
{ | ||
public function key(): string; | ||
|
||
public function value(): string; | ||
} |
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,29 @@ | ||
<?php | ||
|
||
namespace TinyBlocks\Http\Internal; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use TinyBlocks\Http\HttpContentType; | ||
use TinyBlocks\Http\HttpHeaders; | ||
|
||
class HeadersTest extends TestCase | ||
{ | ||
public function testAddAndGetValues(): void | ||
{ | ||
$headers = (new HttpHeaders())->add(header: HttpContentType::APPLICATION_JSON); | ||
$expected = ['header' => ['Content-Type' => 'Content-Type: application/json']]; | ||
|
||
self::assertEquals($expected, $headers->toArray()); | ||
} | ||
|
||
public function testAddAndGetUniqueValues(): void | ||
{ | ||
$headers = (new HttpHeaders()) | ||
->add(header: HttpContentType::TEXT_HTML) | ||
->add(header: HttpContentType::APPLICATION_PDF); | ||
|
||
$expected = ['header' => ['Content-Type' => 'Content-Type: application/pdf']]; | ||
|
||
self::assertEquals($expected, $headers->toArray()); | ||
} | ||
} |
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