Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Signature/Signature.php

raw symbols SCIP
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 * @var array<string, mixed>
16 */
17 private readonly array $protectedHeader;
18
19 /**
20 * @param array{alg?: string, string?: mixed} $protectedHeader
21 * @param array{alg?: string, string?: mixed} $header
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 * The protected header associated with the signature.
35 *
36 * @return array<string, mixed>
37 */
38 public function getProtectedHeader(): array
39 {
40 return $this->protectedHeader;
41 }
42
43 /**
44 * The unprotected header associated with the signature.
45 *
46 * @return array<string, mixed>
47 */
48 public function getHeader(): array
49 {
50 return $this->header;
51 }
52
53 /**
54 * The protected header associated with the signature.
55 */
56 public function getEncodedProtectedHeader(): ?string
57 {
58 return $this->encodedProtectedHeader;
59 }
60
61 /**
62 * Returns the value of the protected header of the specified key.
63 *
64 * @param string $key The key
65 *
66 * @return mixed|null Header value
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 * Returns true if the protected header has the given parameter.
79 *
80 * @param string $key The key
81 */
82 public function hasProtectedHeaderParameter(string $key): bool
83 {
84 return array_key_exists($key, $this->getProtectedHeader());
85 }
86
87 /**
88 * Returns the value of the unprotected header of the specified key.
89 *
90 * @param string $key The key
91 *
92 * @return mixed|null Header value
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 * Returns true if the unprotected header has the given parameter.
105 *
106 * @param string $key The key
107 */
108 public function hasHeaderParameter(string $key): bool
109 {
110 return array_key_exists($key, $this->header);
111 }
112
113 /**
114 * Returns the value of the signature.
115 */
116 public function getSignature(): string
117 {
118 return $this->signature;
119 }
120 }