From a0d97a107609f44c7d89fe75890fafe4862867a3 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:15:51 +0500 Subject: [PATCH 01/12] improve tests --- ...eaderAccessControlAllowCredentialsTest.php | 24 ++----- tests/HeaderAccessControlAllowHeadersTest.php | 24 ------- tests/HeaderAccessControlAllowMethodsTest.php | 24 ------- tests/HeaderAccessControlAllowOriginTest.php | 26 -------- .../HeaderAccessControlExposeHeadersTest.php | 24 ------- tests/HeaderAccessControlMaxAgeTest.php | 24 ------- tests/HeaderAgeTest.php | 24 ------- tests/HeaderAllowTest.php | 24 ------- tests/HeaderCacheControlTest.php | 24 ------- tests/HeaderClearSiteDataTest.php | 24 ------- tests/HeaderConnectionTest.php | 24 ------- tests/HeaderContentDispositionTest.php | 24 ------- tests/HeaderContentEncodingTest.php | 24 ------- tests/HeaderContentLanguageTest.php | 24 ------- tests/HeaderContentLengthTest.php | 24 ------- tests/HeaderContentLocationTest.php | 26 -------- tests/HeaderContentMD5Test.php | 24 ------- tests/HeaderContentRangeTest.php | 24 ------- ...derContentSecurityPolicyReportOnlyTest.php | 24 ------- tests/HeaderContentSecurityPolicyTest.php | 24 ------- tests/HeaderContentTypeTest.php | 24 ------- tests/HeaderCookieTest.php | 62 +++++++++++++++++++ tests/HeaderDateTest.php | 26 -------- tests/HeaderEtagTest.php | 24 ------- tests/HeaderExpiresTest.php | 26 -------- tests/HeaderKeepAliveTest.php | 24 ------- tests/HeaderLastModifiedTest.php | 26 -------- tests/HeaderLinkTest.php | 26 -------- tests/HeaderLocationTest.php | 26 -------- tests/HeaderRefreshTest.php | 26 -------- tests/HeaderRetryAfterTest.php | 26 -------- tests/HeaderSetCookieTest.php | 24 ------- tests/HeaderSunsetTest.php | 26 -------- tests/HeaderTrailerTest.php | 24 ------- tests/HeaderTransferEncodingTest.php | 24 ------- tests/HeaderVaryTest.php | 24 ------- tests/HeaderWWWAuthenticateTest.php | 24 ------- tests/HeaderWarningTest.php | 24 ------- 38 files changed, 68 insertions(+), 902 deletions(-) create mode 100644 tests/HeaderCookieTest.php diff --git a/tests/HeaderAccessControlAllowCredentialsTest.php b/tests/HeaderAccessControlAllowCredentialsTest.php index e63a371..afbbce7 100644 --- a/tests/HeaderAccessControlAllowCredentialsTest.php +++ b/tests/HeaderAccessControlAllowCredentialsTest.php @@ -36,27 +36,15 @@ public function testToString() $this->assertEquals('Access-Control-Allow-Credentials: true', (string) $header); } - public function testSetToMessage() + public function testIteration() { $header = new HeaderAccessControlAllowCredentials(); - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); + $parameters = \iterator_to_array($header); - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAccessControlAllowCredentials(); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); + $this->assertSame([ + $header->getFieldName(), + $header->getFieldValue(), + ], $parameters); } } diff --git a/tests/HeaderAccessControlAllowHeadersTest.php b/tests/HeaderAccessControlAllowHeadersTest.php index 527ff2b..d96c2de 100644 --- a/tests/HeaderAccessControlAllowHeadersTest.php +++ b/tests/HeaderAccessControlAllowHeadersTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Access-Control-Allow-Headers: value-first, value-second, value-third', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAccessControlAllowHeaders('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAccessControlAllowHeaders('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAccessControlAllowMethodsTest.php b/tests/HeaderAccessControlAllowMethodsTest.php index 5d9429b..f086c36 100644 --- a/tests/HeaderAccessControlAllowMethodsTest.php +++ b/tests/HeaderAccessControlAllowMethodsTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Access-Control-Allow-Methods: HEAD, GET, POST', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAccessControlAllowMethods('head'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAccessControlAllowMethods('head'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAccessControlAllowOriginTest.php b/tests/HeaderAccessControlAllowOriginTest.php index 987773e..bba039a 100644 --- a/tests/HeaderAccessControlAllowOriginTest.php +++ b/tests/HeaderAccessControlAllowOriginTest.php @@ -171,30 +171,4 @@ public function testToStringWithValidOrigin() $this->assertEquals('Access-Control-Allow-Origin: http://localhost:3000', (string) $header); } - - public function testSetToMessage() - { - $uri = new Uri('http://localhost'); - $header = new HeaderAccessControlAllowOrigin($uri); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $uri = new Uri('http://localhost'); - $header = new HeaderAccessControlAllowOrigin($uri); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAccessControlExposeHeadersTest.php b/tests/HeaderAccessControlExposeHeadersTest.php index b5e26be..7af748e 100644 --- a/tests/HeaderAccessControlExposeHeadersTest.php +++ b/tests/HeaderAccessControlExposeHeadersTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Access-Control-Expose-Headers: value-first, value-second, value-third', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAccessControlExposeHeaders('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAccessControlExposeHeaders('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAccessControlMaxAgeTest.php b/tests/HeaderAccessControlMaxAgeTest.php index 86d6842..6551a05 100644 --- a/tests/HeaderAccessControlMaxAgeTest.php +++ b/tests/HeaderAccessControlMaxAgeTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('Access-Control-Max-Age: 86400', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAccessControlMaxAge(86400); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAccessControlMaxAge(86400); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAgeTest.php b/tests/HeaderAgeTest.php index d9b6810..c4a3857 100644 --- a/tests/HeaderAgeTest.php +++ b/tests/HeaderAgeTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('Age: 0', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAge(0); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAge(0); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderAllowTest.php b/tests/HeaderAllowTest.php index f29d789..7fb6a00 100644 --- a/tests/HeaderAllowTest.php +++ b/tests/HeaderAllowTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Allow: HEAD, GET, POST', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderAllow('head'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderAllow('head'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderCacheControlTest.php b/tests/HeaderCacheControlTest.php index 00b3d47..59463a2 100644 --- a/tests/HeaderCacheControlTest.php +++ b/tests/HeaderCacheControlTest.php @@ -183,28 +183,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals('Cache-Control: name-1, name-2=token, name-3="quoted string"', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderCacheControl([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderCacheControl([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderClearSiteDataTest.php b/tests/HeaderClearSiteDataTest.php index 7e4bee8..ca14f9c 100644 --- a/tests/HeaderClearSiteDataTest.php +++ b/tests/HeaderClearSiteDataTest.php @@ -100,28 +100,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Clear-Site-Data: "value-first", "value-second", "value-third"', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderClearSiteData('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderClearSiteData('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderConnectionTest.php b/tests/HeaderConnectionTest.php index 70ca260..2639966 100644 --- a/tests/HeaderConnectionTest.php +++ b/tests/HeaderConnectionTest.php @@ -73,28 +73,4 @@ public function testToString() $this->assertEquals('Connection: value', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderConnection('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderConnection('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentDispositionTest.php b/tests/HeaderContentDispositionTest.php index 590a10b..cac4e42 100644 --- a/tests/HeaderContentDispositionTest.php +++ b/tests/HeaderContentDispositionTest.php @@ -215,28 +215,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentDisposition('type'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentDisposition('type'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentEncodingTest.php b/tests/HeaderContentEncodingTest.php index 38a92fc..d64981e 100644 --- a/tests/HeaderContentEncodingTest.php +++ b/tests/HeaderContentEncodingTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('Content-Encoding: value', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentEncoding('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentEncoding('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentLanguageTest.php b/tests/HeaderContentLanguageTest.php index 5c95a96..40e337a 100644 --- a/tests/HeaderContentLanguageTest.php +++ b/tests/HeaderContentLanguageTest.php @@ -132,28 +132,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Content-Language: value-first, value-second, value-third', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentLanguage('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentLanguage('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentLengthTest.php b/tests/HeaderContentLengthTest.php index 8750a87..8b89220 100644 --- a/tests/HeaderContentLengthTest.php +++ b/tests/HeaderContentLengthTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('Content-Length: 0', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentLength(0); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentLength(0); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentLocationTest.php b/tests/HeaderContentLocationTest.php index 5827dfa..e3fa385 100644 --- a/tests/HeaderContentLocationTest.php +++ b/tests/HeaderContentLocationTest.php @@ -66,30 +66,4 @@ public function testToString() $this->assertEquals('Content-Location: /', (string) $header); } - - public function testSetToMessage() - { - $home = new Uri('/'); - $header = new HeaderContentLocation($home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $home = new Uri('/'); - $header = new HeaderContentLocation($home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentMD5Test.php b/tests/HeaderContentMD5Test.php index b939195..0738b46 100644 --- a/tests/HeaderContentMD5Test.php +++ b/tests/HeaderContentMD5Test.php @@ -70,28 +70,4 @@ public function testToString() $this->assertEquals('Content-MD5: ' . self::TEST_MD5_DIGEST_1, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentMD5(self::TEST_MD5_DIGEST_1); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentMD5(self::TEST_MD5_DIGEST_1); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentRangeTest.php b/tests/HeaderContentRangeTest.php index 19d6cbb..05601a6 100644 --- a/tests/HeaderContentRangeTest.php +++ b/tests/HeaderContentRangeTest.php @@ -90,28 +90,4 @@ public function testToString() $this->assertEquals('Content-Range: bytes 0-1/2', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentRange(0, 1, 2); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentRange(0, 1, 2); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentSecurityPolicyReportOnlyTest.php b/tests/HeaderContentSecurityPolicyReportOnlyTest.php index 93feb5b..3cbdde7 100644 --- a/tests/HeaderContentSecurityPolicyReportOnlyTest.php +++ b/tests/HeaderContentSecurityPolicyReportOnlyTest.php @@ -171,28 +171,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentSecurityPolicyReportOnly([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentSecurityPolicyReportOnly([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentSecurityPolicyTest.php b/tests/HeaderContentSecurityPolicyTest.php index f010e7a..a48c0a9 100644 --- a/tests/HeaderContentSecurityPolicyTest.php +++ b/tests/HeaderContentSecurityPolicyTest.php @@ -169,28 +169,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals('Content-Security-Policy: name-1; name-2 value-2; name-3 value-3', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentSecurityPolicy([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentSecurityPolicy([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderContentTypeTest.php b/tests/HeaderContentTypeTest.php index 7cb2c56..e20926e 100644 --- a/tests/HeaderContentTypeTest.php +++ b/tests/HeaderContentTypeTest.php @@ -215,28 +215,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderContentType('type'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderContentType('type'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderCookieTest.php b/tests/HeaderCookieTest.php new file mode 100644 index 0000000..4afd1e3 --- /dev/null +++ b/tests/HeaderCookieTest.php @@ -0,0 +1,62 @@ +assertInstanceOf(HeaderInterface::class, $header); + } + + public function testConstructor() + { + $header = new HeaderCookie(['foo', 'bar']); + + $this->assertSame(['foo', 'bar'], $header->getValue()); + } + + public function testSetValue() + { + $header = new HeaderCookie(); + + $this->assertInstanceOf(HeaderCookie::class, $header->setValue(['foo', 'bar'])); + + $this->assertSame(['foo', 'bar'], $header->getValue()); + } + + public function testGetFieldName() + { + $header = new HeaderCookie(); + + $this->assertEquals('Cookie', $header->getFieldName()); + } + + public function testGetFieldValue() + { + $header = new HeaderCookie([ + 'foo' => 'bar', + 'bar' => 'baz', + 'baz' => ['qux'], + ]); + + $this->assertEquals('foo=bar; bar=baz; baz%5B0%5D=qux', $header->getFieldValue()); + } + + public function testToString() + { + $header = new HeaderCookie([ + 'foo' => 'bar', + 'bar' => 'baz', + 'baz' => ['qux'], + ]); + + $this->assertEquals('Cookie: foo=bar; bar=baz; baz%5B0%5D=qux', (string) $header); + } +} diff --git a/tests/HeaderDateTest.php b/tests/HeaderDateTest.php index 711cf61..e242ebe 100644 --- a/tests/HeaderDateTest.php +++ b/tests/HeaderDateTest.php @@ -67,30 +67,4 @@ public function testToString() $this->assertEquals(\sprintf('Date: %s', $now->format(\DateTime::RFC822)), (string) $header); } - - public function testSetToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderDate($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderDate($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderEtagTest.php b/tests/HeaderEtagTest.php index 530aab6..13add1d 100644 --- a/tests/HeaderEtagTest.php +++ b/tests/HeaderEtagTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('ETag: "value"', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderEtag('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderEtag('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderExpiresTest.php b/tests/HeaderExpiresTest.php index c27c904..c188ca4 100644 --- a/tests/HeaderExpiresTest.php +++ b/tests/HeaderExpiresTest.php @@ -67,30 +67,4 @@ public function testToString() $this->assertEquals(\sprintf('Expires: %s', $now->format(\DateTime::RFC822)), (string) $header); } - - public function testSetToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderExpires($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderExpires($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderKeepAliveTest.php b/tests/HeaderKeepAliveTest.php index d4eb403..58d8170 100644 --- a/tests/HeaderKeepAliveTest.php +++ b/tests/HeaderKeepAliveTest.php @@ -183,28 +183,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals('Keep-Alive: name-1, name-2=token, name-3="quoted string"', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderKeepAlive([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderKeepAlive([]); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderLastModifiedTest.php b/tests/HeaderLastModifiedTest.php index e817b3d..6874ab0 100644 --- a/tests/HeaderLastModifiedTest.php +++ b/tests/HeaderLastModifiedTest.php @@ -67,30 +67,4 @@ public function testToString() $this->assertEquals(\sprintf('Last-Modified: %s', $now->format(\DateTime::RFC822)), (string) $header); } - - public function testSetToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderLastModified($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderLastModified($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderLinkTest.php b/tests/HeaderLinkTest.php index d5feb31..07feb58 100644 --- a/tests/HeaderLinkTest.php +++ b/tests/HeaderLinkTest.php @@ -242,30 +242,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $uri = new Uri('/'); - $header = new HeaderLink($uri); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $uri = new Uri('/'); - $header = new HeaderLink($uri); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderLocationTest.php b/tests/HeaderLocationTest.php index 8326bb0..86e67bc 100644 --- a/tests/HeaderLocationTest.php +++ b/tests/HeaderLocationTest.php @@ -66,30 +66,4 @@ public function testToString() $this->assertEquals('Location: /', (string) $header); } - - public function testSetToMessage() - { - $home = new Uri('/'); - $header = new HeaderLocation($home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $home = new Uri('/'); - $header = new HeaderLocation($home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderRefreshTest.php b/tests/HeaderRefreshTest.php index 898aa1a..e91778b 100644 --- a/tests/HeaderRefreshTest.php +++ b/tests/HeaderRefreshTest.php @@ -106,30 +106,4 @@ public function testToString() $this->assertEquals('Refresh: 0; url=/', (string) $header); } - - public function testSetToMessage() - { - $home = new Uri('/'); - $header = new HeaderRefresh(0, $home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $home = new Uri('/'); - $header = new HeaderRefresh(0, $home); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderRetryAfterTest.php b/tests/HeaderRetryAfterTest.php index 1fee8d7..9e3c7b3 100644 --- a/tests/HeaderRetryAfterTest.php +++ b/tests/HeaderRetryAfterTest.php @@ -67,30 +67,4 @@ public function testToString() $this->assertEquals(\sprintf('Retry-After: %s', $now->format(\DateTime::RFC822)), (string) $header); } - - public function testSetToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderRetryAfter($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderRetryAfter($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderSetCookieTest.php b/tests/HeaderSetCookieTest.php index 55770f5..52de0f8 100644 --- a/tests/HeaderSetCookieTest.php +++ b/tests/HeaderSetCookieTest.php @@ -329,28 +329,4 @@ public function testToString() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderSetCookie('name', 'value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderSetCookie('name', 'value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderSunsetTest.php b/tests/HeaderSunsetTest.php index 498e86d..213127c 100644 --- a/tests/HeaderSunsetTest.php +++ b/tests/HeaderSunsetTest.php @@ -67,30 +67,4 @@ public function testToString() $this->assertEquals(\sprintf('Sunset: %s', $now->format(\DateTime::RFC822)), (string) $header); } - - public function testSetToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderSunset($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $now = new \DateTime('now'); - $header = new HeaderSunset($now); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderTrailerTest.php b/tests/HeaderTrailerTest.php index 324cd4e..0b417c4 100644 --- a/tests/HeaderTrailerTest.php +++ b/tests/HeaderTrailerTest.php @@ -67,28 +67,4 @@ public function testToString() $this->assertEquals('Trailer: value', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderTrailer('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderTrailer('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderTransferEncodingTest.php b/tests/HeaderTransferEncodingTest.php index 5a7d6b3..c9bc02a 100644 --- a/tests/HeaderTransferEncodingTest.php +++ b/tests/HeaderTransferEncodingTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Transfer-Encoding: value-first, value-second, value-third', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderTransferEncoding('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderTransferEncoding('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderVaryTest.php b/tests/HeaderVaryTest.php index 9ff616b..275eaef 100644 --- a/tests/HeaderVaryTest.php +++ b/tests/HeaderVaryTest.php @@ -116,28 +116,4 @@ public function testToStringWithSeveralValues() $this->assertEquals('Vary: value-first, value-second, value-third', (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderVary('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderVary('value'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderWWWAuthenticateTest.php b/tests/HeaderWWWAuthenticateTest.php index 82d7cf9..2cfaf47 100644 --- a/tests/HeaderWWWAuthenticateTest.php +++ b/tests/HeaderWWWAuthenticateTest.php @@ -229,28 +229,4 @@ public function testToStringWithSeveralParameters() $this->assertEquals($expected, (string) $header); } - - public function testSetToMessage() - { - $header = new HeaderWWWAuthenticate('scheme'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderWWWAuthenticate('scheme'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } diff --git a/tests/HeaderWarningTest.php b/tests/HeaderWarningTest.php index 826623a..5d9b8c0 100644 --- a/tests/HeaderWarningTest.php +++ b/tests/HeaderWarningTest.php @@ -229,28 +229,4 @@ public function testToStringWithDate() (string) $header ); } - - public function testSetToMessage() - { - $header = new HeaderWarning(199, 'agent', 'text'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->setToMessage($message); - - $this->assertEquals([$header->getFieldValue()], $message->getHeader($header->getFieldName())); - } - - public function testAddToMessage() - { - $header = new HeaderWarning(199, 'agent', 'text'); - - $message = (new \Sunrise\Http\Message\ResponseFactory)->createResponse(); - $message = $message->withHeader($header->getFieldName(), 'foo bar baz'); - - $message = $header->addToMessage($message); - - $this->assertEquals(['foo bar baz', $header->getFieldValue()], $message->getHeader($header->getFieldName())); - } } From 6d1025afd8e8d72b4408e8520dc580d281f95fa3 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:16:04 +0500 Subject: [PATCH 02/12] improve code --- src/AbstractHeader.php | 39 +++--- src/HeaderAccessControlAllowCredentials.php | 4 +- src/HeaderAccessControlAllowHeaders.php | 37 ++++-- src/HeaderAccessControlAllowMethods.php | 40 ++++-- src/HeaderAccessControlAllowOrigin.php | 46 ++++--- src/HeaderAccessControlExposeHeaders.php | 37 ++++-- src/HeaderAccessControlMaxAge.php | 26 ++-- src/HeaderAge.php | 26 ++-- src/HeaderAllow.php | 40 ++++-- src/HeaderCacheControl.php | 64 ++++++---- src/HeaderClearSiteData.php | 39 ++++-- src/HeaderConnection.php | 27 ++-- src/HeaderContentDisposition.php | 59 +++++---- src/HeaderContentEncoding.php | 27 ++-- src/HeaderContentLanguage.php | 37 ++++-- src/HeaderContentLength.php | 26 ++-- src/HeaderContentLocation.php | 4 +- src/HeaderContentMD5.php | 27 ++-- src/HeaderContentRange.php | 23 +++- src/HeaderContentSecurityPolicy.php | 56 ++++++--- src/HeaderContentSecurityPolicyReportOnly.php | 2 +- src/HeaderContentType.php | 59 +++++---- src/HeaderCookie.php | 16 ++- src/HeaderDate.php | 28 +++-- src/HeaderEtag.php | 29 +++-- src/HeaderExpires.php | 28 +++-- src/HeaderKeepAlive.php | 66 ++++++---- src/HeaderLastModified.php | 28 +++-- src/HeaderLink.php | 45 ++++--- src/HeaderLocation.php | 4 +- src/HeaderRefresh.php | 21 ++-- src/HeaderRetryAfter.php | 28 +++-- src/HeaderSetCookie.php | 118 ++++++++++-------- src/HeaderSunset.php | 28 +++-- src/HeaderTrailer.php | 27 ++-- src/HeaderTransferEncoding.php | 37 ++++-- src/HeaderVary.php | 37 ++++-- src/HeaderWWWAuthenticate.php | 64 ++++++---- src/HeaderWarning.php | 56 +++++---- 39 files changed, 914 insertions(+), 491 deletions(-) diff --git a/src/AbstractHeader.php b/src/AbstractHeader.php index 018c24a..1b0ad13 100644 --- a/src/AbstractHeader.php +++ b/src/AbstractHeader.php @@ -14,44 +14,41 @@ /** * Import classes */ -use Psr\Http\Message\MessageInterface; +use ArrayIterator; +use IteratorAggregate; +use Traversable; + +/** + * Import functions + */ +use function sprintf; /** * AbstractHeader */ -abstract class AbstractHeader implements HeaderInterface +abstract class AbstractHeader implements HeaderInterface, IteratorAggregate { /** - * {@inheritDoc} + * {@inheritdoc} */ - public function setToMessage(MessageInterface $message) : MessageInterface + public function __toString() : string { - return $message->withHeader( + return sprintf( + '%s: %s', $this->getFieldName(), $this->getFieldValue() ); } /** - * {@inheritDoc} + * {@inheritdoc} */ - public function addToMessage(MessageInterface $message) : MessageInterface + public function getIterator() : Traversable { - return $message->withAddedHeader( + return new ArrayIterator([ $this->getFieldName(), - $this->getFieldValue() - ); - } - - /** - * {@inheritDoc} - */ - public function __toString() - { - return \sprintf('%s: %s', - $this->getFieldName(), - $this->getFieldValue() - ); + $this->getFieldValue(), + ]); } } diff --git a/src/HeaderAccessControlAllowCredentials.php b/src/HeaderAccessControlAllowCredentials.php index 2eaf5b1..b467eb5 100644 --- a/src/HeaderAccessControlAllowCredentials.php +++ b/src/HeaderAccessControlAllowCredentials.php @@ -20,7 +20,7 @@ class HeaderAccessControlAllowCredentials extends AbstractHeader implements Head { /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -28,7 +28,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderAccessControlAllowHeaders.php b/src/HeaderAccessControlAllowHeaders.php index 19888c9..98c3b5b 100644 --- a/src/HeaderAccessControlAllowHeaders.php +++ b/src/HeaderAccessControlAllowHeaders.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderAccessControlAllowHeaders * @@ -22,7 +35,7 @@ class HeaderAccessControlAllowHeaders extends AbstractHeader implements HeaderIn /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,15 +56,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -63,11 +78,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -83,7 +98,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -91,10 +106,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderAccessControlAllowMethods.php b/src/HeaderAccessControlAllowMethods.php index ab0e05e..9211640 100644 --- a/src/HeaderAccessControlAllowMethods.php +++ b/src/HeaderAccessControlAllowMethods.php @@ -11,6 +11,20 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; +use function strtoupper; + /** * HeaderAccessControlAllowMethods * @@ -22,7 +36,7 @@ class HeaderAccessControlAllowMethods extends AbstractHeader implements HeaderIn /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,18 +57,20 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } - $oneOf = \strtoupper($oneOf); + $oneOf = strtoupper($oneOf); $this->value[$oneOf] = true; } @@ -65,11 +81,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -85,7 +101,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -93,10 +109,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderAccessControlAllowOrigin.php b/src/HeaderAccessControlAllowOrigin.php index d525851..21acd58 100644 --- a/src/HeaderAccessControlAllowOrigin.php +++ b/src/HeaderAccessControlAllowOrigin.php @@ -14,8 +14,14 @@ /** * Import classes */ +use InvalidArgumentException; use Psr\Http\Message\UriInterface; +/** + * Import functions + */ +use function sprintf; + /** * HeaderAccessControlAllowOrigin * @@ -27,14 +33,14 @@ class HeaderAccessControlAllowOrigin extends AbstractHeader implements HeaderInt /** * URI for the header field-value * - * @var null|UriInterface + * @var UriInterface|null */ protected $uri; /** * Constructor of the class * - * @param null|UriInterface $uri + * @param UriInterface|null $uri */ public function __construct(?UriInterface $uri) { @@ -44,18 +50,20 @@ public function __construct(?UriInterface $uri) /** * Sets URI for the header field-value * - * @param null|UriInterface $uri + * @param UriInterface|null $uri * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setUri(?UriInterface $uri) : self { - if (! (\is_null($uri) || (\strlen($uri->getScheme()) && \strlen($uri->getHost())))) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %d" is not valid', $this->getFieldName(), (string) $uri) - ); + if (isset($uri) && ('' === $uri->getScheme() || '' === $uri->getHost())) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %d" is not valid', + $this->getFieldName(), + (string) $uri + )); } $this->uri = $uri; @@ -66,7 +74,7 @@ public function setUri(?UriInterface $uri) : self /** * Gets URI for the header field-value * - * @return null|UriInterface + * @return UriInterface|null */ public function getUri() : ?UriInterface { @@ -74,7 +82,7 @@ public function getUri() : ?UriInterface } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -82,22 +90,20 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $uri = $this->getUri(); - if ($uri instanceof UriInterface) { - $value = $uri->getScheme() . ':'; - $value .= '//' . $uri->getHost(); - - if (! (null === $uri->getPort())) { - $value .= ':' . $uri->getPort(); - } + if (null === $uri) { + return '*'; + } - return $value; + $value = $uri->getScheme() . '://' . $uri->getHost(); + if (null !== $uri->getPort()) { + $value .= ':' . $uri->getPort(); } - return '*'; + return $value; } } diff --git a/src/HeaderAccessControlExposeHeaders.php b/src/HeaderAccessControlExposeHeaders.php index 7336d9c..254388d 100644 --- a/src/HeaderAccessControlExposeHeaders.php +++ b/src/HeaderAccessControlExposeHeaders.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderAccessControlExposeHeaders * @@ -22,7 +35,7 @@ class HeaderAccessControlExposeHeaders extends AbstractHeader implements HeaderI /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,15 +56,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -63,11 +78,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -83,7 +98,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -91,10 +106,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderAccessControlMaxAge.php b/src/HeaderAccessControlMaxAge.php index 4ac141b..bb47199 100644 --- a/src/HeaderAccessControlMaxAge.php +++ b/src/HeaderAccessControlMaxAge.php @@ -11,6 +11,16 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function sprintf; + /** * HeaderAccessControlMaxAge * @@ -43,14 +53,16 @@ public function __construct(int $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(int $value) : self { if (! ($value >= 1 || $value === -1)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %d" is not valid', $this->getFieldName(), $value) - ); + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %d" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +81,7 @@ public function getValue() : int } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,10 +89,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('%d', $this->getValue()); + return sprintf('%d', $this->getValue()); } } diff --git a/src/HeaderAge.php b/src/HeaderAge.php index c46515d..e460e4f 100644 --- a/src/HeaderAge.php +++ b/src/HeaderAge.php @@ -11,6 +11,16 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function sprintf; + /** * HeaderAge * @@ -43,14 +53,16 @@ public function __construct(int $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(int $value) : self { if (! ($value >= 0)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %d" is not valid', $this->getFieldName(), $value) - ); + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %d" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +81,7 @@ public function getValue() : int } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,10 +89,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('%d', $this->getValue()); + return sprintf('%d', $this->getValue()); } } diff --git a/src/HeaderAllow.php b/src/HeaderAllow.php index 57df248..6b924cf 100644 --- a/src/HeaderAllow.php +++ b/src/HeaderAllow.php @@ -11,6 +11,20 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; +use function strtoupper; + /** * HeaderAllow * @@ -22,7 +36,7 @@ class HeaderAllow extends AbstractHeader implements HeaderInterface /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,18 +57,20 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } - $oneOf = \strtoupper($oneOf); + $oneOf = strtoupper($oneOf); $this->value[$oneOf] = true; } @@ -65,11 +81,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -85,7 +101,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -93,10 +109,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderCacheControl.php b/src/HeaderCacheControl.php index f5759b6..56ee859 100644 --- a/src/HeaderCacheControl.php +++ b/src/HeaderCacheControl.php @@ -11,6 +11,18 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderCacheControl * @@ -22,7 +34,7 @@ class HeaderCacheControl extends AbstractHeader implements HeaderInterface /** * The header parameters * - * @var array + * @var array */ protected $parameters = []; @@ -44,20 +56,24 @@ public function __construct(array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -68,7 +84,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the header parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -84,7 +100,7 @@ public function setParameters(array $parameters) : self /** * Gets the header parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -104,7 +120,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -112,26 +128,26 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $parameters = []; foreach ($this->getParameters() as $name => $value) { - $parameter = $name; - - if (! (\strlen($value) === 0)) { - // Example: max-age=31536000 - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { - $value = '"' . $value . '"'; - } + if ('' === $value) { + $parameters[] = $name; + continue; + } - $parameter .= '=' . $value; + // e.g.: max-age=31536000 + if (preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { + $parameters[] = $name . '=' . $value; + continue; } - $parameters[] = $parameter; + $parameters[] = $name . '="' . $value . '"'; } - return \implode(', ', $parameters); + return implode(', ', $parameters); } } diff --git a/src/HeaderClearSiteData.php b/src/HeaderClearSiteData.php index 5bafdcf..0f889d4 100644 --- a/src/HeaderClearSiteData.php +++ b/src/HeaderClearSiteData.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderClearSiteData * @@ -22,7 +35,7 @@ class HeaderClearSiteData extends AbstractHeader implements HeaderInterface /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,15 +56,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -63,11 +78,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -83,7 +98,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -91,15 +106,15 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $directives = []; foreach ($this->getValue() as $directive) { - $directives[] = \sprintf('"%s"', $directive); + $directives[] = sprintf('"%s"', $directive); } - return \implode(', ', $directives); + return implode(', ', $directives); } } diff --git a/src/HeaderConnection.php b/src/HeaderConnection.php index 55f318e..02bef45 100644 --- a/src/HeaderConnection.php +++ b/src/HeaderConnection.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderConnection * @@ -49,14 +60,16 @@ public function __construct(string $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $value) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -75,7 +88,7 @@ public function getValue() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -83,7 +96,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderContentDisposition.php b/src/HeaderContentDisposition.php index 78c4ce9..2296881 100644 --- a/src/HeaderContentDisposition.php +++ b/src/HeaderContentDisposition.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderContentDisposition * @@ -29,7 +40,7 @@ class HeaderContentDisposition extends AbstractHeader implements HeaderInterface /** * The content disposition-type parameters * - * @var array + * @var array */ protected $parameters = []; @@ -37,7 +48,7 @@ class HeaderContentDisposition extends AbstractHeader implements HeaderInterface * Constructor of the class * * @param string $type - * @param array $parameters + * @param array $parameters */ public function __construct(string $type, array $parameters = []) { @@ -52,14 +63,16 @@ public function __construct(string $type, array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setType(string $type) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $type)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $type) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $type)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $type + )); } $this->type = $type; @@ -75,20 +88,24 @@ public function setType(string $type) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -99,7 +116,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the content disposition-type parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -125,7 +142,7 @@ public function getType() : string /** * Gets the content disposition-type parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -145,7 +162,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -153,13 +170,13 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $r = $this->getType(); foreach ($this->getParameters() as $name => $value) { - $r .= \sprintf('; %s="%s"', $name, $value); + $r .= sprintf('; %s="%s"', $name, $value); } return $r; diff --git a/src/HeaderContentEncoding.php b/src/HeaderContentEncoding.php index 81f1214..58bb307 100644 --- a/src/HeaderContentEncoding.php +++ b/src/HeaderContentEncoding.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderContentEncoding * @@ -43,14 +54,16 @@ public function __construct(string $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $value) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +82,7 @@ public function getValue() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,7 +90,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderContentLanguage.php b/src/HeaderContentLanguage.php index aefc0e7..6d7b528 100644 --- a/src/HeaderContentLanguage.php +++ b/src/HeaderContentLanguage.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderContentLanguage * @@ -31,7 +44,7 @@ class HeaderContentLanguage extends AbstractHeader implements HeaderInterface /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -52,15 +65,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(self::VALID_LANGUAGE_TAG, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(self::VALID_LANGUAGE_TAG, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -72,11 +87,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -92,7 +107,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -100,10 +115,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderContentLength.php b/src/HeaderContentLength.php index f7677d1..2b111ad 100644 --- a/src/HeaderContentLength.php +++ b/src/HeaderContentLength.php @@ -11,6 +11,16 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function sprintf; + /** * HeaderContentLength * @@ -43,14 +53,16 @@ public function __construct(int $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(int $value) : self { if (! ($value >= 0)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %d" is not valid', $this->getFieldName(), $value) - ); + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %d" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +81,7 @@ public function getValue() : int } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,10 +89,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('%d', $this->getValue()); + return sprintf('%d', $this->getValue()); } } diff --git a/src/HeaderContentLocation.php b/src/HeaderContentLocation.php index ca1a9f4..43c6e82 100644 --- a/src/HeaderContentLocation.php +++ b/src/HeaderContentLocation.php @@ -66,7 +66,7 @@ public function getUri() : UriInterface } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -74,7 +74,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderContentMD5.php b/src/HeaderContentMD5.php index 7896b34..417ba7e 100644 --- a/src/HeaderContentMD5.php +++ b/src/HeaderContentMD5.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderContentMD5 * @@ -52,14 +63,16 @@ public function __construct(string $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string $value) : self { - if (! \preg_match(self::VALID_NON_STRICT_MD5_DIGEST, $value)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $value) - ); + if (!preg_match(self::VALID_NON_STRICT_MD5_DIGEST, $value)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -78,7 +91,7 @@ public function getValue() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -86,7 +99,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderContentRange.php b/src/HeaderContentRange.php index 58bc899..66d87c0 100644 --- a/src/HeaderContentRange.php +++ b/src/HeaderContentRange.php @@ -11,6 +11,16 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function sprintf; + /** * HeaderContentRange * @@ -61,19 +71,19 @@ public function __construct(int $firstBytePosition, int $lastBytePosition, int $ * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setRange(int $firstBytePosition, int $lastBytePosition, int $instanceLength) : self { if (! ($firstBytePosition <= $lastBytePosition)) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( 'The "first-byte-pos" value of the content range ' . 'must be less than or equal to the "last-byte-pos" value' ); } if (! ($lastBytePosition < $instanceLength)) { - throw new \InvalidArgumentException( + throw new InvalidArgumentException( 'The "last-byte-pos" value of the content range ' . 'must be less than the "instance-length" value' ); @@ -117,7 +127,7 @@ public function getInstanceLength() : int } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -125,11 +135,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('bytes %d-%d/%d', + return sprintf( + 'bytes %d-%d/%d', $this->getFirstBytePosition(), $this->getLastBytePosition(), $this->getInstanceLength() diff --git a/src/HeaderContentSecurityPolicy.php b/src/HeaderContentSecurityPolicy.php index 239fa27..8c6f2dc 100644 --- a/src/HeaderContentSecurityPolicy.php +++ b/src/HeaderContentSecurityPolicy.php @@ -11,6 +11,18 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderContentSecurityPolicy * @@ -30,14 +42,14 @@ class HeaderContentSecurityPolicy extends AbstractHeader implements HeaderInterf /** * The header parameters * - * @var array + * @var array */ protected $parameters = []; /** * Constructor of the class * - * @param array $parameters + * @param array $parameters */ public function __construct(array $parameters = []) { @@ -52,20 +64,24 @@ public function __construct(array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(self::VALID_CONTENT_SECURITY_POLICY_DIRECTIVE_NAME, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(self::VALID_CONTENT_SECURITY_POLICY_DIRECTIVE_NAME, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(self::VALID_CONTENT_SECURITY_POLICY_DIRECTIVE_VALUE, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(self::VALID_CONTENT_SECURITY_POLICY_DIRECTIVE_VALUE, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -76,7 +92,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the header parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -92,7 +108,7 @@ public function setParameters(array $parameters) : self /** * Gets the header parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -112,7 +128,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -120,20 +136,20 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $parameters = []; foreach ($this->getParameters() as $name => $value) { - $parameter = $name; - if (! (\strlen($value) === 0)) { - $parameter .= ' ' . $value; + if ('' === $value) { + $parameters[] = $name; + continue; } - $parameters[] = $parameter; + $parameters[] = $name . ' ' . $value; } - return \implode('; ', $parameters); + return implode('; ', $parameters); } } diff --git a/src/HeaderContentSecurityPolicyReportOnly.php b/src/HeaderContentSecurityPolicyReportOnly.php index af20fb6..2174d8c 100644 --- a/src/HeaderContentSecurityPolicyReportOnly.php +++ b/src/HeaderContentSecurityPolicyReportOnly.php @@ -20,7 +20,7 @@ class HeaderContentSecurityPolicyReportOnly extends HeaderContentSecurityPolicy { /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { diff --git a/src/HeaderContentType.php b/src/HeaderContentType.php index 38d7670..c70f6c7 100644 --- a/src/HeaderContentType.php +++ b/src/HeaderContentType.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderContentType * @@ -38,7 +49,7 @@ class HeaderContentType extends AbstractHeader implements HeaderInterface /** * The content media-type parameters * - * @var array + * @var array */ protected $parameters = []; @@ -46,7 +57,7 @@ class HeaderContentType extends AbstractHeader implements HeaderInterface * Constructor of the class * * @param string $type - * @param array $parameters + * @param array $parameters */ public function __construct(string $type, array $parameters = []) { @@ -61,14 +72,16 @@ public function __construct(string $type, array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setType(string $type) : self { - if (! \preg_match(self::VALID_MEDIA_TYPE, $type)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $type) - ); + if (!preg_match(self::VALID_MEDIA_TYPE, $type)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $type + )); } $this->type = $type; @@ -84,20 +97,24 @@ public function setType(string $type) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -108,7 +125,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the content media-type parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -134,7 +151,7 @@ public function getType() : string /** * Gets the content media-type parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -154,7 +171,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -162,13 +179,13 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $r = $this->getType(); foreach ($this->getParameters() as $name => $value) { - $r .= \sprintf('; %s="%s"', $name, $value); + $r .= sprintf('; %s="%s"', $name, $value); } return $r; diff --git a/src/HeaderCookie.php b/src/HeaderCookie.php index 9c4a83b..b4ee4e7 100644 --- a/src/HeaderCookie.php +++ b/src/HeaderCookie.php @@ -11,6 +11,16 @@ namespace Sunrise\Http\Header; +/** + * Import functions + */ +use function http_build_query; + +/** + * Import constants + */ +use const PHP_QUERY_RFC3986; + /** * HeaderCookie * @@ -61,7 +71,7 @@ public function getValue() : array } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -69,10 +79,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \http_build_query($this->getValue(), '', '; ', \PHP_QUERY_RFC3986); + return http_build_query($this->getValue(), '', '; ', PHP_QUERY_RFC3986); } } diff --git a/src/HeaderDate.php b/src/HeaderDate.php index 7d8ead2..9425e21 100644 --- a/src/HeaderDate.php +++ b/src/HeaderDate.php @@ -11,6 +11,12 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; + /** * HeaderDate * @@ -23,16 +29,16 @@ class HeaderDate extends AbstractHeader implements HeaderInterface /** * Timestamp for the header field-value * - * @var \DateTimeInterface + * @var DateTimeInterface */ protected $timestamp; /** * Constructor of the class * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp */ - public function __construct(\DateTimeInterface $timestamp) + public function __construct(DateTimeInterface $timestamp) { $this->setTimestamp($timestamp); } @@ -40,11 +46,11 @@ public function __construct(\DateTimeInterface $timestamp) /** * Sets timestamp for the header field-value * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp * * @return self */ - public function setTimestamp(\DateTimeInterface $timestamp) : self + public function setTimestamp(DateTimeInterface $timestamp) : self { $this->timestamp = $timestamp; @@ -54,15 +60,15 @@ public function setTimestamp(\DateTimeInterface $timestamp) : self /** * Gets timestamp for the header field-value * - * @return \DateTimeInterface + * @return DateTimeInterface */ - public function getTimestamp() : \DateTimeInterface + public function getTimestamp() : DateTimeInterface { return $this->timestamp; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -70,12 +76,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $this->getTimestamp()->setTimezone(new \DateTimeZone('GMT')); + $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(\DateTime::RFC822); + return $this->getTimestamp()->format(DateTimeInterface::RFC822); } } diff --git a/src/HeaderEtag.php b/src/HeaderEtag.php index 1fd6767..cf107d7 100644 --- a/src/HeaderEtag.php +++ b/src/HeaderEtag.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderEtag * @@ -43,14 +54,16 @@ public function __construct(string $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $value) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +82,7 @@ public function getValue() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,10 +90,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('"%s"', $this->getValue()); + return sprintf('"%s"', $this->getValue()); } } diff --git a/src/HeaderExpires.php b/src/HeaderExpires.php index 79b5583..d2ea456 100644 --- a/src/HeaderExpires.php +++ b/src/HeaderExpires.php @@ -11,6 +11,12 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; + /** * HeaderExpires * @@ -23,16 +29,16 @@ class HeaderExpires extends AbstractHeader implements HeaderInterface /** * Timestamp for the header field-value * - * @var \DateTimeInterface + * @var DateTimeInterface */ protected $timestamp; /** * Constructor of the class * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp */ - public function __construct(\DateTimeInterface $timestamp) + public function __construct(DateTimeInterface $timestamp) { $this->setTimestamp($timestamp); } @@ -40,11 +46,11 @@ public function __construct(\DateTimeInterface $timestamp) /** * Sets timestamp for the header field-value * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp * * @return self */ - public function setTimestamp(\DateTimeInterface $timestamp) : self + public function setTimestamp(DateTimeInterface $timestamp) : self { $this->timestamp = $timestamp; @@ -54,15 +60,15 @@ public function setTimestamp(\DateTimeInterface $timestamp) : self /** * Gets timestamp for the header field-value * - * @return \DateTimeInterface + * @return DateTimeInterface */ - public function getTimestamp() : \DateTimeInterface + public function getTimestamp() : DateTimeInterface { return $this->timestamp; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -70,12 +76,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $this->getTimestamp()->setTimezone(new \DateTimeZone('GMT')); + $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(\DateTime::RFC822); + return $this->getTimestamp()->format(DateTimeInterface::RFC822); } } diff --git a/src/HeaderKeepAlive.php b/src/HeaderKeepAlive.php index 68cef21..1e6749f 100644 --- a/src/HeaderKeepAlive.php +++ b/src/HeaderKeepAlive.php @@ -11,6 +11,18 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderKeepAlive * @@ -22,14 +34,14 @@ class HeaderKeepAlive extends AbstractHeader implements HeaderInterface /** * The header parameters * - * @var array + * @var array */ protected $parameters = []; /** * Constructor of the class * - * @param array $parameters + * @param array $parameters */ public function __construct(array $parameters = []) { @@ -44,20 +56,24 @@ public function __construct(array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -68,7 +84,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the header parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -84,7 +100,7 @@ public function setParameters(array $parameters) : self /** * Gets the header parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -104,7 +120,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -112,26 +128,26 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { $parameters = []; foreach ($this->getParameters() as $name => $value) { - $parameter = $name; - - if (! (\strlen($value) === 0)) { - // Example: timeout=5, max=1000 - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { - $value = '"' . $value . '"'; - } + if ('' === $value) { + $parameters[] = $name; + continue; + } - $parameter .= '=' . $value; + // e.g.: timeout=5, max=1000 + if (preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { + $parameters[] = $name . '=' . $value; + continue; } - $parameters[] = $parameter; + $parameters[] = $name . '="' . $value . '"'; } - return \implode(', ', $parameters); + return implode(', ', $parameters); } } diff --git a/src/HeaderLastModified.php b/src/HeaderLastModified.php index 0de88af..103a525 100644 --- a/src/HeaderLastModified.php +++ b/src/HeaderLastModified.php @@ -11,6 +11,12 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; + /** * HeaderLastModified * @@ -23,16 +29,16 @@ class HeaderLastModified extends AbstractHeader implements HeaderInterface /** * Timestamp for the header field-value * - * @var \DateTimeInterface + * @var DateTimeInterface */ protected $timestamp; /** * Constructor of the class * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp */ - public function __construct(\DateTimeInterface $timestamp) + public function __construct(DateTimeInterface $timestamp) { $this->setTimestamp($timestamp); } @@ -40,11 +46,11 @@ public function __construct(\DateTimeInterface $timestamp) /** * Sets timestamp for the header field-value * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp * * @return self */ - public function setTimestamp(\DateTimeInterface $timestamp) : self + public function setTimestamp(DateTimeInterface $timestamp) : self { $this->timestamp = $timestamp; @@ -54,15 +60,15 @@ public function setTimestamp(\DateTimeInterface $timestamp) : self /** * Gets timestamp for the header field-value * - * @return \DateTimeInterface + * @return DateTimeInterface */ - public function getTimestamp() : \DateTimeInterface + public function getTimestamp() : DateTimeInterface { return $this->timestamp; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -70,12 +76,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $this->getTimestamp()->setTimezone(new \DateTimeZone('GMT')); + $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(\DateTime::RFC822); + return $this->getTimestamp()->format(DateTimeInterface::RFC822); } } diff --git a/src/HeaderLink.php b/src/HeaderLink.php index c12ca0e..c181534 100644 --- a/src/HeaderLink.php +++ b/src/HeaderLink.php @@ -14,8 +14,15 @@ /** * Import classes */ +use InvalidArgumentException; use Psr\Http\Message\UriInterface; +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderLink * @@ -34,7 +41,7 @@ class HeaderLink extends AbstractHeader implements HeaderInterface /** * The link parameters * - * @var array + * @var array */ protected $parameters = []; @@ -42,7 +49,7 @@ class HeaderLink extends AbstractHeader implements HeaderInterface * Constructor of the class * * @param UriInterface $uri - * @param array $parameters + * @param array $parameters */ public function __construct(UriInterface $uri, array $parameters = []) { @@ -72,20 +79,24 @@ public function setUri(UriInterface $uri) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -96,7 +107,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the link parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -122,7 +133,7 @@ public function getUri() : UriInterface /** * Gets the link parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -142,7 +153,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -150,13 +161,13 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $r = \sprintf('<%s>', (string) $this->getUri()); + $r = sprintf('<%s>', (string) $this->getUri()); foreach ($this->getParameters() as $name => $value) { - $r .= \sprintf('; %s="%s"', $name, $value); + $r .= sprintf('; %s="%s"', $name, $value); } return $r; diff --git a/src/HeaderLocation.php b/src/HeaderLocation.php index 52e9308..46e883d 100644 --- a/src/HeaderLocation.php +++ b/src/HeaderLocation.php @@ -66,7 +66,7 @@ public function getUri() : UriInterface } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -74,7 +74,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderRefresh.php b/src/HeaderRefresh.php index f5eff8a..9fccd21 100644 --- a/src/HeaderRefresh.php +++ b/src/HeaderRefresh.php @@ -14,8 +14,14 @@ /** * Import classes */ +use InvalidArgumentException; use Psr\Http\Message\UriInterface; +/** + * Import functions + */ +use function sprintf; + /** * HeaderRefresh * @@ -57,14 +63,15 @@ public function __construct(int $delay, UriInterface $uri) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setDelay(int $delay) : self { if (! ($delay >= 0)) { - throw new \InvalidArgumentException( - \sprintf('The given delay "%d" for the "Refresh" header is not valid', $delay) - ); + throw new InvalidArgumentException(sprintf( + 'The given delay "%d" for the "Refresh" header is not valid', + $delay + )); } $this->delay = $delay; @@ -107,7 +114,7 @@ public function getUri() : UriInterface } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -115,10 +122,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \sprintf('%d; url=%s', $this->getDelay(), (string) $this->getUri()); + return sprintf('%d; url=%s', $this->getDelay(), (string) $this->getUri()); } } diff --git a/src/HeaderRetryAfter.php b/src/HeaderRetryAfter.php index fb66600..dae121d 100644 --- a/src/HeaderRetryAfter.php +++ b/src/HeaderRetryAfter.php @@ -11,6 +11,12 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; + /** * HeaderRetryAfter * @@ -23,16 +29,16 @@ class HeaderRetryAfter extends AbstractHeader implements HeaderInterface /** * Timestamp for the header field-value * - * @var \DateTimeInterface + * @var DateTimeInterface */ protected $timestamp; /** * Constructor of the class * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp */ - public function __construct(\DateTimeInterface $timestamp) + public function __construct(DateTimeInterface $timestamp) { $this->setTimestamp($timestamp); } @@ -40,11 +46,11 @@ public function __construct(\DateTimeInterface $timestamp) /** * Sets timestamp for the header field-value * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp * * @return self */ - public function setTimestamp(\DateTimeInterface $timestamp) : self + public function setTimestamp(DateTimeInterface $timestamp) : self { $this->timestamp = $timestamp; @@ -54,15 +60,15 @@ public function setTimestamp(\DateTimeInterface $timestamp) : self /** * Gets timestamp for the header field-value * - * @return \DateTimeInterface + * @return DateTimeInterface */ - public function getTimestamp() : \DateTimeInterface + public function getTimestamp() : DateTimeInterface { return $this->timestamp; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -70,12 +76,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $this->getTimestamp()->setTimezone(new \DateTimeZone('GMT')); + $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(\DateTime::RFC822); + return $this->getTimestamp()->format(DateTimeInterface::RFC822); } } diff --git a/src/HeaderSetCookie.php b/src/HeaderSetCookie.php index eba6e05..cd49d46 100644 --- a/src/HeaderSetCookie.php +++ b/src/HeaderSetCookie.php @@ -11,6 +11,22 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_key_exists; +use function rawurlencode; +use function sprintf; +use function strpbrk; +use function time; + /** * HeaderSetCookie * @@ -37,42 +53,42 @@ class HeaderSetCookie extends AbstractHeader implements HeaderInterface /** * The cookie attribute "Expires" * - * @var null|\DateTimeInterface + * @var DateTimeInterface|null */ protected $expires; /** * The cookie attribute "Domain" * - * @var null|string + * @var string|null */ protected $domain; /** * The cookie attribute "Path" * - * @var null|string + * @var string|null */ protected $path; /** * The cookie attribute "Secure" * - * @var null|bool + * @var bool|null */ protected $secure; /** * The cookie attribute "HttpOnly" * - * @var null|bool + * @var bool|null */ protected $httponly; /** * The cookie attribute "SameSite" * - * @var null|string + * @var string|null */ protected $samesite; @@ -81,32 +97,32 @@ class HeaderSetCookie extends AbstractHeader implements HeaderInterface * * @param string $name * @param string $value - * @param null|\DateTimeInterface $expires + * @param DateTimeInterface|null $expires * @param array $options */ - public function __construct(string $name, string $value, \DateTimeInterface $expires = null, array $options = []) + public function __construct(string $name, string $value, DateTimeInterface $expires = null, array $options = []) { $this->setName($name); $this->setValue($value); $this->setExpires($expires); - if (\array_key_exists('domain', $options)) { + if (array_key_exists('domain', $options)) { $this->setDomain($options['domain']); } - if (\array_key_exists('path', $options)) { + if (array_key_exists('path', $options)) { $this->setPath($options['path']); } - if (\array_key_exists('secure', $options)) { + if (array_key_exists('secure', $options)) { $this->setSecure($options['secure']); } - if (\array_key_exists('httponly', $options)) { + if (array_key_exists('httponly', $options)) { $this->setHttpOnly($options['httponly']); } - if (\array_key_exists('samesite', $options)) { + if (array_key_exists('samesite', $options)) { $this->setSameSite($options['samesite']); } } @@ -118,18 +134,16 @@ public function __construct(string $name, string $value, \DateTimeInterface $exp * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setName(string $name) : self { - if (! (\strlen($name) > 0)) { - throw new \InvalidArgumentException( - 'Cookie names must not be empty' - ); + if ('' === $name) { + throw new InvalidArgumentException('Cookie names must not be empty'); } - if (! (\strpbrk($name, "=,; \t\r\n\013\014") === false)) { - throw new \InvalidArgumentException( + if (strpbrk($name, "=,; \t\r\n\013\014") !== false) { + throw new InvalidArgumentException( 'Cookie names cannot contain any of the following "=,; \\t\\r\\n\\013\\014"' ); } @@ -156,11 +170,11 @@ public function setValue(string $value) : self /** * Sets the cookie attribute "Expires" * - * @param null|\DateTimeInterface $expires + * @param DateTimeInterface|null $expires * * @return self */ - public function setExpires(?\DateTimeInterface $expires) : self + public function setExpires(?DateTimeInterface $expires) : self { $this->expires = $expires; @@ -170,16 +184,16 @@ public function setExpires(?\DateTimeInterface $expires) : self /** * Sets the cookie attribute "Domain" * - * @param null|string $domain + * @param string|null $domain * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setDomain(?string $domain) : self { - if (! (\is_null($domain) || \strpbrk($domain, ",; \t\r\n\013\014") === false)) { - throw new \InvalidArgumentException( + if (isset($domain) && strpbrk($domain, ",; \t\r\n\013\014") !== false) { + throw new InvalidArgumentException( 'Cookie domains cannot contain any of the following ",; \\t\\r\\n\\013\\014"' ); } @@ -192,16 +206,16 @@ public function setDomain(?string $domain) : self /** * Sets the cookie attribute "Path" * - * @param null|string $path + * @param string|null $path * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setPath(?string $path) : self { - if (! (\is_null($path) || \strpbrk($path, ",; \t\r\n\013\014") === false)) { - throw new \InvalidArgumentException( + if (isset($path) && strpbrk($path, ",; \t\r\n\013\014") !== false) { + throw new InvalidArgumentException( 'Cookie paths cannot contain any of the following ",; \\t\\r\\n\\013\\014"' ); } @@ -214,7 +228,7 @@ public function setPath(?string $path) : self /** * Sets the cookie attribute "Secure" * - * @param null|bool $secure + * @param bool|null $secure * * @return self */ @@ -228,7 +242,7 @@ public function setSecure(?bool $secure) : self /** * Sets the cookie attribute "HttpOnly" * - * @param null|bool $httponly + * @param bool|null $httponly * * @return self */ @@ -242,16 +256,16 @@ public function setHttpOnly(?bool $httponly) : self /** * Sets the cookie attribute "SameSite" * - * @param null|string $samesite + * @param string|null $samesite * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setSameSite(?string $samesite) : self { - if (! (\is_null($samesite) || \strpbrk($samesite, ",; \t\r\n\013\014") === false)) { - throw new \InvalidArgumentException( + if (isset($samesite) && strpbrk($samesite, ",; \t\r\n\013\014") !== false) { + throw new InvalidArgumentException( 'Cookie samesites cannot contain any of the following ",; \\t\\r\\n\\013\\014"' ); } @@ -284,9 +298,9 @@ public function getValue() : string /** * Gets the cookie attribute "Expires" * - * @return null|\DateTimeInterface + * @return DateTimeInterface|null */ - public function getExpires() : ?\DateTimeInterface + public function getExpires() : ?DateTimeInterface { return $this->expires; } @@ -294,7 +308,7 @@ public function getExpires() : ?\DateTimeInterface /** * Gets the cookie attribute "Domain" * - * @return null|string + * @return string|null */ public function getDomain() : ?string { @@ -304,7 +318,7 @@ public function getDomain() : ?string /** * Gets the cookie attribute "Path" * - * @return null|string + * @return string|null */ public function getPath() : ?string { @@ -314,7 +328,7 @@ public function getPath() : ?string /** * Gets the cookie attribute "Secure" * - * @return null|bool + * @return bool|null */ public function getSecure() : ?bool { @@ -324,7 +338,7 @@ public function getSecure() : ?bool /** * Gets the cookie attribute "HttpOnly" * - * @return null|bool + * @return bool|null */ public function getHttpOnly() : ?bool { @@ -334,7 +348,7 @@ public function getHttpOnly() : ?bool /** * Gets the cookie attribute "SameSite" * - * @return null|string + * @return string|null */ public function getSameSite() : ?string { @@ -342,7 +356,7 @@ public function getSameSite() : ?string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -350,19 +364,19 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $name = \rawurlencode($this->getName()); - $value = \rawurlencode($this->getValue()); - $result = \sprintf('%s=%s', $name, $value); + $name = rawurlencode($this->getName()); + $value = rawurlencode($this->getValue()); + $result = sprintf('%s=%s', $name, $value); - if ($this->getExpires() instanceof \DateTimeInterface) { - $this->getExpires()->setTimezone(new \DateTimeZone('GMT')); + if ($this->getExpires() instanceof DateTimeInterface) { + $this->getExpires()->setTimezone(new DateTimeZone('GMT')); - $result .= '; Expires=' . $this->getExpires()->format(\DateTime::RFC822); - $result .= '; Max-Age=' . ($this->getExpires()->getTimestamp() - \time()); + $result .= '; Expires=' . $this->getExpires()->format(DateTimeInterface::RFC822); + $result .= '; Max-Age=' . ($this->getExpires()->getTimestamp() - time()); } if ($this->getDomain()) { diff --git a/src/HeaderSunset.php b/src/HeaderSunset.php index 3a30300..585200b 100644 --- a/src/HeaderSunset.php +++ b/src/HeaderSunset.php @@ -11,6 +11,12 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; + /** * HeaderSunset * @@ -23,16 +29,16 @@ class HeaderSunset extends AbstractHeader implements HeaderInterface /** * Timestamp for the header field-value * - * @var \DateTimeInterface + * @var DateTimeInterface */ protected $timestamp; /** * Constructor of the class * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp */ - public function __construct(\DateTimeInterface $timestamp) + public function __construct(DateTimeInterface $timestamp) { $this->setTimestamp($timestamp); } @@ -40,11 +46,11 @@ public function __construct(\DateTimeInterface $timestamp) /** * Sets timestamp for the header field-value * - * @param \DateTimeInterface $timestamp + * @param DateTimeInterface $timestamp * * @return self */ - public function setTimestamp(\DateTimeInterface $timestamp) : self + public function setTimestamp(DateTimeInterface $timestamp) : self { $this->timestamp = $timestamp; @@ -54,15 +60,15 @@ public function setTimestamp(\DateTimeInterface $timestamp) : self /** * Gets timestamp for the header field-value * - * @return \DateTimeInterface + * @return DateTimeInterface */ - public function getTimestamp() : \DateTimeInterface + public function getTimestamp() : DateTimeInterface { return $this->timestamp; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -70,12 +76,12 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $this->getTimestamp()->setTimezone(new \DateTimeZone('GMT')); + $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(\DateTime::RFC822); + return $this->getTimestamp()->format(DateTimeInterface::RFC822); } } diff --git a/src/HeaderTrailer.php b/src/HeaderTrailer.php index 4bf2b16..f9a322d 100644 --- a/src/HeaderTrailer.php +++ b/src/HeaderTrailer.php @@ -11,6 +11,17 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderTrailer * @@ -43,14 +54,16 @@ public function __construct(string $value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $value) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $value)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $value + )); } $this->value = $value; @@ -69,7 +82,7 @@ public function getValue() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -77,7 +90,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { diff --git a/src/HeaderTransferEncoding.php b/src/HeaderTransferEncoding.php index d03cb45..cbffc78 100644 --- a/src/HeaderTransferEncoding.php +++ b/src/HeaderTransferEncoding.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderTransferEncoding * @@ -22,7 +35,7 @@ class HeaderTransferEncoding extends AbstractHeader implements HeaderInterface /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,15 +56,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -63,11 +78,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -83,7 +98,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -91,10 +106,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderVary.php b/src/HeaderVary.php index 41189f1..ed2ccf9 100644 --- a/src/HeaderVary.php +++ b/src/HeaderVary.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function array_keys; +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderVary * @@ -22,7 +35,7 @@ class HeaderVary extends AbstractHeader implements HeaderInterface /** * The header value * - * @var array + * @var array */ protected $value = []; @@ -43,15 +56,17 @@ public function __construct(string ...$value) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setValue(string ...$value) : self { foreach ($value as $oneOf) { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { - throw new \InvalidArgumentException( - \sprintf('The value "%s" for the header "%s" is not valid', $oneOf, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $oneOf)) { + throw new InvalidArgumentException(sprintf( + 'The value "%s" for the header "%s" is not valid', + $oneOf, + $this->getFieldName() + )); } $this->value[$oneOf] = true; @@ -63,11 +78,11 @@ public function setValue(string ...$value) : self /** * Gets the header value * - * @return array + * @return array */ public function getValue() : array { - return \array_keys($this->value); + return array_keys($this->value); } /** @@ -83,7 +98,7 @@ public function resetValue() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -91,10 +106,10 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - return \implode(', ', $this->getValue()); + return implode(', ', $this->getValue()); } } diff --git a/src/HeaderWWWAuthenticate.php b/src/HeaderWWWAuthenticate.php index c22f87a..95d6fc4 100644 --- a/src/HeaderWWWAuthenticate.php +++ b/src/HeaderWWWAuthenticate.php @@ -11,6 +11,18 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use InvalidArgumentException; + +/** + * Import functions + */ +use function implode; +use function preg_match; +use function sprintf; + /** * HeaderWWWAuthenticate * @@ -45,7 +57,7 @@ class HeaderWWWAuthenticate extends AbstractHeader implements HeaderInterface /** * The authentication parameters * - * @var array + * @var array */ protected $parameters = []; @@ -53,7 +65,7 @@ class HeaderWWWAuthenticate extends AbstractHeader implements HeaderInterface * Constructor of the class * * @param string $scheme - * @param array $parameters + * @param array $parameters */ public function __construct(string $scheme, array $parameters = []) { @@ -68,14 +80,16 @@ public function __construct(string $scheme, array $parameters = []) * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setScheme(string $scheme) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $scheme)) { - throw new \InvalidArgumentException( - \sprintf('The header field "%s: %s" is not valid', $this->getFieldName(), $scheme) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $scheme)) { + throw new InvalidArgumentException(sprintf( + 'The header field "%s: %s" is not valid', + $this->getFieldName(), + $scheme + )); } $this->scheme = $scheme; @@ -91,20 +105,24 @@ public function setScheme(string $scheme) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setParameter(string $name, string $value) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-name "%s" for the header "%s" is not valid', $name, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $name)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-name "%s" for the header "%s" is not valid', + $name, + $this->getFieldName() + )); } - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { - throw new \InvalidArgumentException( - \sprintf('The parameter-value "%s" for the header "%s" is not valid', $value, $this->getFieldName()) - ); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $value)) { + throw new InvalidArgumentException(sprintf( + 'The parameter-value "%s" for the header "%s" is not valid', + $value, + $this->getFieldName() + )); } $this->parameters[$name] = $value; @@ -115,7 +133,7 @@ public function setParameter(string $name, string $value) : self /** * Sets the authentication parameters * - * @param array $parameters + * @param array $parameters * * @return self */ @@ -141,7 +159,7 @@ public function getScheme() : string /** * Gets the authentication parameters * - * @return array + * @return array */ public function getParameters() : array { @@ -161,7 +179,7 @@ public function clearParameters() : self } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -169,7 +187,7 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { @@ -177,11 +195,11 @@ public function getFieldValue() : string $challenge = []; foreach ($this->getParameters() as $name => $value) { - $challenge[] = \sprintf(' %s="%s"', $name, $value); + $challenge[] = sprintf(' %s="%s"', $name, $value); } - if (! empty($challenge)) { - $r .= \implode(',', $challenge); + if (!empty($challenge)) { + $r .= implode(',', $challenge); } return $r; diff --git a/src/HeaderWarning.php b/src/HeaderWarning.php index 20bba91..08711a7 100644 --- a/src/HeaderWarning.php +++ b/src/HeaderWarning.php @@ -11,6 +11,19 @@ namespace Sunrise\Http\Header; +/** + * Import classes + */ +use DateTimeInterface; +use DateTimeZone; +use InvalidArgumentException; + +/** + * Import functions + */ +use function preg_match; +use function sprintf; + /** * HeaderWarning * @@ -56,7 +69,7 @@ class HeaderWarning extends AbstractHeader implements HeaderInterface /** * The warning date * - * @var null|\DateTimeInterface + * @var DateTimeInterface|null */ protected $date; @@ -66,9 +79,9 @@ class HeaderWarning extends AbstractHeader implements HeaderInterface * @param int $code * @param string $agent * @param string $text - * @param null|\DateTimeInterface $date + * @param DateTimeInterface|null $date */ - public function __construct(int $code, string $agent, string $text, \DateTimeInterface $date = null) + public function __construct(int $code, string $agent, string $text, DateTimeInterface $date = null) { $this->setCode($code); $this->setAgent($agent); @@ -83,12 +96,12 @@ public function __construct(int $code, string $agent, string $text, \DateTimeInt * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setCode(int $code) : self { if (! ($code >= 100 && $code <= 999)) { - throw new \InvalidArgumentException('The warning code is not valid'); + throw new InvalidArgumentException('The warning code is not valid'); } $this->code = $code; @@ -103,12 +116,12 @@ public function setCode(int $code) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setAgent(string $agent) : self { - if (! \preg_match(HeaderInterface::RFC7230_TOKEN, $agent)) { - throw new \InvalidArgumentException('The warning agent is not valid'); + if (!preg_match(HeaderInterface::RFC7230_TOKEN, $agent)) { + throw new InvalidArgumentException('The warning agent is not valid'); } $this->agent = $agent; @@ -123,12 +136,12 @@ public function setAgent(string $agent) : self * * @return self * - * @throws \InvalidArgumentException + * @throws InvalidArgumentException */ public function setText(string $text) : self { - if (! \preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $text)) { - throw new \InvalidArgumentException('The warning text is not valid'); + if (!preg_match(HeaderInterface::RFC7230_QUOTED_STRING, $text)) { + throw new InvalidArgumentException('The warning text is not valid'); } $this->text = $text; @@ -139,11 +152,11 @@ public function setText(string $text) : self /** * Sets the warning date * - * @param null|\DateTimeInterface $date + * @param DateTimeInterface|null $date * * @return self */ - public function setDate(?\DateTimeInterface $date) : self + public function setDate(?DateTimeInterface $date) : self { $this->date = $date; @@ -183,15 +196,15 @@ public function getText() : string /** * Gets the warning date * - * @return null|\DateTimeInterface + * @return DateTimeInterface|null */ - public function getDate() : ?\DateTimeInterface + public function getDate() : ?DateTimeInterface { return $this->date; } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldName() : string { @@ -199,20 +212,21 @@ public function getFieldName() : string } /** - * {@inheritDoc} + * {@inheritdoc} */ public function getFieldValue() : string { - $result = \sprintf('%s %s "%s"', + $result = sprintf( + '%s %s "%s"', $this->getCode(), $this->getAgent(), $this->getText() ); - if ($this->getDate() instanceof \DateTimeInterface) { - $this->getDate()->setTimezone(new \DateTimeZone('GMT')); + if ($this->getDate() instanceof DateTimeInterface) { + $this->getDate()->setTimezone(new DateTimeZone('GMT')); - $result .= ' "' . $this->getDate()->format(\DateTime::RFC822) . '"'; + $result .= ' "' . $this->getDate()->format(DateTimeInterface::RFC822) . '"'; } return $result; From 50a4614994ed3980d9cb3942125d65b6c965a596 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:16:21 +0500 Subject: [PATCH 03/12] integration with circleci --- .circleci/config.yml | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..0441507 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,55 @@ +# PHP CircleCI 2.0 configuration file +# +# Check https://circleci.com/docs/2.0/language-php/ for more details +# +version: 2 +jobs: + php71: + docker: + - image: circleci/php:7.1-cli-node-browsers + steps: + - checkout + - run: php -v + - run: composer install --no-interaction --no-suggest --prefer-source + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text + php72: + docker: + - image: circleci/php:7.2-cli-node-browsers + steps: + - checkout + - run: php -v + - run: composer install --no-interaction --no-suggest --prefer-source + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text + php73: + docker: + - image: circleci/php:7.3-cli-node-browsers + steps: + - checkout + - run: php -v + - run: composer install --no-interaction --no-suggest --prefer-source + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text + php74: + docker: + - image: circleci/php:7.4-cli-node-browsers + steps: + - checkout + - run: php -v + - run: composer install --no-interaction --no-suggest --prefer-source + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text + php80: + docker: + - image: circleci/php:8.0-cli-node-browsers + steps: + - checkout + - run: php -v + - run: composer install --no-interaction --no-suggest --prefer-source + - run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text +workflows: + version: 2 + build: + jobs: + - php71 + - php72 + - php73 + - php74 + - php80 From 7209fdada66d96a094689e54e409c1d84481cb0e Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:16:32 +0500 Subject: [PATCH 04/12] update editorconfig --- .editorconfig | 3 --- 1 file changed, 3 deletions(-) diff --git a/.editorconfig b/.editorconfig index 27e2667..91aebb8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,8 +11,5 @@ indent_size = 4 trim_trailing_whitespace = true insert_final_newline = true -[*.md] -trim_trailing_whitespace = false - [*.yml] indent_size = 2 From 88318e91feb0d7fe3b842446726f97a02759607d Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:16:45 +0500 Subject: [PATCH 05/12] update gitignore --- .gitignore | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 41cf726..ac89ebd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ -.php_cs.cache -.phpunit.result.cache -composer.lock -coverage.xml -phpcs.xml -phpunit.xml -vendor/ +/.php_cs.cache +/.phpunit.result.cache +/composer.lock +/coverage.xml +/phpbench.json +/phpcs.xml +/phpunit.xml +/vendor/ From adf51992a64b85bb2e463825aef00df3a96458bb Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:17:00 +0500 Subject: [PATCH 06/12] update scrutinizer.yml --- .scrutinizer.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d95425d..1e6a96f 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,4 +1,7 @@ build: + environment: + php: + version: '8.0' nodes: analysis: tests: @@ -7,7 +10,7 @@ build: coverage: tests: override: - - command: php vendor/bin/phpunit --coverage-clover coverage.xml + - command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml coverage: file: coverage.xml format: clover From 343c24593f504669b9c8635899a4c5b178d070df Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:17:16 +0500 Subject: [PATCH 07/12] delete --- .travis.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b0e5e8d..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: php - -matrix: - include: - - php: 7.1 - - php: 7.2 - - php: 7.3 - - php: 7.4 - - php: nightly - fast_finish: true - -before_install: - - travis_retry composer self-update - -install: - - travis_retry composer install --no-interaction --prefer-source --no-suggest - -script: php vendor/bin/phpunit --colors=always --coverage-text From 96d10006c58220f206a79f33efa8c8fb597559c9 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:17:24 +0500 Subject: [PATCH 08/12] update README.md --- README.md | 161 +++++++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index d25e0f1..0da813b 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,44 @@ -## HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7 +## HTTP header kit for PHP 7.1+ compatible with PSR-7 -[![Gitter](https://badges.gitter.im/sunrise-php/support.png)](https://gitter.im/sunrise-php/support) -[![Build Status](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/badges/build.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/build-status/master) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/?branch=master) +[![Build Status](https://circleci.com/gh/sunrise-php/http-header-kit.svg?style=shield)](https://circleci.com/gh/sunrise-php/http-header-kit) [![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/?branch=master) -[![Latest Stable Version](https://poser.pugx.org/sunrise/http-header-kit/v/stable)](https://packagist.org/packages/sunrise/http-header-kit) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/?branch=master) [![Total Downloads](https://poser.pugx.org/sunrise/http-header-kit/downloads)](https://packagist.org/packages/sunrise/http-header-kit) +[![Latest Stable Version](https://poser.pugx.org/sunrise/http-header-kit/v/stable)](https://packagist.org/packages/sunrise/http-header-kit) [![License](https://poser.pugx.org/sunrise/http-header-kit/license)](https://packagist.org/packages/sunrise/http-header-kit) +--- + ## Installation ```bash -composer require sunrise/http-header-kit +composer require 'sunrise/http-header-kit:^2.0' ``` ## How to use? -### HTTP Header Collection - -> More useful information: -> -> https://github.com/sunrise-php/http-header-collection +#### PSR-7 -```bash -composer require sunrise/http-header-collection +```php +$message->withHeader(...$header); ``` -```php -// Creates the header collection -$headers = new \Sunrise\Http\Header\HeaderCollection([ - new \Sunrise\Http\Header\HeaderAllow('OPTIONS', 'HEAD', 'GET'), - new \Sunrise\Http\Header\HeaderContentLanguage('de-DE'), - new \Sunrise\Http\Header\HeaderContentType('application/json'), -]); +#### Symfony -// Sets headers to the message -$message = $headers->setToMessage($message); +```php +$response->headers->set(...$header); +``` -// ... or adds headers to the message -$message = $headers->addToMessage($message); +#### Laravel -// ...or converts headers to an array -$headers->toArray(); +```php +$response->header(...$header); ``` ### HTTP Headers +Note that in the examples below will use PSR-7. + #### Access-Control-Allow-Credentials > Usage link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials @@ -57,7 +50,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAccessControlAllowCredentials(); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Access-Control-Allow-Headers @@ -71,7 +64,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAccessControlAllowHeaders('X-Custom-Header', 'Upgrade-Insecure-Requests'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Access-Control-Allow-Methods @@ -85,7 +78,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAccessControlAllowMethods('OPTIONS', 'HEAD', 'GET'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Access-Control-Allow-Origin @@ -102,13 +95,13 @@ $message = (new ResponseFactory)->createResponse(); // A response that tells the browser to allow code from any origin to access // a resource will include the following: $header = new HeaderAccessControlAllowOrigin(null); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // A response that tells the browser to allow requesting code from the origin // https://developer.mozilla.org to access a resource will include the following: $uri = (new UriFactory)->createUri('https://developer.mozilla.org'); $header = new HeaderAccessControlAllowOrigin($uri); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Access-Control-Expose-Headers @@ -122,7 +115,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAccessControlExposeHeaders('Content-Length', 'X-Kuma-Revision'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Access-Control-Max-Age @@ -136,7 +129,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAccessControlMaxAge(600); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Age @@ -150,7 +143,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAge(24); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Allow @@ -164,7 +157,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderAllow('OPTIONS', 'HEAD', 'GET'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Cache-Control @@ -179,11 +172,11 @@ $message = (new ResponseFactory)->createResponse(); // Preventing caching $header = new HeaderCacheControl(['no-cache' => '', 'no-store' => '', 'must-revalidate' => '']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // Caching static assets $header = new HeaderCacheControl(['public' => '', 'max-age' => '31536000']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Clear-Site-Data @@ -198,15 +191,15 @@ $message = (new ResponseFactory)->createResponse(); // Single directive $header = new HeaderClearSiteData(['cache']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // Multiple directives (comma separated) $header = new HeaderClearSiteData(['cache', 'cookies']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // Wild card $header = new HeaderClearSiteData(['*']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Connection @@ -221,11 +214,11 @@ $message = (new ResponseFactory)->createResponse(); // close $header = new HeaderConnection(HeaderConnection::CONNECTION_CLOSE); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // keep-alive $header = new HeaderConnection(HeaderConnection::CONNECTION_KEEP_ALIVE); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Disposition @@ -240,11 +233,11 @@ $message = (new ResponseFactory)->createResponse(); // As a response header for the main body $header = new HeaderContentDisposition('attachment', ['filename' => 'filename.jpg']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); // As a header for a multipart body $header = new HeaderContentDisposition('form-data', ['name' => 'fieldName', 'filename' => 'filename.jpg']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Encoding @@ -258,7 +251,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderContentEncoding('gzip'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Language @@ -272,7 +265,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderContentLanguage('de-DE', 'en-CA'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Length @@ -286,7 +279,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderContentLength(4096); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Location @@ -302,7 +295,7 @@ $message = (new ResponseFactory)->createResponse(); $uri = (new UriFactory)->createUri('https://example.com/documents/foo'); $header = new HeaderContentLocation($uri); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-MD5 @@ -316,7 +309,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderContentMD5('MzAyMWU2OGRmOWE3MjAwMTM1NzI1YzYzMzEzNjlhMjI='); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Range @@ -334,7 +327,7 @@ $header = new HeaderContentRange( 1000, // An integer in the given unit indicating the end of the requested range. 67589 // The total size of the document. ); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Content-Security-Policy @@ -350,12 +343,12 @@ $message = (new ResponseFactory)->createResponse(); // Pre-existing site that uses too much inline code to fix but wants // to ensure resources are loaded only over https and disable plugins: $header = new HeaderContentSecurityPolicy(['default-src' => "https: 'unsafe-eval' 'unsafe-inline'", 'object-src' => "'none'"]); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); // Don't implement the above policy yet; instead just report // violations that would have occurred: $header = new HeaderContentSecurityPolicy(['default-src' => 'https:', 'report-uri' => '/csp-violation-report-endpoint/']); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); ``` #### Content-Security-Policy-Report-Only @@ -373,12 +366,12 @@ $message = (new ResponseFactory)->createResponse(); // You observe how your site behaves, watching for violation reports, // then choose the desired policy enforced by the Content-Security-Policy header. $header = new HeaderContentSecurityPolicy(['default-src' => 'https:', 'report-uri' => '/csp-violation-report-endpoint/']); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); // If you still want to receive reporting, but also want // to enforce a policy, use the Content-Security-Policy header with the report-uri directive. $header = new HeaderContentSecurityPolicy(['default-src' => 'https:', 'report-uri' => '/csp-violation-report-endpoint/']); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); ``` #### Content-Type @@ -392,7 +385,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderContentType('application/json', ['charset' => 'utf-8']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Cookie @@ -406,7 +399,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderCookie(['name' => 'value', 'name2' => 'value2', 'name3' => 'value3']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Date @@ -420,7 +413,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderDate(new \DateTime('now')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Etag @@ -434,7 +427,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderEtag('33a64df551425fcc55e4d42a148795d9f25f89d4'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Expires @@ -448,7 +441,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderExpires(new \DateTime('1 day ago')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Keep-Alive @@ -462,7 +455,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderKeepAlive(['timeout' => '5', 'max' => '1000']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Last-Modified @@ -476,7 +469,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderLastModified(new \DateTime('1 year ago')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Link @@ -492,7 +485,7 @@ $message = (new ResponseFactory)->createResponse(); $uri = (new UriFactory)->createUri('meta.rdf'); $header = new HeaderLink($uri, ['rel' => 'meta']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Location @@ -508,7 +501,7 @@ $message = (new ResponseFactory)->createResponse(); $uri = (new UriFactory)->createUri('/'); $header = new HeaderLocation($uri); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Refresh @@ -524,7 +517,7 @@ $message = (new ResponseFactory)->createResponse(); $uri = (new UriFactory)->createUri('/login'); $header = new HeaderRefresh(3, $uri); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Retry-After @@ -538,7 +531,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderRetryAfter(new \DateTime('+30 second')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Set-Cookie @@ -556,20 +549,20 @@ $message = (new ResponseFactory)->createResponse(); // They don't specify the Expires or Max-Age directives. // Note that web browser have often enabled session restoring. $header = new HeaderSetCookie('sessionid', '38afes7a8', null, ['path' => '/', 'httponly' => true]); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); // Permanent cookie // Instead of expiring when the client is closed, permanent cookies expire // at a specific date (Expires) or after a specific length of time (Max-Age). $header = new HeaderSetCookie('id', 'a3fWa', new \DateTime('+1 day'), ['secure' => true, 'httponly' => true]); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); // Invalid domains // A cookie belonging to a domain that does not include the origin server // should be rejected by the user agent. The following cookie will be rejected // if it was set by a server hosted on originalcompany.com. $header = new HeaderSetCookie('qwerty', '219ffwef9w0f', new \DateTime('+1 day'), ['domain' => 'somecompany.co.uk', 'path' => '/']); -$message = $header->addToMessage($message); +$message = $message->withAddedHeader(...$header); ``` #### Sunset @@ -583,7 +576,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderSunset(new \DateTime('2038-01-19 03:14:07')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Trailer @@ -597,7 +590,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderTrailer('Expires', 'X-Streaming-Error'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Transfer-Encoding @@ -611,7 +604,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderTransferEncoding('gzip', 'chunked'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Vary @@ -625,7 +618,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderVary('User-Agent', 'Content-Language'); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### WWW-Authenticate @@ -639,7 +632,7 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderWWWAuthenticate(HeaderWWWAuthenticate::HTTP_AUTHENTICATE_SCHEME_BASIC, ['realm' => 'Access to the staging site', 'charset' => 'UTF-8']); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); ``` #### Warning @@ -653,13 +646,31 @@ use Sunrise\Http\Message\ResponseFactory; $message = (new ResponseFactory)->createResponse(); $header = new HeaderWarning(HeaderWarning::HTTP_WARNING_CODE_RESPONSE_IS_STALE, 'anderson/1.3.37', 'Response is stale', new \DateTime('now')); -$message = $header->setToMessage($message); +$message = $message->withHeader(...$header); +``` + +### HTTP header collection + +```php +// Create the header collection: +$headers = new \Sunrise\Http\Header\HeaderCollection([ + new \Sunrise\Http\Header\HeaderAllow('HEAD', 'GET'), + new \Sunrise\Http\Header\HeaderContentLanguage('de-DE'), + new \Sunrise\Http\Header\HeaderContentType('application/json', [ + 'charset' => 'UTF-8', + ]), +]); + +// ...and convert the collection to an array: +$headers->all(); ``` +--- + ## Test run ```bash -php vendor/bin/phpunit +composer test ``` ## Useful links From ba606b8a289ced24b9089b518859d0005e8f0ec4 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:17:31 +0500 Subject: [PATCH 09/12] update composer.json --- composer.json | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index a4fa906..98e3f98 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "sunrise/http-header-kit", "homepage": "https://github.com/sunrise-php/http-header-kit", - "description": "Sunrise HTTP header kit for PHP 7.1+ based on PSR-7", + "description": "Sunrise // HTTP header kit for PHP 7.1+ compatible with PSR-7", "license": "MIT", "keywords": [ "fenric", @@ -23,14 +23,14 @@ ], "require": { "php": "^7.1|^8.0", - "psr/http-message": "^1.0", - "sunrise/http-header": "^1.1", - "sunrise/http-header-collection": "^1.1" + "sunrise/http-header": "^2.0", + "sunrise/http-header-collection": "^2.0" }, "require-dev": { "phpunit/phpunit": "7.5.20|9.5.0", "sunrise/coding-standard": "1.0.0", - "sunrise/http-factory": "1.1.0" + "psr/http-message": "~1.0.0", + "sunrise/uri": "~1.2.0" }, "autoload": { "psr-4": { @@ -39,8 +39,12 @@ }, "scripts": { "test": [ - "phpunit --colors=always --coverage-text", - "phpcs" + "phpcs", + "XDEBUG_MODE=coverage phpunit --coverage-text --colors=always" + ], + "build": [ + "phpdoc -d src/ -t phpdoc/", + "XDEBUG_MODE=coverage phpunit --coverage-html coverage/" ] } } From 707a2989c0e0abfc5069263e2ac84f35cfcdc261 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:17:42 +0500 Subject: [PATCH 10/12] update phpunit.xml.dist --- phpunit.xml.dist | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0648e32..b342ca4 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,13 @@ - + + + + ./src + + - + ./tests/ - - - ./src - - From b49db76698dc44505083ddc1fd27536fac355e92 Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:24:58 +0500 Subject: [PATCH 11/12] update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0da813b..c20243b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## HTTP header kit for PHP 7.1+ compatible with PSR-7 +## HTTP header kit for PHP 7.1+ compatible with PSR-7, Symfony and Laravel [![Build Status](https://circleci.com/gh/sunrise-php/http-header-kit.svg?style=shield)](https://circleci.com/gh/sunrise-php/http-header-kit) [![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-header-kit/?branch=master) @@ -37,7 +37,7 @@ $response->header(...$header); ### HTTP Headers -Note that in the examples below will use PSR-7. +> ⚠️ Note that in the examples below will use PSR-7. #### Access-Control-Allow-Credentials From 124169b4f2e8f5cb609d09a97526fc9adea0af8c Mon Sep 17 00:00:00 2001 From: Anatoly Nekhay Date: Sat, 23 Oct 2021 12:27:53 +0500 Subject: [PATCH 12/12] improve code --- src/HeaderDate.php | 3 ++- src/HeaderExpires.php | 3 ++- src/HeaderLastModified.php | 3 ++- src/HeaderRetryAfter.php | 3 ++- src/HeaderSetCookie.php | 3 ++- src/HeaderSunset.php | 3 ++- src/HeaderWarning.php | 3 ++- 7 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/HeaderDate.php b/src/HeaderDate.php index 9425e21..06e7d48 100644 --- a/src/HeaderDate.php +++ b/src/HeaderDate.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; @@ -82,6 +83,6 @@ public function getFieldValue() : string { $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(DateTimeInterface::RFC822); + return $this->getTimestamp()->format(DateTime::RFC822); } } diff --git a/src/HeaderExpires.php b/src/HeaderExpires.php index d2ea456..0cbcb98 100644 --- a/src/HeaderExpires.php +++ b/src/HeaderExpires.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; @@ -82,6 +83,6 @@ public function getFieldValue() : string { $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(DateTimeInterface::RFC822); + return $this->getTimestamp()->format(DateTime::RFC822); } } diff --git a/src/HeaderLastModified.php b/src/HeaderLastModified.php index 103a525..3037a73 100644 --- a/src/HeaderLastModified.php +++ b/src/HeaderLastModified.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; @@ -82,6 +83,6 @@ public function getFieldValue() : string { $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(DateTimeInterface::RFC822); + return $this->getTimestamp()->format(DateTime::RFC822); } } diff --git a/src/HeaderRetryAfter.php b/src/HeaderRetryAfter.php index dae121d..15abf4e 100644 --- a/src/HeaderRetryAfter.php +++ b/src/HeaderRetryAfter.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; @@ -82,6 +83,6 @@ public function getFieldValue() : string { $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(DateTimeInterface::RFC822); + return $this->getTimestamp()->format(DateTime::RFC822); } } diff --git a/src/HeaderSetCookie.php b/src/HeaderSetCookie.php index cd49d46..2be434e 100644 --- a/src/HeaderSetCookie.php +++ b/src/HeaderSetCookie.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; @@ -375,7 +376,7 @@ public function getFieldValue() : string if ($this->getExpires() instanceof DateTimeInterface) { $this->getExpires()->setTimezone(new DateTimeZone('GMT')); - $result .= '; Expires=' . $this->getExpires()->format(DateTimeInterface::RFC822); + $result .= '; Expires=' . $this->getExpires()->format(DateTime::RFC822); $result .= '; Max-Age=' . ($this->getExpires()->getTimestamp() - time()); } diff --git a/src/HeaderSunset.php b/src/HeaderSunset.php index 585200b..f0b72e9 100644 --- a/src/HeaderSunset.php +++ b/src/HeaderSunset.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; @@ -82,6 +83,6 @@ public function getFieldValue() : string { $this->getTimestamp()->setTimezone(new DateTimeZone('GMT')); - return $this->getTimestamp()->format(DateTimeInterface::RFC822); + return $this->getTimestamp()->format(DateTime::RFC822); } } diff --git a/src/HeaderWarning.php b/src/HeaderWarning.php index 08711a7..0fea956 100644 --- a/src/HeaderWarning.php +++ b/src/HeaderWarning.php @@ -14,6 +14,7 @@ /** * Import classes */ +use DateTime; use DateTimeInterface; use DateTimeZone; use InvalidArgumentException; @@ -226,7 +227,7 @@ public function getFieldValue() : string if ($this->getDate() instanceof DateTimeInterface) { $this->getDate()->setTimezone(new DateTimeZone('GMT')); - $result .= ' "' . $this->getDate()->format(DateTimeInterface::RFC822) . '"'; + $result .= ' "' . $this->getDate()->format(DateTime::RFC822) . '"'; } return $result;