| Total Complexity | 6 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 14 | class Parser |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string $bytes |
||
| 19 | * @param array $data |
||
| 20 | * @return array<array{format: string, big_endian?: bool}> |
||
| 21 | * @throws Exception |
||
| 22 | */ |
||
| 23 | public static function decode(string $bytes, array $data): array |
||
| 24 | { |
||
| 25 | $deserializer = new Deserializer($bytes); |
||
| 26 | $result = []; |
||
| 27 | $index = 0; |
||
| 28 | foreach ($data as $value) { |
||
| 29 | $format = $value['format'] ?? null; |
||
| 30 | if (empty($format)) { |
||
| 31 | continue; |
||
| 32 | } |
||
| 33 | $bigEndian = $value['big_endian'] ?? false; |
||
| 34 | $result[$index] = match ($format) { |
||
| 35 | Formats::LONG => $deserializer->readLong($bigEndian), |
||
| 36 | Formats::LONG_LONG => $deserializer->readLongLong($bigEndian), |
||
| 37 | Formats::STRING => $deserializer->readString() |
||
| 38 | }; |
||
| 39 | $index++; |
||
| 40 | } |
||
| 41 | return $result; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param array<array{format: string, value: mixed, big_endian?: bool}> $data |
||
| 46 | * @return string |
||
| 47 | * @throws Exception |
||
| 48 | */ |
||
| 49 | public static function encode(array $data): string |
||
| 66 | } |
||
| 67 | |||
| 68 | } |