11
11
use Stringable ;
12
12
use Throwable ;
13
13
14
- use function abs ;
15
14
use function date_default_timezone_get ;
16
- use function floor ;
17
15
use function is_float ;
18
16
use function is_int ;
19
17
use function json_encode ;
20
- use function preg_match ;
21
18
use function preg_replace ;
22
19
use function round ;
23
20
@@ -34,9 +31,9 @@ final class Value
34
31
public readonly Type $ type ;
35
32
36
33
/**
37
- * @throws InvalidArgument
34
+ * @throws SyntaxError
38
35
*/
39
- public function __construct (mixed $ value )
36
+ public function __construct (Item | Token | Bytes | DisplayString | DateTimeInterface | int | float | string | bool $ value )
40
37
{
41
38
[$ this ->value , $ this ->type ] = match (true ) {
42
39
$ value instanceof Item => [$ value ->value (), $ value ->type ()],
@@ -46,49 +43,7 @@ public function __construct(mixed $value)
46
43
false === $ value ,
47
44
$ value => [$ value , Type::Boolean],
48
45
$ value instanceof DateTimeInterface => [self ::filterDate ($ value ), Type::Date],
49
- is_int ($ value ) => [self ::filterIntegerRange ($ value , 'Integer ' ), Type::Integer],
50
- is_float ($ value ) => [self ::filterDecimal ($ value ), Type::Decimal],
51
- is_string ($ value ) => [self ::filterString ($ value ), Type::String],
52
- default => throw new InvalidArgument ('Unknown or unsupported type. ' )
53
- };
54
- }
55
-
56
- /**
57
- * Filter a decimal according to RFC8941.
58
- *
59
- * @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3.1
60
- */
61
- private static function filterIntegerRange (int $ value , string $ type ): int
62
- {
63
- return match (true ) {
64
- Type::MAXIMUM_INT < abs ($ value ) => throw new SyntaxError ($ type .' are limited to 15 digits. ' ),
65
- default => $ value ,
66
- };
67
- }
68
-
69
- /**
70
- * Filter a decimal according to RFC8941.
71
- *
72
- * @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3.2
73
- */
74
- private static function filterDecimal (float $ value ): float
75
- {
76
- return match (true ) {
77
- Type::MAXIMUM_FLOAT < abs (floor ($ value )) => throw new SyntaxError ('The integer portion of decimals is limited to 12 digits. ' ),
78
- default => $ value ,
79
- };
80
- }
81
-
82
- /**
83
- * Filter a decimal according to RFC8941.
84
- *
85
- * @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3.3
86
- */
87
- private static function filterString (string $ value ): string
88
- {
89
- return match (true ) {
90
- 1 === preg_match ('/[^\x20-\x7E]/i ' , $ value ) => throw new SyntaxError ('The string contains invalid characters. ' ),
91
- default => $ value ,
46
+ default => [$ value , Type::fromVariable ($ value )],
92
47
};
93
48
}
94
49
@@ -99,9 +54,11 @@ private static function filterString(string $value): string
99
54
*/
100
55
private static function filterDate (DateTimeInterface $ value ): DateTimeImmutable
101
56
{
102
- self ::filterIntegerRange ($ value ->getTimestamp (), 'Date timestamp ' );
103
-
104
- return $ value instanceof DateTimeImmutable ? $ value : DateTimeImmutable::createFromInterface ($ value );
57
+ return match (true ) {
58
+ !Type::Integer->supports ($ value ->getTimestamp ()) => throw new SyntaxError ('Date timestamp are limited to 15 digits. ' ),
59
+ !$ value instanceof DateTimeImmutable => DateTimeImmutable::createFromInterface ($ value ),
60
+ default => $ value ,
61
+ };
105
62
}
106
63
107
64
/**
0 commit comments