public static function parse(string $data): self
    {
        if (preg_match_all('/([^:\r\n]+):\s+([^\r\n]*(\r\n[ \t][^\r\n]+)*)\r\n/', $data, $matches, PREG_SET_ORDER) === false) {
            throw new FormatException('Invalid header format');
        }

        foreach ($matches as $match) {
            // Undo header folding.
            $value = preg_replace('/\r\n[ \t]([^\r\n]+)/', ' $1', $match[2]);

            $headers[$match[1]] = $value;
        }

        return new self($headers);
    }