Completed
Push — master ( 44e036...1829b3 )
by Julien
18s queued 11s
created

ChromeRequest::setPageRanges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\Gotenberg;
6
7
use function count;
8
9
abstract class ChromeRequest extends Request implements GotenbergRequestInterface
10
{
11
    private const WAIT_DELAY = 'waitDelay';
12
    private const PAPER_WIDTH = 'paperWidth';
13
    private const PAPER_HEIGHT = 'paperHeight';
14
    private const MARGIN_TOP = 'marginTop';
15
    private const MARGIN_BOTTOM  = 'marginBottom';
16
    private const MARGIN_LEFT = 'marginLeft';
17
    private const MARGIN_RIGHT = 'marginRight';
18
    private const LANDSCAPE = 'landscape';
19
    private const PAGE_RANGES = 'pageRanges';
20
    private const GOOGLE_CHROME_RPCC_BUFFER_SIZE = 'googleChromeRpccBufferSize';
21
22
    /** @var float|null */
23
    private $waitDelay;
24
25
    /** @var Document|null */
26
    private $header;
27
28
    /** @var Document|null */
29
    private $footer;
30
31
    /** @var float|null */
32
    private $paperWidth;
33
34
    /** @var float|null */
35
    private $paperHeight;
36
37
    /** @var float|null */
38
    private $marginTop;
39
40
    /** @var float|null */
41
    private $marginBottom;
42
43
    /** @var float|null */
44
    private $marginLeft;
45
46
    /** @var float|null */
47
    private $marginRight;
48
49
    /** @var bool */
50
    private $landscape;
51
52
    /** @var string|null */
53
    private $pageRanges;
54
55
    /** @var int|null */
56
    private $googleChromeRpccBufferSize;
57
58
    /**
59
     * @return array<string,mixed>
60
     */
61
    public function getFormValues(): array
62
    {
63
        $values = parent::getFormValues();
64
        if ($this->waitDelay !== null) {
65
            $values[self::WAIT_DELAY] = $this->waitDelay;
66
        }
67
        if ($this->paperWidth !== null) {
68
            $values[self::PAPER_WIDTH] = $this->paperWidth;
69
        }
70
        if ($this->paperHeight !== null) {
71
            $values[self::PAPER_HEIGHT] = $this->paperHeight;
72
        }
73
        if ($this->marginTop !== null) {
74
            $values[self::MARGIN_TOP] = $this->marginTop;
75
        }
76
        if ($this->marginBottom !== null) {
77
            $values[self::MARGIN_BOTTOM] = $this->marginBottom;
78
        }
79
        if ($this->marginLeft !== null) {
80
            $values[self::MARGIN_LEFT] = $this->marginLeft;
81
        }
82
        if ($this->marginRight !== null) {
83
            $values[self::MARGIN_RIGHT] = $this->marginRight;
84
        }
85
        if ($this->pageRanges !== null) {
86
            $values[self::PAGE_RANGES] = $this->pageRanges;
87
        }
88
        if ($this->googleChromeRpccBufferSize !== null) {
89
            $values[self::GOOGLE_CHROME_RPCC_BUFFER_SIZE] = $this->googleChromeRpccBufferSize;
90
        }
91
        $values[self::LANDSCAPE] = $this->landscape;
92
93
        return $values;
94
    }
95
96
    /**
97
     * @return array<string,Document>
98
     */
99
    public function getFormFiles(): array
100
    {
101
        $files = [];
102
        if (! empty($this->header)) {
103
            $files['header.html'] = $this->header;
104
        }
105
        if (! empty($this->footer)) {
106
            $files['footer.html'] = $this->footer;
107
        }
108
109
        return $files;
110
    }
111
112
    public function setWaitDelay(?float $waitDelay): void
113
    {
114
        $this->waitDelay = $waitDelay;
115
    }
116
117
    /**
118
     * @param float[] $paperSize
119
     *
120
     * @throws RequestException
121
     */
122
    public function setPaperSize(array $paperSize): void
123
    {
124
        if (count($paperSize) !== 2) {
125
            throw new RequestException('Wrong paper size.');
126
        }
127
        $this->paperWidth = $paperSize[0];
128
        $this->paperHeight = $paperSize[1];
129
    }
130
131
    /**
132
     * @param float[] $margins
133
     *
134
     * @throws RequestException
135
     */
136
    public function setMargins(array $margins): void
137
    {
138
        if (count($margins) !== 4) {
139
            throw new RequestException('Wrong margins.');
140
        }
141
        $this->marginTop = $margins[0];
142
        $this->marginBottom = $margins[1];
143
        $this->marginLeft = $margins[2];
144
        $this->marginRight = $margins[3];
145
    }
146
147
    public function setHeader(?Document $header): void
148
    {
149
        $this->header = $header;
150
    }
151
152
    public function setFooter(?Document $footer): void
153
    {
154
        $this->footer = $footer;
155
    }
156
157
    public function setPaperWidth(?float $paperWidth): void
158
    {
159
        $this->paperWidth = $paperWidth;
160
    }
161
162
    public function setPaperHeight(?float $paperHeight): void
163
    {
164
        $this->paperHeight = $paperHeight;
165
    }
166
167
    public function setMarginTop(?float $marginTop): void
168
    {
169
        $this->marginTop = $marginTop;
170
    }
171
172
    public function setMarginBottom(?float $marginBottom): void
173
    {
174
        $this->marginBottom = $marginBottom;
175
    }
176
177
    public function setMarginLeft(?float $marginLeft): void
178
    {
179
        $this->marginLeft = $marginLeft;
180
    }
181
182
    public function setMarginRight(?float $marginRight): void
183
    {
184
        $this->marginRight = $marginRight;
185
    }
186
187
    public function setLandscape(bool $landscape): void
188
    {
189
        $this->landscape = $landscape;
190
    }
191
192
    public function setPageRanges(string $pageRanges): void
193
    {
194
        $this->pageRanges = $pageRanges;
195
    }
196
197
    public function setGoogleChromeRpccBufferSize(?int $googleChromeRpccBufferSize): void
198
    {
199
        $this->googleChromeRpccBufferSize = $googleChromeRpccBufferSize;
200
    }
201
}
202