17
17
* can_fail?: bool,
18
18
* expected?: array,
19
19
* }
20
- * @phpstan-type itemValue array{__type:string, value:int|string}|string|bool|int|float|null
20
+ * @phpstan-type CustomValueType array{
21
+ * __type: 'binary'|'date'|'displaystring'|'token',
22
+ * value: int|string
23
+ * }
24
+ * @phpstan-type ItemValue CustomValueType|string|bool|int|float|null
21
25
*/
22
26
final class Record
23
27
{
24
28
private function __construct (
25
29
public readonly string $ name ,
26
- /** @var 'dictionary'|'list'|'item' */
27
- public readonly string $ type ,
30
+ public readonly DataType $ type ,
28
31
/** @var array<string> */
29
32
public readonly array $ raw ,
30
33
/** @var array<string> */
31
34
public readonly array $ canonical ,
32
35
public readonly bool $ mustFail ,
33
36
public readonly bool $ canFail ,
34
- public readonly OuterList |Dictionary |InnerList | Item | Parameters |null $ expected
37
+ public readonly OuterList |Dictionary |Item |null $ expected
35
38
) {
36
39
}
37
40
@@ -41,30 +44,31 @@ private function __construct(
41
44
public static function fromDecoded (array $ data ): self
42
45
{
43
46
$ data += ['canonical ' => $ data ['raw ' ], 'must_fail ' => false , 'can_fail ' => false , 'expected ' => []];
47
+ $ dataType = DataType::from ($ data ['header_type ' ]);
44
48
45
49
return new self (
46
50
$ data ['name ' ],
47
- $ data [ ' header_type ' ] ,
51
+ $ dataType ,
48
52
$ data ['raw ' ],
49
53
$ data ['canonical ' ],
50
54
$ data ['must_fail ' ],
51
55
$ data ['can_fail ' ],
52
- self ::parseExpected ($ data [ ' header_type ' ] , $ data ['expected ' ])
56
+ self ::parseExpected ($ dataType , $ data ['expected ' ])
53
57
);
54
58
}
55
59
56
- private static function parseExpected (string $ dataTypeValue , array $ expected ): OuterList |Dictionary |InnerList | Item | Parameters |null
60
+ private static function parseExpected (DataType $ dataType , array $ expected ): OuterList |Dictionary |Item |null
57
61
{
58
- return match (DataType:: tryFrom ( $ dataTypeValue ) ) {
62
+ return match ($ dataType ) {
59
63
DataType::Dictionary => self ::parseDictionary ($ expected ),
60
64
DataType::List => self ::parseList ($ expected ),
61
65
DataType::Item => self ::parseItem ($ expected ),
62
- default => null ,
66
+ default => throw new ValueError ( ' The structured field can not be of the type " ' . $ dataType -> value . ' ". ' ) ,
63
67
};
64
68
}
65
69
66
70
/**
67
- * @param itemValue $data
71
+ * @param ItemValue $data
68
72
*/
69
73
private static function parseValue (array |string |bool |int |float |null $ data ): Token |DateTimeImmutable |Bytes |DisplayString |string |bool |int |float |null
70
74
{
@@ -83,7 +87,7 @@ private static function parseValue(array|string|bool|int|float|null $data): Toke
83
87
}
84
88
85
89
/**
86
- * @param array<array{0:string, 1:itemValue > $parameters
90
+ * @param array<array{0:string, 1:ItemValue > $parameters
87
91
*/
88
92
private static function parseParameters (array $ parameters ): Parameters
89
93
{
@@ -94,18 +98,16 @@ private static function parseParameters(array $parameters): Parameters
94
98
}
95
99
96
100
/**
97
- * @param array{0:itemValue , 1:array<array{0:string, 1:itemValue }>}|itemValue $value
101
+ * @param array{0:ItemValue , 1:array<array{0:string, 1:ItemValue }>}|ItemValue $value
98
102
*/
99
- private static function parseItem (mixed $ value ): ?Item
103
+ private static function parseItem (array | string | int $ value ): ?Item
100
104
{
101
- return match (true ) {
102
- !is_array ($ value ) => Item::new ($ value ),
103
- [] === $ value => null ,
104
- default => Item::new ([
105
- self ::parseValue ($ value [0 ]),
106
- self ::parseParameters ($ value [1 ]),
107
- ]),
108
- };
105
+ return Item::tryfromPair (match (true ) {
106
+ !is_array ($ value ) => [$ value ],
107
+ [] === $ value => [],
108
+ 1 === count ($ value ) => [$ value ],
109
+ default => [self ::parseValue ($ value [0 ]), self ::parseParameters ($ value [1 ])],
110
+ });
109
111
}
110
112
111
113
private static function parseInnerList (array $ innerListPair ): InnerList
0 commit comments