1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Modify\Compress; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Modify\Exception\InvalidCompressionTypeException; |
17
|
|
|
|
18
|
|
|
class CompressionFactory |
19
|
|
|
{ |
20
|
|
|
const TYPE_NONE = 'none'; |
21
|
|
|
const TYPE_UNKNOWN = 'unknown'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CompressorInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $compressors; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DeCompressorInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $deCompressors; |
32
|
|
|
|
33
|
11 |
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
// build known compression types |
36
|
11 |
|
$gzip = new Gzip(); |
37
|
11 |
|
$zip = new Zip(); |
38
|
|
|
|
39
|
11 |
|
$this->addCompressor($gzip); |
40
|
11 |
|
$this->addCompressor($zip); |
41
|
11 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param CompressionTypeInterface $type |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
11 |
|
public function addCompressor(CompressionTypeInterface $type) |
49
|
|
|
{ |
50
|
11 |
|
if ($type instanceof CompressorInterface) { |
51
|
11 |
|
$this->compressors[$type->getName()] = $type; |
52
|
11 |
|
} |
53
|
11 |
|
if ($type instanceof DeCompressorInterface) { |
54
|
11 |
|
$this->deCompressors[$type->getName()] = $type; |
55
|
11 |
|
} |
56
|
|
|
|
57
|
11 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if the specified $compression is valid or not |
62
|
|
|
* |
63
|
|
|
* @param string $compression |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
1 |
|
public function isCompression($compression) |
68
|
|
|
{ |
69
|
1 |
|
return isset($this->compressors[$compression]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $compression |
74
|
|
|
* |
75
|
|
|
* @return CompressorInterface |
76
|
|
|
* @throws InvalidCompressionTypeException |
77
|
|
|
*/ |
78
|
5 |
|
public function getCompressor($compression) |
79
|
|
|
{ |
80
|
5 |
|
if (isset($this->compressors[$compression])) { |
81
|
2 |
|
return $this->compressors[$compression]; |
82
|
|
|
} else { |
83
|
3 |
|
throw new InvalidCompressionTypeException($compression); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $compression |
89
|
|
|
* |
90
|
|
|
* @return DeCompressorInterface |
91
|
|
|
* @throws InvalidCompressionTypeException |
92
|
|
|
*/ |
93
|
7 |
|
public function getDeCompressor($compression) |
94
|
|
|
{ |
95
|
7 |
|
if (isset($this->deCompressors[$compression])) { |
96
|
4 |
|
return $this->deCompressors[$compression]; |
97
|
|
|
} else { |
98
|
3 |
|
throw new InvalidCompressionTypeException($compression); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|