3708e3a41c407982031ee8f5058c8138fa7c5fd9
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Encryption\Compression;
6
7 use InvalidArgumentException;
8 use function array_key_exists;
9
10 /**
11 * @deprecated This class is deprecated and will be removed in v4.0. Compression is not recommended for JWE.
12 */
13 class CompressionMethodManager
14 {
15 /**
16 * @var CompressionMethod[]
17 */
18 private array $compressionMethods = [];
19
20 /**
21 * @param CompressionMethod[] $methods
22 */
23 public function __construct(iterable $methods = [])
24 {
25 foreach ($methods as $method) {
26 $this->add($method);
27 }
28 }
29
30 /**
31 * Returns true if the givn compression method is supported.
32 */
33 public function has(string $name): bool
34 {
35 return array_key_exists($name, $this->compressionMethods);
36 }
37
38 /**
39 * This method returns the compression method with the given name. Throws an exception if the method is not
40 * supported.
41 *
42 * @param string $name The name of the compression method
43 */
44 public function get(string $name): CompressionMethod
45 {
46 if (! $this->has($name)) {
47 throw new InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name));
48 }
49
50 return $this->compressionMethods[$name];
51 }
52
53 /**
54 * Returns the list of compression method names supported by the manager.
55 *
56 * @return string[]
57 */
58 public function list(): array
59 {
60 return array_keys($this->compressionMethods);
61 }
62
63 /**
64 * Add the given compression method to the manager.
65 */
66 protected function add(CompressionMethod $compressionMethod): void
67 {
68 $name = $compressionMethod->name();
69 $this->compressionMethods[$name] = $compressionMethod;
70 }
71 }