Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlateFilter
88.89% covered (warning)
88.89%
8 / 9
66.67% covered (warning)
66.67%
2 / 3
5.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 encode
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 decode
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Filters;
6
7/**
8 * FlateDecode — zlib/deflate compression (ISO 32000-2 §7.4.4).
9 *
10 * The default filter for PDF streams. Compression level 6 (the default)
11 * gives a good speed/size balance for typical PDF content. Higher levels
12 * yield marginal gains at significantly more CPU cost.
13 */
14final class FlateFilter implements FilterInterface
15{
16    public function __construct(private int $level = 6) {}
17
18    public function encode(string $data): string
19    {
20        $result = gzcompress($data, $this->level);
21        if ($result === false) {
22            throw new \RuntimeException('FlateFilter encode failed');
23        }
24        return $result;
25    }
26
27    public function decode(string $data): string
28    {
29        $result = @gzuncompress($data);
30        if ($result === false) {
31            throw new \RuntimeException('FlateFilter decode failed');
32        }
33        return $result;
34    }
35}