public static function fromString(string $str): self
    {
        if (!preg_match_all(PEM::PEM_REGEX, $str, $matches, PREG_SET_ORDER)) {
            throw new \UnexpectedValueException("No PEM blocks.");
        }
        $pems = array_map(
            function ($match) {
                $payload = preg_replace('/\s+/', "", $match[2]);
                $data = base64_decode($payload, true);
                if (false === $data) {
                    throw new \UnexpectedValueException(
                        "Failed to decode PEM data.");
                }
                return new PEM($match[1], $data);
            }, $matches);
        return new self(...$pems);
    }