Completed
Push — master ( d8c018...19ac78 )
by Florent
06:38
created

Deflate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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\Compression;
13
14
use Assert\Assertion;
15
16
/**
17
 * This class implements the compression algorithm DEF (defalte)
18
 * This compression algorithm is part of the specification.
19
 */
20
final class Deflate implements CompressionInterface
21
{
22
    /**
23
     * @var int
24
     */
25
    protected $compression_level = -1;
26
27
    /**
28
     * Deflate constructor.
29
     *
30
     * @param int $compression_level
31
     */
32
    public function __construct($compression_level = -1)
33
    {
34
        Assertion::integer($compression_level, 'The compression level can be given as 0 for no compression up to 9 for maximum compression. If -1 given, the default compression level will be the default compression level of the zlib library.');
35
        Assertion::range($compression_level, -1, 9, 'The compression level can be given as 0 for no compression up to 9 for maximum compression. If -1 given, the default compression level will be the default compression level of the zlib library.');
36
37
        $this->compression_level = $compression_level;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    private function getCompressionLevel()
44
    {
45
        return $this->compression_level;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getMethodName()
52
    {
53
        return 'DEF';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function compress($data)
60
    {
61
        $data = gzdeflate($data, $this->getCompressionLevel());
62
        Assertion::false(false === $data, 'Unable to compress data');
63
64
        return $data;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function uncompress($data)
71
    {
72
        $data = gzinflate($data);
73
        Assertion::false(false === $data, 'Unable to uncompress data');
74
75
        return $data;
76
    }
77
}
78