|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2015 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\Factory; |
|
13
|
|
|
|
|
14
|
|
|
use Jose\Compression\CompressionManager; |
|
15
|
|
|
|
|
16
|
|
|
final class CompressionManagerFactory |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @param string[] $methods |
|
20
|
|
|
* |
|
21
|
|
|
* @return \Jose\Compression\CompressionManagerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function createCompressionManager(array $methods) |
|
24
|
|
|
{ |
|
25
|
|
|
$compression_manager = new CompressionManager(); |
|
26
|
|
|
|
|
27
|
|
|
foreach ($methods as $method => $compression_level) { |
|
28
|
|
|
if (is_string($compression_level)) { |
|
29
|
|
|
$method = $compression_level; |
|
30
|
|
|
$compression_level = -1; |
|
31
|
|
|
} |
|
32
|
|
|
$class = self::getMethodClass($method); |
|
33
|
|
|
$compression_manager->addCompressionAlgorithm(new $class($compression_level)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $compression_manager; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $method |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
private static function isAlgorithmSupported($method) |
|
45
|
|
|
{ |
|
46
|
|
|
return array_key_exists($method, self::getSupportedMethods()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $method |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \InvalidArgumentException |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private static function getMethodClass($method) |
|
57
|
|
|
{ |
|
58
|
|
|
if (self::isAlgorithmSupported($method)) { |
|
59
|
|
|
return self::getSupportedMethods()[$method]; |
|
60
|
|
|
} |
|
61
|
|
|
throw new \InvalidArgumentException(sprintf('Compression method "%s" is not supported.', $method)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private static function getSupportedMethods() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
'DEF' => '\Jose\Compression\Deflate', |
|
68
|
|
|
'GZ' => '\Jose\Compression\GZip', |
|
69
|
|
|
'ZLIB' => '\Jose\Compression\ZLib', |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|