1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Encryption\Compression; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CompressionMethodManager. |
18
|
|
|
*/ |
19
|
|
|
final class CompressionMethodManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var CompressionMethodInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $compressionMethods = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param CompressionMethodInterface[] $methods |
28
|
|
|
* |
29
|
|
|
* @return CompressionMethodManager |
30
|
|
|
*/ |
31
|
|
|
public static function create(array $methods): CompressionMethodManager |
32
|
|
|
{ |
33
|
|
|
$manager = new self(); |
34
|
|
|
foreach ($methods as $method) { |
35
|
|
|
$manager->add($method); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $manager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param CompressionMethodInterface $compressionMethod |
43
|
|
|
*/ |
44
|
|
|
protected function add(CompressionMethodInterface $compressionMethod) |
45
|
|
|
{ |
46
|
|
|
$name = $compressionMethod->name(); |
47
|
|
|
if ($this->has($name)) { |
48
|
|
|
throw new \InvalidArgumentException(sprintf('The compression method "%s" is already supported.', $name)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->compressionMethods[$name] = $compressionMethod; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function has(string $name): bool |
60
|
|
|
{ |
61
|
|
|
return array_key_exists($name, $this->compressionMethods); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method will try to find a CompressionInterface object able to support the compression method. |
66
|
|
|
* |
67
|
|
|
* @param string $name The name of the compression method |
68
|
|
|
* |
69
|
|
|
* @return CompressionMethodInterface |
70
|
|
|
*/ |
71
|
|
|
public function get(string $name): CompressionMethodInterface |
72
|
|
|
{ |
73
|
|
|
if (!$this->has($name)) { |
74
|
|
|
throw new \InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->compressionMethods[$name]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string[] |
82
|
|
|
*/ |
83
|
|
|
public function list(): array |
84
|
|
|
{ |
85
|
|
|
return array_keys($this->compressionMethods); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|