File tree Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -6,11 +6,12 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
6
6
7
7
### Added
8
8
9
- - None
9
+ - Support for the ` DisplayString ` type
10
10
11
11
### Fixed
12
12
13
13
- Tests file moved under the ` /tests ` directory
14
+ - Fix ` Type::tryFromValue ` to correctly detect string type derivative.
14
15
15
16
### Deprecated
16
17
Original file line number Diff line number Diff line change 6
6
7
7
use Stringable ;
8
8
9
+ use Throwable ;
9
10
use function base64_decode ;
10
11
use function base64_encode ;
11
12
use function preg_match ;
@@ -38,6 +39,15 @@ public static function fromEncoded(Stringable|string $encodedValue): self
38
39
return new self ($ decoded );
39
40
}
40
41
42
+ public static function tryFromEncoded (Stringable |string $ encodedValue ): ?self
43
+ {
44
+ try {
45
+ return self ::fromEncoded ($ encodedValue );
46
+ } catch (Throwable ) {
47
+ return null ;
48
+ }
49
+ }
50
+
41
51
/**
42
52
* Returns a new instance from a raw decoded string.
43
53
*/
Original file line number Diff line number Diff line change 5
5
namespace Bakame \Http \StructuredFields ;
6
6
7
7
use DateTimeInterface ;
8
+ use Throwable ;
8
9
9
10
/**
10
11
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
@@ -47,7 +48,12 @@ public static function tryFromValue(mixed $value): self|null
47
48
is_int ($ value ) => Type::Integer,
48
49
is_float ($ value ) => Type::Decimal,
49
50
is_bool ($ value ) => Type::Boolean,
50
- is_string ($ value ) => 1 === preg_match ('/[^\x20-\x7f]/ ' , $ value ) ? Type::DisplayString : Type::String,
51
+ is_string ($ value ) => match (true ) {
52
+ 1 === preg_match ('/[^\x20-\x7f]/ ' , $ value ) => Type::DisplayString,
53
+ 1 === preg_match ("/^([a-z*][a-z\d:\/!# \$%&'*+\-.^_`|~]*)$/i " , $ value ) => Type::Token,
54
+ null !== ByteSequence::tryFromEncoded ($ value ) => Type::ByteSequence,
55
+ default => Type::String,
56
+ },
51
57
default => null ,
52
58
};
53
59
}
You can’t perform that action at this time.
0 commit comments