Lines
100.00%
10 / 10
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| DelimitedMessage | 100.00% | 10 / 10 | 100.00% | 2 / 2 | 5 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Message; | |
| 6 | ||
| 7 | use Override; | |
| 8 | use Psl\Option; | |
| 9 | ||
| 10 | final readonly class DelimitedMessage implements Message | |
| 11 | { | |
| 12 | /** @param non-empty-list<string> $data */ | |
| 13 | public function __construct( | |
| 14 | public array $data, | |
| 15 | public string $raw, | |
| 16 | ) {} | |
| 17 | ||
| 18 | /** @return Option\Option<static> */ | |
| 19 | #[Override] | |
| 20 | public static function from(string $message): Option\Option | |
| 21 | { | |
| 22 | if (!str_starts_with($message, '%') || !str_ends_with($message, '%')) { | |
| 23 | return Option\none(); | |
| 24 | } | |
| 25 | ||
| 26 | $segments = array_values(array_filter( | |
| 27 | explode('%', $message), | |
| 28 | static fn(string $segment): bool => $segment !== '', | |
| 29 | )); | |
| 30 | ||
| 31 | if ($segments === []) { | |
| 32 | return Option\none(); | |
| 33 | } | |
| 34 | ||
| 35 | return Option\some(new self($segments, $message)); | |
| 36 | } | |
| 37 | } |