| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\CryptoEncoding; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use function is_string; |
| 11 |
use IteratorAggregate; |
| 12 |
use LogicException; |
| 13 |
use const PREG_SET_ORDER; |
| 14 |
use RuntimeException; |
| 15 |
use Stringable; |
| 16 |
use UnexpectedValueException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class PEMBundle implements Countable, IteratorAggregate, Stringable |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private array $pems; |
| 31 |
|
| 32 |
private function __construct(PEM ...$pems) |
| 33 |
{ |
| 34 |
$this->pems = $pems; |
| 35 |
} |
| 36 |
|
| 37 |
public function __toString(): string |
| 38 |
{ |
| 39 |
return $this->string(); |
| 40 |
} |
| 41 |
|
| 42 |
public static function create(PEM ...$pems): self |
| 43 |
{ |
| 44 |
return new self(...$pems); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static function fromString(string $str): self |
| 51 |
{ |
| 52 |
$hasMatches = preg_match_all(PEM::PEM_REGEX, $str, $matches, PREG_SET_ORDER); |
| 53 |
if ($hasMatches === false || $hasMatches === 0) { |
| 54 |
throw new UnexpectedValueException('No PEM blocks.'); |
| 55 |
} |
| 56 |
$pems = array_map( |
| 57 |
static function ($match) { |
| 58 |
$payload = preg_replace('/\s+/', '', $match[2]); |
| 59 |
if (! is_string($payload)) { |
| 60 |
throw new UnexpectedValueException('Failed to decode PEM data.'); |
| 61 |
} |
| 62 |
$data = base64_decode($payload, true); |
| 63 |
if ($data === false) { |
| 64 |
throw new UnexpectedValueException('Failed to decode PEM data.'); |
| 65 |
} |
| 66 |
return PEM::create($match[1], $data); |
| 67 |
}, |
| 68 |
$matches |
| 69 |
); |
| 70 |
return self::create(...$pems); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static function fromFile(string $filename): self |
| 77 |
{ |
| 78 |
if (! is_readable($filename)) { |
| 79 |
throw new RuntimeException("Failed to read {$filename}."); |
| 80 |
} |
| 81 |
$str = file_get_contents($filename); |
| 82 |
if ($str === false) { |
| 83 |
throw new RuntimeException("Failed to read {$filename}."); |
| 84 |
} |
| 85 |
return self::fromString($str); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function withPEMs(PEM ...$pems): self |
| 92 |
{ |
| 93 |
$obj = clone $this; |
| 94 |
$obj->pems = array_merge($obj->pems, $pems); |
| 95 |
return $obj; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function all(): array |
| 104 |
{ |
| 105 |
return $this->pems; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function first(): PEM |
| 112 |
{ |
| 113 |
if (count($this->pems) === 0) { |
| 114 |
throw new LogicException('No PEMs.'); |
| 115 |
} |
| 116 |
return $this->pems[0]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function last(): PEM |
| 123 |
{ |
| 124 |
if (count($this->pems) === 0) { |
| 125 |
throw new LogicException('No PEMs.'); |
| 126 |
} |
| 127 |
return $this->pems[count($this->pems) - 1]; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function count(): int |
| 134 |
{ |
| 135 |
return count($this->pems); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function getIterator(): ArrayIterator |
| 144 |
{ |
| 145 |
return new ArrayIterator($this->pems); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function string(): string |
| 152 |
{ |
| 153 |
return implode("\n", array_map(static fn (PEM $pem) => $pem->string(), $this->pems)); |
| 154 |
} |
| 155 |
} |