Completed
Pull Request — master (#7)
by Julien
03:18
created

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