Merger::merge()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace iio\libmergepdf;
6
7
use iio\libmergepdf\Driver\DriverInterface;
8
use iio\libmergepdf\Driver\DefaultDriver;
9
use iio\libmergepdf\Source\SourceInterface;
10
use iio\libmergepdf\Source\FileSource;
11
use iio\libmergepdf\Source\RawSource;
12
13
/**
14
 * Merge existing pdfs into one
15
 *
16
 * Note that your PDFs are merged in the order that you add them
17
 */
18
final class Merger
19
{
20
    /**
21
     * @var SourceInterface[] List of pdf sources to merge
22
     */
23
    private $sources = [];
24
25
    /**
26
     * @var DriverInterface
27
     */
28
    private $driver;
29
30
    public function __construct(DriverInterface $driver = null)
31
    {
32
        $this->driver = $driver ?: new DefaultDriver;
33
    }
34
35
    /**
36
     * Add raw PDF from string
37
     */
38
    public function addRaw(string $content, PagesInterface $pages = null): void
39
    {
40
        $this->sources[] = new RawSource($content, $pages);
41
    }
42
43
    /**
44
     * Add PDF from file
45
     */
46
    public function addFile(string $filename, PagesInterface $pages = null): void
47
    {
48
        $this->sources[] = new FileSource($filename, $pages);
49
    }
50
51
    /**
52
     * Add files using iterator
53
     *
54
     * @param iterable<string> $iterator Set of filenames to add
0 ignored issues
show
Documentation introduced by
The doc-type iterable<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
55
     * @param PagesInterface $pages Optional pages constraint used for every added pdf
56
     */
57
    public function addIterator(iterable $iterator, PagesInterface $pages = null): void
58
    {
59
        foreach ($iterator as $filename) {
60
            $this->addFile($filename, $pages);
61
        }
62
    }
63
64
    /**
65
     * Merges loaded PDFs
66
     */
67
    public function merge(): string
68
    {
69
        return $this->driver->merge(...$this->sources);
70
    }
71
72
    /**
73
     * Reset internal state
74
     */
75
    public function reset(): void
76
    {
77
        $this->sources = [];
78
    }
79
}
80