StreamObject   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 25
c 0
b 0
f 0
dl 0
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A setFilter() 0 4 1
A addRawContent() 0 7 2
A render() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * StreamObject class.
6
 *
7
 * @package   YetiForcePDF\Objects\Basic
8
 *
9
 * @copyright YetiForce Sp. z o.o
10
 * @license   MIT
11
 * @author    Rafal Pospiech <[email protected]>
12
 */
13
14
namespace YetiForcePDF\Objects\Basic;
15
16
/**
17
 * Class StreamObject.
18
 */
19
class StreamObject extends \YetiForcePDF\Objects\PdfObject
20
{
21
	/**
22
	 * Basic object type (integer, string, boolean, dictionary etc..).
23
	 *
24
	 * @var string
25
	 */
26
	protected $basicType = 'Stream';
27
	/**
28
	 * Object name.
29
	 *
30
	 * @var string
31
	 */
32
	protected $name = 'Stream';
33
	/**
34
	 * Content of the stream as string instructions.
35
	 *
36
	 * @var string[]
37
	 */
38
	protected $content = [];
39
	/**
40
	 * Filter used to decode stream.
41
	 *
42
	 * @var string|null
43
	 */
44
	protected $filter;
45
46
	/**
47
	 * Initialisation.
48
	 *
49
	 * @return $this
50
	 */
51
	public function init()
52
	{
53
		parent::init();
54
		$this->id = $this->document->getActualId();
55
		return $this;
56
	}
57
58
	/**
59
	 * Add raw content instructions as string.
60
	 *
61
	 * @param string $content
62
	 * @param string $filter
63
	 *
64
	 * @return \YetiForcePDF\Objects\Basic\StreamObject
65
	 */
66
	public function addRawContent(string $content, string $filter = ''): self
67
	{
68
		$this->content[] = $content;
69
		if ($filter) {
70
			$this->filter = $filter;
71
		}
72
		return $this;
73
	}
74
75
	/**
76
	 * Set filter.
77
	 *
78
	 * @param string $filter
79
	 *
80
	 * @return $this
81
	 */
82
	public function setFilter(string $filter)
83
	{
84
		$this->filter = $filter;
85
		return $this;
86
	}
87
88
	/**
89
	 * {@inheritdoc}
90
	 */
91
	public function render(): string
92
	{
93
		$stream = trim(implode("\n", $this->content), "\n");
94
		$sizeBefore = mb_strlen($stream, '8bit');
95
		if ('FlateDecode' === $this->filter) {
96
			$stream = gzcompress($stream);
97
		}
98
		$filter = $this->filter ? '/Filter /' . $this->filter : '';
99
		return implode("\n", [
100
			$this->getRawId() . ' obj',
101
			'<</Length ' . mb_strlen($stream, '8bit') . '/Lenght1 ' . $sizeBefore . $filter . '>>stream',
102
			$stream,
103
			'endstream',
104
			'endobj',
105
		]);
106
	}
107
}
108