Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| XmlMessage | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| from | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AqwSocketClient\Messages; |
| 6 | |
| 7 | use AqwSocketClient\Interfaces\MessageInterface; |
| 8 | use DOMDocument; |
| 9 | use Override; |
| 10 | |
| 11 | /** |
| 12 | * This class wraps a raw string and attempts to parse it into a |
| 13 | * {@see \DOMDocument} object for easy access and interpretation. |
| 14 | */ |
| 15 | final class XmlMessage implements MessageInterface |
| 16 | { |
| 17 | /** |
| 18 | * @param DOMDocument $dom The parsed XML data of the message, accessible as a DOMDocument. |
| 19 | */ |
| 20 | private function __construct( |
| 21 | public readonly DOMDocument $dom, |
| 22 | public readonly string $raw, |
| 23 | ) {} |
| 24 | |
| 25 | /** |
| 26 | * Attempts to create an XmlMessage object by loading the raw string as XML. |
| 27 | * |
| 28 | * Parsing failures (e.g., incomplete or malformed XML) result in `false`. |
| 29 | * |
| 30 | * @param string $message The raw string data received from the socket. |
| 31 | * @return MessageInterface|false The newly created message object containing the |
| 32 | * parsed DOMDocument, or **false** on failure to load the XML. |
| 33 | */ |
| 34 | #[Override] |
| 35 | public static function from(string $message): MessageInterface|false |
| 36 | { |
| 37 | $dom = new DOMDocument(); |
| 38 | |
| 39 | if ($message === '') { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // @mago-expect lint:no-error-control-operator |
| 44 | $success = @$dom->loadXML($message); |
| 45 | if (!$success) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | return new self($dom, $message); |
| 50 | } |
| 51 | } |