| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Signature; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use function array_key_exists; |
| 9 |
|
| 10 |
class Signature |
| 11 |
{ |
| 12 |
private readonly ?string $encodedProtectedHeader; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private readonly array $protectedHeader; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function __construct( |
| 24 |
private readonly string $signature, |
| 25 |
array $protectedHeader, |
| 26 |
?string $encodedProtectedHeader, |
| 27 |
private readonly array $header |
| 28 |
) { |
| 29 |
$this->protectedHeader = $encodedProtectedHeader === null ? [] : $protectedHeader; |
| 30 |
$this->encodedProtectedHeader = $encodedProtectedHeader; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function getProtectedHeader(): array |
| 39 |
{ |
| 40 |
return $this->protectedHeader; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function getHeader(): array |
| 49 |
{ |
| 50 |
return $this->header; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function getEncodedProtectedHeader(): ?string |
| 57 |
{ |
| 58 |
return $this->encodedProtectedHeader; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function getProtectedHeaderParameter(string $key) |
| 69 |
{ |
| 70 |
if ($this->hasProtectedHeaderParameter($key)) { |
| 71 |
return $this->getProtectedHeader()[$key]; |
| 72 |
} |
| 73 |
|
| 74 |
throw new InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key)); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function hasProtectedHeaderParameter(string $key): bool |
| 83 |
{ |
| 84 |
return array_key_exists($key, $this->getProtectedHeader()); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function getHeaderParameter(string $key) |
| 95 |
{ |
| 96 |
if (array_key_exists($key, $this->header)) { |
| 97 |
return $this->header[$key]; |
| 98 |
} |
| 99 |
|
| 100 |
throw new InvalidArgumentException(sprintf('The header "%s" does not exist', $key)); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function hasHeaderParameter(string $key): bool |
| 109 |
{ |
| 110 |
return array_key_exists($key, $this->header); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getSignature(): string |
| 117 |
{ |
| 118 |
return $this->signature; |
| 119 |
} |
| 120 |
} |