Passed
Pull Request — master (#8)
by Tim
05:17 queued 03:13
created

Certificate::parseIssuer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Utils;
6
7
/**
8
 * Collection of Utility functions specifically for certificates
9
 */
10
class Certificate
11
{
12
    /**
13
     * The pattern that the contents of a certificate should adhere to
14
     */
15
    public const CERTIFICATE_PATTERN = '/^-----BEGIN CERTIFICATE-----([^-]*)^-----END CERTIFICATE-----/m';
16
17
18
    /**
19
     * @param string $certificate
20
     *
21
     * @return bool
22
     */
23
    public static function hasValidStructure(string $certificate): bool
24
    {
25
        return !!preg_match(self::CERTIFICATE_PATTERN, $certificate);
26
    }
27
28
29
    /**
30
     * @param string $X509CertificateContents
31
     *
32
     * @return string
33
     */
34
    public static function convertToCertificate(string $X509CertificateContents): string
35
    {
36
        return "-----BEGIN CERTIFICATE-----\n"
37
                . chunk_split($X509CertificateContents, 64, "\n")
38
                . "-----END CERTIFICATE-----";
39
    }
40
41
42
    /**
43
     * @param array|string $issuer
44
     *
45
     * @return string
46
     */
47
    public static function parseIssuer($issuer): string
48
    {
49
        if (is_array($issuer)) {
50
            $parts = [];
51
            foreach ($issuer as $key => $value) {
52
                array_unshift($parts, $key . '=' . $value);
53
            }
54
            return implode(',', $parts);
55
        }
56
57
        return $issuer;
58
    }
59
}
60