Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Packet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 packetify
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unpacketify
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace AqwSocketClient;
6
7/**
8 * Represents a packet of data to be sent to an AQW server.
9 *
10 * This class handles packet creation and ensures proper formatting by appending
11 * the null terminator character to the raw data. It also provides methods to
12 * extract the raw packet data.
13 */
14final class Packet
15{
16    /**
17     * @param string $data The raw data of the packet.
18     */
19    private function __construct(
20        private readonly string $data,
21    ) {}
22
23    /**
24     * Creates a packet from raw string data.
25     *
26     * Appends the null terminator to the end of the string to conform with
27     * AQW server protocol requirements.
28     *
29     * @param string $data The raw data to be packetified.
30     * @return Packet The newly created packet.
31     */
32    public static function packetify(string $data): Packet
33    {
34        return new self($data . "\u{0000}");
35    }
36
37    /**
38     * Returns the raw data of the packet.
39     *
40     * @return string The packet's data including the null terminator.
41     */
42    public function unpacketify(): string
43    {
44        return $this->data;
45    }
46}