PrivateKey::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Domain\Keypair;
4
5
use \InvalidArgumentException;
6
7
final class PrivateKey
8
{
9
    /**
10
     * @var string
11
     */
12
    private $key;
13
14
    /**
15
     * @param string $privateKey
16
     */
17
    public function __construct(string $privateKey)
18
    {
19
        if (!preg_match('/^\-\-\-\-\-BEGIN PRIVATE KEY\-\-\-\-\-[\S\n]+\-\-\-\-\-END PRIVATE KEY\-\-\-\-\-$/', $privateKey)) {
20
            throw new InvalidArgumentException("Invalid PrivateKey format. Please use a shielded format");
21
        }
22
23
        $this->key = $privateKey;
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public function __toString()
30
    {
31
        return $this->key;
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function __debugInfo()
38
    {
39
        // Hide the actual key from backtraces
40
        return ['key' => '***PRIVATE KEY***'];
41
    }
42
}
43