File tree Expand file tree Collapse file tree 6 files changed +45
-0
lines changed Expand file tree Collapse file tree 6 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
7
7
### Added
8
8
9
9
- Support for the ` DisplayString ` type
10
+ - ` ByteSequence::tryFromEncoded `
11
+ - ` Token::tryFromString `
10
12
11
13
### Fixed
12
14
Original file line number Diff line number Diff line change 6
6
7
7
use Stringable ;
8
8
9
+ use Throwable ;
10
+
9
11
use function preg_match ;
10
12
use function preg_replace_callback ;
11
13
use function rawurldecode ;
@@ -21,6 +23,15 @@ private function __construct(
21
23
) {
22
24
}
23
25
26
+ public static function tryFromEncoded (Stringable |string $ encodedValue ): ?self
27
+ {
28
+ try {
29
+ return self ::fromEncoded ($ encodedValue );
30
+ } catch (Throwable ) {
31
+ return null ;
32
+ }
33
+ }
34
+
24
35
/**
25
36
* Returns a new instance from a Base64 encoded string.
26
37
*/
Original file line number Diff line number Diff line change 6
6
7
7
use Stringable ;
8
8
9
+ use Throwable ;
10
+
9
11
use function preg_match ;
10
12
11
13
/**
@@ -25,6 +27,15 @@ public function toString(): string
25
27
return $ this ->value ;
26
28
}
27
29
30
+ public static function tryFromString (Stringable |string $ value ): ?self
31
+ {
32
+ try {
33
+ return self ::fromString ($ value );
34
+ } catch (Throwable ) {
35
+ return null ;
36
+ }
37
+ }
38
+
28
39
public static function fromString (Stringable |string $ value ): self
29
40
{
30
41
return new self ((string ) $ value );
Original file line number Diff line number Diff line change @@ -21,6 +21,13 @@ public function it_will_fail_on_invalid_decoded_string_with_inner_space(): void
21
21
ByteSequence::fromEncoded ('a a ' );
22
22
}
23
23
24
+ #[Test]
25
+ public function it_will_return_null_on_invalid_encoded_string (): void
26
+ {
27
+ self ::assertNull (ByteSequence::tryFromEncoded ('a a ' ));
28
+ self ::assertNull (ByteSequence::tryFromEncoded ('aaaaa ' ));
29
+ }
30
+
24
31
#[Test]
25
32
public function it_will_fail_on_invalid_decoded_string (): void
26
33
{
Original file line number Diff line number Diff line change @@ -29,6 +29,13 @@ public function it_will_fail_on_invalid_decoded_string(): void
29
29
DisplayString::fromEncoded ('%c3%28" ' );
30
30
}
31
31
32
+ #[Test]
33
+ public function it_will_return_null_on_invalid_encoded_string (): void
34
+ {
35
+ self ::assertNull (DisplayString::tryFromEncoded ('a %a ' ));
36
+ self ::assertNull (DisplayString::tryFromEncoded ('%c3%28" ' ));
37
+ }
38
+
32
39
#[Test]
33
40
public function it_can_decode_base64_field (): void
34
41
{
Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ public function it_will_fail_on_invalid_token_string(string $httpValue): void
24
24
Token::fromString ($ httpValue );
25
25
}
26
26
27
+ #[Test]
28
+ #[DataProvider('invalidTokenString ' )]
29
+ public function it_will_return_null_on_invalid_encoded_string (string $ httpValue ): void
30
+ {
31
+ self ::assertNull (Token::tryFromString ($ httpValue ));
32
+ }
33
+
27
34
/**
28
35
* @return array<array{0:string}>
29
36
*/
You can’t perform that action at this time.
0 commit comments