11<?php
22
3- declare (strict_types = 1 );
3+ declare (strict_types= 1 );
44
55namespace Sop \CryptoEncoding ;
66
7+ use ArrayIterator ;
8+ use Countable ;
9+ use IteratorAggregate ;
10+ use LogicException ;
11+ use RuntimeException ;
12+ use UnexpectedValueException ;
13+
14+ use function array_map ;
15+ use function array_merge ;
16+ use function base64_decode ;
17+ use function count ;
18+ use function implode ;
19+ use function is_readable ;
20+ use function file_get_contents ;
21+ use function preg_match_all ;
22+ use function preg_replace ;
23+
724/**
825 * Container for multiple PEM objects.
926 *
1027 * The order of PEMs shall be retained, eg. when read from a file.
1128 */
12- class PEMBundle implements \ Countable, \ IteratorAggregate
29+ class PEMBundle implements Countable, IteratorAggregate
1330{
1431 /**
1532 * Array of PEM objects.
1633 *
17- * @var PEM[]
34+ * @var \Sop\CryptoEncoding\ PEM[]
1835 */
19- protected $ _pems ;
36+ protected $ pems ;
2037
2138 /**
2239 * Constructor.
2340 *
24- * @param PEM ... $pems
41+ * @param \Sop\CryptoEncoding\ PEM[] $pems
2542 */
2643 public function __construct (PEM ...$ pems )
2744 {
28- $ this ->_pems = $ pems ;
45+ $ this ->pems = $ pems ;
2946 }
3047
3148 /**
@@ -48,18 +65,21 @@ public function __toString(): string
4865 public static function fromString (string $ str ): self
4966 {
5067 if (!preg_match_all (PEM ::PEM_REGEX , $ str , $ matches , PREG_SET_ORDER )) {
51- throw new \ UnexpectedValueException ('No PEM blocks. ' );
68+ throw new UnexpectedValueException ('No PEM blocks. ' );
5269 }
70+
5371 $ pems = array_map (
5472 function ($ match ) {
5573 $ payload = preg_replace ('/\s+/ ' , '' , $ match [2 ]);
5674 $ data = base64_decode ($ payload , true );
5775 if (false === $ data ) {
58- throw new \UnexpectedValueException (
59- 'Failed to decode PEM data. ' );
76+ throw new UnexpectedValueException ('Failed to decode PEM data. ' );
6077 }
6178 return new PEM ($ match [1 ], $ data );
62- }, $ matches );
79+ },
80+ $ matches
81+ );
82+
6383 return new self (...$ pems );
6484 }
6585
@@ -74,9 +94,11 @@ function ($match) {
7494 */
7595 public static function fromFile (string $ filename ): self
7696 {
77- if (!is_readable ($ filename ) ||
78- false === ($ str = file_get_contents ($ filename ))) {
79- throw new \RuntimeException ("Failed to read {$ filename }. " );
97+ if (
98+ !is_readable ($ filename ) ||
99+ false === ($ str = file_get_contents ($ filename ))
100+ ) {
101+ throw new RuntimeException ("Failed to read {$ filename }. " );
80102 }
81103 return self ::fromString ($ str );
82104 }
@@ -91,48 +113,48 @@ public static function fromFile(string $filename): self
91113 public function withPEMs (PEM ...$ pems ): self
92114 {
93115 $ obj = clone $ this ;
94- $ obj ->_pems = array_merge ($ obj ->_pems , $ pems );
116+ $ obj ->pems = array_merge ($ obj ->pems , $ pems );
95117 return $ obj ;
96118 }
97119
98120 /**
99121 * Get all PEMs in a bundle.
100122 *
101- * @return PEM[]
123+ * @return \Sop\CryptoEncoding\ PEM[]
102124 */
103125 public function all (): array
104126 {
105- return $ this ->_pems ;
127+ return $ this ->pems ;
106128 }
107129
108130 /**
109131 * Get the first PEM in a bundle.
110132 *
111133 * @throws \LogicException If bundle contains no PEM objects
112134 *
113- * @return PEM
135+ * @return \Sop\CryptoEncoding\ PEM
114136 */
115137 public function first (): PEM
116138 {
117- if (!count ($ this ->_pems )) {
118- throw new \ LogicException ('No PEMs. ' );
139+ if (!count ($ this ->pems )) {
140+ throw new LogicException ('No PEMs. ' );
119141 }
120- return $ this ->_pems [0 ];
142+ return $ this ->pems [0 ];
121143 }
122144
123145 /**
124146 * Get the last PEM in a bundle.
125147 *
126148 * @throws \LogicException If bundle contains no PEM objects
127149 *
128- * @return PEM
150+ * @return \Sop\CryptoEncoding\ PEM
129151 */
130152 public function last (): PEM
131153 {
132- if (!count ($ this ->_pems )) {
133- throw new \ LogicException ('No PEMs. ' );
154+ if (!count ($ this ->pems )) {
155+ throw new LogicException ('No PEMs. ' );
134156 }
135- return $ this ->_pems [count ($ this ->_pems ) - 1 ];
157+ return $ this ->pems [count ($ this ->pems ) - 1 ];
136158 }
137159
138160 /**
@@ -142,7 +164,7 @@ public function last(): PEM
142164 */
143165 public function count (): int
144166 {
145- return count ($ this ->_pems );
167+ return count ($ this ->pems );
146168 }
147169
148170 /**
@@ -152,9 +174,9 @@ public function count(): int
152174 *
153175 * @return \ArrayIterator
154176 */
155- public function getIterator (): \ ArrayIterator
177+ public function getIterator (): ArrayIterator
156178 {
157- return new \ ArrayIterator ($ this ->_pems );
179+ return new ArrayIterator ($ this ->pems );
158180 }
159181
160182 /**
@@ -164,10 +186,14 @@ public function getIterator(): \ArrayIterator
164186 */
165187 public function string (): string
166188 {
167- return implode ("\n" ,
189+ return implode (
190+ "\n" ,
168191 array_map (
169192 function (PEM $ pem ) {
170193 return $ pem ->string ();
171- }, $ this ->_pems ));
194+ },
195+ $ this ->pems
196+ )
197+ );
172198 }
173199}
0 commit comments