806424ef9aabbe1109d290523c6733c6928fdf9f
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Encryption\Compression;
6
7 use InvalidArgumentException;
8
9 class CompressionMethodManagerFactory
10 {
11 /**
12 * @var CompressionMethod[]
13 */
14 private array $compressionMethods = [];
15
16 /**
17 * This method adds a compression method to this factory. The method is uniquely identified by an alias. This allows
18 * the same method to be added twice (or more) using several configuration options.
19 */
20 public function add(string $alias, CompressionMethod $compressionMethod): void
21 {
22 $this->compressionMethods[$alias] = $compressionMethod;
23 }
24
25 /**
26 * Returns the list of compression method aliases supported by the factory.
27 *
28 * @return string[]
29 */
30 public function aliases(): array
31 {
32 return array_keys($this->compressionMethods);
33 }
34
35 /**
36 * Returns all compression methods supported by this factory.
37 *
38 * @return CompressionMethod[]
39 */
40 public function all(): array
41 {
42 return $this->compressionMethods;
43 }
44
45 /**
46 * Creates a compression method manager using the compression methods identified by the given aliases. If one of the
47 * aliases does not exist, an exception is thrown.
48 *
49 * @param string[] $aliases
50 */
51 public function create(array $aliases): CompressionMethodManager
52 {
53 $compressionMethods = [];
54 foreach ($aliases as $alias) {
55 if (! isset($this->compressionMethods[$alias])) {
56 throw new InvalidArgumentException(sprintf(
57 'The compression method with the alias "%s" is not supported.',
58 $alias
59 ));
60 }
61 $compressionMethods[] = $this->compressionMethods[$alias];
62 }
63
64 return new CompressionMethodManager($compressionMethods);
65 }
66 }