1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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
|
|
|
class CompressionMethodManager |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CompressionMethod[] |
20
|
|
|
*/ |
21
|
|
|
private $compressionMethods = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* CompressionMethodManager constructor. |
25
|
|
|
* |
26
|
|
|
* @deprecated Will be private in v2.x. Please use CompressionManager::create() instead. |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This method creates a Compression Manager with the selected compression methods. |
34
|
|
|
* |
35
|
|
|
* @param CompressionMethod[] $methods |
36
|
|
|
* |
37
|
|
|
* @return CompressionMethodManager |
38
|
|
|
*/ |
39
|
|
|
public static function create(array $methods): self |
40
|
|
|
{ |
41
|
|
|
$manager = new self(); |
42
|
|
|
foreach ($methods as $method) { |
43
|
|
|
$manager->add($method); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $manager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add the given compression method to the manager. |
51
|
|
|
* |
52
|
|
|
* @param CompressionMethod $compressionMethod |
53
|
|
|
*/ |
54
|
|
|
protected function add(CompressionMethod $compressionMethod) |
55
|
|
|
{ |
56
|
|
|
$name = $compressionMethod->name(); |
57
|
|
|
if ($this->has($name)) { |
58
|
|
|
throw new \InvalidArgumentException(sprintf('The compression method "%s" is already supported.', $name)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->compressionMethods[$name] = $compressionMethod; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns true if the givn compression method is supported. |
66
|
|
|
* |
67
|
|
|
* @param string $name |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function has(string $name): bool |
72
|
|
|
{ |
73
|
|
|
return array_key_exists($name, $this->compressionMethods); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This method returns the compression method with the given name. |
78
|
|
|
* Throws an exception if the method is not supported. |
79
|
|
|
* |
80
|
|
|
* @param string $name The name of the compression method |
81
|
|
|
* |
82
|
|
|
* @return CompressionMethod |
83
|
|
|
*/ |
84
|
|
|
public function get(string $name): CompressionMethod |
85
|
|
|
{ |
86
|
|
|
if (!$this->has($name)) { |
87
|
|
|
throw new \InvalidArgumentException(sprintf('The compression method "%s" is not supported.', $name)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->compressionMethods[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the list of compression method names supported by the manager. |
95
|
|
|
* |
96
|
|
|
* @return string[] |
97
|
|
|
*/ |
98
|
|
|
public function list(): array |
99
|
|
|
{ |
100
|
|
|
return array_keys($this->compressionMethods); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|