Completed
Push — master ( 7a405c...245614 )
by Ben
02:22
created

PageWriter::writePage()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 3
eloc 22
nc 4
nop 2
1
<?php
2
/**
3
 * BaconPdf
4
 *
5
 * @link      http://github.com/Bacon/BaconPdf For the canonical source repository
6
 * @copyright 2015 Ben Scholzen (DASPRiD)
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Bacon\Pdf\Writer;
11
12
use Bacon\Pdf\Rectangle;
13
use DomainException;
14
15
class PageWriter
16
{
17
    /**
18
     * @var ObjectWriter
19
     */
20
    private $objectWriter;
21
22
    /**
23
     * @var int
24
     */
25
    private $pageId;
26
27
    /**
28
     * @var Rectangle[]
29
     */
30
    private $boxes = [];
31
32
    /**
33
     * @var int|null
34
     */
35
    private $rotation;
36
37
    /**
38
     * @var string
39
     */
40
    private $contentStream = '';
41
42
    /**
43
     * @param ObjectWriter $objectWriter
44
     */
45
    public function __construct(ObjectWriter $objectWriter)
46
    {
47
        $this->objectWriter = $objectWriter;
48
        $this->pageId = $this->objectWriter->allocateObjectId();
49
    }
50
51
    /**
52
     * Sets a box for the page.
53
     *
54
     * @param string    $name
55
     * @param Rectangle $box
56
     */
57
    public function setBox($name, Rectangle $box)
58
    {
59
        $this->boxes[$name] = $box;
60
    }
61
62
    /**
63
     * Sets the rotation of the page.
64
     *
65
     * @param  int $degrees
66
     * @throws DomainException
67
     */
68
    public function setRotation($degrees)
69
    {
70
        if (!in_array($degrees, [0, 90, 180, 270])) {
71
            throw new DomainException('Degrees value must be a multiple of 90');
72
        }
73
74
        $this->rotation = $degrees;
75
    }
76
77
    /**
78
     * Appends data to the content stream.
79
     *
80
     * @param string $data
81
     */
82
    public function appendContentStream($data)
83
    {
84
        $this->contentStream .= $data;
85
    }
86
87
    /**
88
     * Writes the page contents and definition to the writer.
89
     *
90
     * @param  ObjectWriter $objectWriter
91
     * @param  int          $pageTreeId
92
     * @return int
93
     */
94
    public function writePage(ObjectWriter $objectWriter, $pageTreeId)
95
    {
96
        $objectWriter->startObject($this->pageId);
97
        $objectWriter->startDictionary();
98
        $objectWriter->writeName('Type');
99
        $objectWriter->writeName('Page');
100
101
        $objectWriter->writeName('Parent');
102
        $objectWriter->writeIndirectReference($pageTreeId);
103
104
        $objectWriter->writeName('Resources');
105
        $objectWriter->startDictionary();
106
        $objectWriter->endDictionary();
107
108
        $objectWriter->writeName('Contents');
109
        $objectWriter->startArray();
110
        $objectWriter->endArray();
111
112
        foreach ($this->boxes as $name => $box) {
113
            $objectWriter->writeName($name);
114
            $box->writeRectangleArray($objectWriter);
115
        }
116
117
        if (null !== $this->rotation) {
118
            $objectWriter->writeName('Rotate');
119
            $objectWriter->writeNumber($this->rotation);
120
        }
121
122
        $objectWriter->endDictionary();
123
        $objectWriter->endObject();
124
        return $this->pageId;
125
    }
126
}
127