Lines
100.00%
11 / 11
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| XmlMessage | 100.00% | 11 / 11 | 100.00% | 2 / 2 | 4 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| from | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 3 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Server\Message; | |
| 6 | ||
| 7 | use DOMDocument; | |
| 8 | use Override; | |
| 9 | use Psl\Option; | |
| 10 | ||
| 11 | final readonly class XmlMessage implements Message | |
| 12 | { | |
| 13 | public function __construct( | |
| 14 | public DOMDocument $dom, | |
| 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 ($message === '') { | |
| 23 | return Option\none(); | |
| 24 | } | |
| 25 | ||
| 26 | $dom = new DOMDocument(); | |
| 27 | $previous = libxml_use_internal_errors(true); | |
| 28 | $loaded = $dom->loadXML($message); | |
| 29 | libxml_clear_errors(); | |
| 30 | libxml_use_internal_errors($previous); | |
| 31 | ||
| 32 | if ($loaded === false) { | |
| 33 | return Option\none(); | |
| 34 | } | |
| 35 | ||
| 36 | return Option\some(new self($dom, $message)); | |
| 37 | } | |
| 38 | } |