Lines
100.00%
10 / 10
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| from | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 2 | ||
| 12 | final readonly class JsonMessage implements Message | |
| 13 | { | |
| 14 | /** @param array<string, mixed> $data */ | |
| 15 | public function __construct( | |
| 16 | public array $data, | |
| 17 | public string $raw, | |
| 18 | ) {} | |
| 19 | ||
| 20 | /** @return Option\Option<static> */ | |
| 21 | #[Override] | |
| 22 | public static function from(string $message): Option\Option | |
| 23 | { | |
| 24 | $type = Type\json_decoded(Type\shape([ | |
| 25 | 'b' => Type\shape([ | |
| 26 | 'o' => Type\dict(Type\string(), Type\mixed()), | |
| 27 | ], allowUnknownFields: true), | |
| 28 | ], allowUnknownFields: true)); | |
| 29 | ||
| 30 | try { | |
| 31 | $json = $type->coerce($message); | |
| 32 | } catch (CoercionException) { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | return Option\some(new self($json['b']['o'], $message)); | |
| 37 | } | |
| 38 | } |