1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bunq\Certificate; |
4
|
|
|
|
5
|
|
|
final class CertificateType |
6
|
|
|
{ |
7
|
|
|
const PRIVATE_KEY = 'private.pem'; |
8
|
|
|
const PUBLIC_KEY = 'public.pem'; |
9
|
|
|
const BUNQ_SERVER_KEY = 'public_server_key.pem'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $certificateType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return self[] |
18
|
|
|
*/ |
19
|
|
|
public static function all() |
20
|
|
|
{ |
21
|
|
|
return [ |
22
|
|
|
self::PRIVATE_KEY(), |
23
|
|
|
self::PUBLIC_KEY(), |
24
|
|
|
self::BUNQ_SERVER_KEY(), |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string[] |
30
|
|
|
*/ |
31
|
|
|
public static function allAsString() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
self::PRIVATE_KEY, |
35
|
|
|
self::PUBLIC_KEY, |
36
|
|
|
self::BUNQ_SERVER_KEY, |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return CertificateType |
42
|
|
|
*/ |
43
|
|
|
public static function PRIVATE_KEY() |
44
|
|
|
{ |
45
|
|
|
return new self(self::PRIVATE_KEY); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return CertificateType |
50
|
|
|
*/ |
51
|
|
|
public static function PUBLIC_KEY() |
52
|
|
|
{ |
53
|
|
|
return new self(self::PUBLIC_KEY); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return CertificateType |
58
|
|
|
*/ |
59
|
|
|
public static function BUNQ_SERVER_KEY() |
60
|
|
|
{ |
61
|
|
|
return new self(self::BUNQ_SERVER_KEY); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $certificate |
66
|
|
|
* |
67
|
|
|
* @return CertificateType |
68
|
|
|
*/ |
69
|
|
|
public static function fromString($certificate) |
70
|
|
|
{ |
71
|
|
|
return new self($certificate); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $other |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function equals($other) |
80
|
|
|
{ |
81
|
|
|
return $other == $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function toString() |
88
|
|
|
{ |
89
|
|
|
return $this->certificateType; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function __toString() |
96
|
|
|
{ |
97
|
|
|
return $this->toString(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $certificateType |
102
|
|
|
*/ |
103
|
|
|
private function __construct($certificateType) |
104
|
|
|
{ |
105
|
|
|
$this->certificateType = (string)$certificateType; |
106
|
|
|
|
107
|
|
|
$this->protect(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check if the certificateType exists in our list |
112
|
|
|
*/ |
113
|
|
|
private function protect() |
114
|
|
|
{ |
115
|
|
|
if (!in_array($this->certificateType, self::allAsString(), true)) { |
116
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid certificate type %s', $this->certificateType)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|