Skip to content

Commit c2ce895

Browse files
committed
Convert to proper psr-4
1 parent e18ed0b commit c2ce895

File tree

4 files changed

+181
-146
lines changed

4 files changed

+181
-146
lines changed

src/Api2Pdf.php

Lines changed: 42 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
<?php
22

3-
interface Api2PdfInterface
4-
{
5-
public function chromeUrlToPdf($url, $inline = true, $filename = null, $options = null);
6-
public function chromeHtmlToPdf($html, $inline = true, $filename = null, $options = null);
7-
public function chromeUrlToImage($url, $inline = true, $filename = null, $options = null);
8-
public function chromeHtmlToImage($html, $inline = true, $filename = null, $options = null);
9-
public function wkUrlToPdf($url, $inline = true, $filename = null, $options = null, $enableToc = false);
10-
public function wkHtmlToPdf($html, $inline = true, $filename = null, $options = null, $enableToc = false);
11-
public function libreOfficeAnyToPdf($url, $inline = true, $filename = null);
12-
public function libreOfficeThumbnail($url, $inline = true, $filename = null);
13-
public function libreOfficePdfToHtml($url, $inline = true, $filename = null);
14-
public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null);
15-
public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null);
16-
public function pdfsharpMerge($urls, $inline = true, $filename = null);
17-
public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename = null);
18-
public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null, $inline = true, $filename = null);
19-
public function utilityDelete($responseId);
20-
}
3+
namespace Api2Pdf;
214

225
class Api2Pdf implements Api2PdfInterface
236
{
@@ -31,7 +14,8 @@ class Api2Pdf implements Api2PdfInterface
3114
*/
3215
private $base_url = null;
3316

34-
public function __construct($apiKey, $base_url = 'https://v2.api2pdf.com') {
17+
public function __construct($apiKey, $base_url = 'https://v2.api2pdf.com')
18+
{
3519
$this->apiKey = $apiKey;
3620
$this->base_url = $base_url;
3721
}
@@ -41,7 +25,8 @@ public function __construct($apiKey, $base_url = 'https://v2.api2pdf.com') {
4125
* @throws Api2PdfException
4226
* @return Api2PdfResult
4327
*/
44-
public function chromeUrlToPdf($url, $inline = true, $filename = null, $options = null) {
28+
public function chromeUrlToPdf($url, $inline = true, $filename = null, $options = null)
29+
{
4530
$payload = array_merge(
4631
$this->buildPayloadBase($inline, $filename, $options),
4732
[
@@ -57,7 +42,8 @@ public function chromeUrlToPdf($url, $inline = true, $filename = null, $options
5742
* @throws Api2PdfException
5843
* @return Api2PdfResult
5944
*/
60-
public function chromeHtmlToPdf($html, $inline = true, $filename = null, $options = null) {
45+
public function chromeHtmlToPdf($html, $inline = true, $filename = null, $options = null)
46+
{
6147
$payload = array_merge(
6248
$this->buildPayloadBase($inline, $filename, $options),
6349
[
@@ -73,7 +59,8 @@ public function chromeHtmlToPdf($html, $inline = true, $filename = null, $option
7359
* @throws Api2PdfException
7460
* @return Api2PdfResult
7561
*/
76-
public function chromeUrlToImage($url, $inline = true, $filename = null, $options = null) {
62+
public function chromeUrlToImage($url, $inline = true, $filename = null, $options = null)
63+
{
7764
$payload = array_merge(
7865
$this->buildPayloadBase($inline, $filename, $options),
7966
[
@@ -89,7 +76,8 @@ public function chromeUrlToImage($url, $inline = true, $filename = null, $option
8976
* @throws Api2PdfException
9077
* @return Api2PdfResult
9178
*/
92-
public function chromeHtmlToImage($html, $inline = true, $filename = null, $options = null) {
79+
public function chromeHtmlToImage($html, $inline = true, $filename = null, $options = null)
80+
{
9381
$payload = array_merge(
9482
$this->buildPayloadBase($inline, $filename, $options),
9583
[
@@ -106,7 +94,8 @@ public function chromeHtmlToImage($html, $inline = true, $filename = null, $opti
10694
* @throws Api2PdfException
10795
* @return Api2PdfResult
10896
*/
109-
public function wkUrlToPdf($url, $inline = true, $filename = null, $options = null, $enableToc = false) {
97+
public function wkUrlToPdf($url, $inline = true, $filename = null, $options = null, $enableToc = false)
98+
{
11099
$payload = array_merge(
111100
$this->buildPayloadBase($inline, $filename, $options),
112101
[
@@ -124,7 +113,8 @@ public function wkUrlToPdf($url, $inline = true, $filename = null, $options = nu
124113
* @throws Api2PdfException
125114
* @return Api2PdfResult
126115
*/
127-
public function wkHtmlToPdf($html, $inline = true, $filename = null, $options = null, $enableToc = false) {
116+
public function wkHtmlToPdf($html, $inline = true, $filename = null, $options = null, $enableToc = false)
117+
{
128118
$payload = array_merge(
129119
$this->buildPayloadBase($inline, $filename, $options),
130120
[
@@ -142,7 +132,8 @@ public function wkHtmlToPdf($html, $inline = true, $filename = null, $options =
142132
* @throws Api2PdfException
143133
* @return Api2PdfResult
144134
*/
145-
public function libreOfficeAnyToPdf($url, $inline = true, $filename = null) {
135+
public function libreOfficeAnyToPdf($url, $inline = true, $filename = null)
136+
{
146137
$payload = array_merge(
147138
$this->buildPayloadBase($inline, $filename),
148139
[
@@ -159,7 +150,8 @@ public function libreOfficeAnyToPdf($url, $inline = true, $filename = null) {
159150
* @throws Api2PdfException
160151
* @return Api2PdfResult
161152
*/
162-
public function libreOfficeThumbnail($url, $inline = true, $filename = null) {
153+
public function libreOfficeThumbnail($url, $inline = true, $filename = null)
154+
{
163155
$payload = array_merge(
164156
$this->buildPayloadBase($inline, $filename),
165157
[
@@ -176,7 +168,8 @@ public function libreOfficeThumbnail($url, $inline = true, $filename = null) {
176168
* @throws Api2PdfException
177169
* @return Api2PdfResult
178170
*/
179-
public function libreOfficePdfToHtml($url, $inline = true, $filename = null) {
171+
public function libreOfficePdfToHtml($url, $inline = true, $filename = null)
172+
{
180173
$payload = array_merge(
181174
$this->buildPayloadBase($inline, $filename),
182175
[
@@ -193,7 +186,8 @@ public function libreOfficePdfToHtml($url, $inline = true, $filename = null) {
193186
* @throws Api2PdfException
194187
* @return Api2PdfResult
195188
*/
196-
public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null) {
189+
public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null)
190+
{
197191
$payload = array_merge(
198192
$this->buildPayloadBase($inline, $filename),
199193
[
@@ -210,7 +204,8 @@ public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null) {
210204
* @throws Api2PdfException
211205
* @return Api2PdfResult
212206
*/
213-
public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null) {
207+
public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null)
208+
{
214209
$payload = array_merge(
215210
$this->buildPayloadBase($inline, $filename),
216211
[
@@ -227,7 +222,8 @@ public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null) {
227222
* @throws Api2PdfException
228223
* @return Api2PdfResult
229224
*/
230-
public function pdfsharpMerge($urls, $inline = true, $filename = null) {
225+
public function pdfsharpMerge($urls, $inline = true, $filename = null)
226+
{
231227
$payload = array_merge(
232228
$this->buildPayloadBase($inline, $filename),
233229
[
@@ -244,7 +240,8 @@ public function pdfsharpMerge($urls, $inline = true, $filename = null) {
244240
* @throws Api2PdfException
245241
* @return Api2PdfResult
246242
*/
247-
public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename = null) {
243+
public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename = null)
244+
{
248245
$payload = array_merge(
249246
$this->buildPayloadBase($inline, $filename),
250247
[
@@ -262,7 +259,8 @@ public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename
262259
* @throws Api2PdfException
263260
* @return Api2PdfResult
264261
*/
265-
public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null, $inline = true, $filename = null) {
262+
public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null, $inline = true, $filename = null)
263+
{
266264
$payload = array_merge(
267265
$this->buildPayloadBase($inline, $filename),
268266
[
@@ -271,8 +269,9 @@ public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null,
271269
]
272270
);
273271

274-
if (!is_null($ownerpassword))
272+
if (!is_null($ownerpassword)) {
275273
$payload['ownerpassword'] = $ownerpassword;
274+
}
276275

277276
return $this->makeRequest('/pdfsharp/password', $payload);
278277
}
@@ -284,7 +283,8 @@ public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null,
284283
* @throws ConversionException
285284
* @throws ProtocolException
286285
*/
287-
public function utilityDelete($responseId) {
286+
public function utilityDelete($responseId)
287+
{
288288
$url = $this->base_url . '/file/'. $responseId;
289289

290290
$ch = curl_init($url);
@@ -296,7 +296,8 @@ public function utilityDelete($responseId) {
296296

297297
curl_setopt(
298298
$ch,
299-
CURLOPT_HTTPHEADER, [
299+
CURLOPT_HTTPHEADER,
300+
[
300301
'Content-Type: application/json',
301302
'Authorization: '.$this->apiKey
302303
]
@@ -311,13 +312,14 @@ public function utilityDelete($responseId) {
311312
return Api2PdfResult::createFromResponse($response);
312313
}
313314

314-
/**
315+
/**
315316
* @param $endpoint
316317
* @param $payload
317318
* @throws Api2PdfException
318319
* @return Api2PdfResult
319320
*/
320-
private function makeRequest($endpoint, $payload) {
321+
private function makeRequest($endpoint, $payload)
322+
{
321323
$url = $this->base_url . $endpoint;
322324

323325
$ch = curl_init($url);
@@ -332,7 +334,8 @@ private function makeRequest($endpoint, $payload) {
332334

333335
curl_setopt(
334336
$ch,
335-
CURLOPT_HTTPHEADER, [
337+
CURLOPT_HTTPHEADER,
338+
[
336339
'Content-Type: application/json',
337340
'Authorization: '.$this->apiKey
338341
]
@@ -367,110 +370,3 @@ private function buildPayloadBase($inline, $filename, $options = null)
367370
return $payload;
368371
}
369372
}
370-
371-
class Api2PdfResult
372-
{
373-
/**
374-
* @var string|null
375-
*/
376-
private $file = null;
377-
378-
/**
379-
* @var float|null
380-
*/
381-
private $seconds = null;
382-
383-
/**
384-
* @var float|null
385-
*/
386-
private $mbOut = null;
387-
388-
/**
389-
* @var float|null
390-
*/
391-
private $cost = null;
392-
393-
/**
394-
* @var string|null
395-
*/
396-
private $responseId = null;
397-
398-
private $raw_json = null;
399-
400-
public static function createFromResponse($response)
401-
{
402-
$data = json_decode($response, true);
403-
404-
if ($data === false) {
405-
throw new ProtocolException('Error decoding API response');
406-
}
407-
408-
if (!isset($data['Success']) || !$data['Success']) {
409-
if (isset($data['Error'])) {
410-
$errorMessage = $data['Error'];
411-
} else {
412-
$errorMessage = 'Error response received from API';
413-
}
414-
415-
throw new Api2PdfException($errorMessage);
416-
}
417-
418-
$apiResponse = new static();
419-
420-
$apiResponse->file = isset($data['FileUrl'])?$data['FileUrl']: null;
421-
$apiResponse->seconds = $data['Seconds'];
422-
$apiResponse->mbOut = $data['MbOut'];
423-
$apiResponse->cost = $data['Cost'];
424-
$apiResponse->responseId = $data['ResponseId'];
425-
$apiResponse->raw_json = $response;
426-
427-
return $apiResponse;
428-
}
429-
430-
/**
431-
* @return string|null
432-
*/
433-
public function getFile()
434-
{
435-
return $this->file;
436-
}
437-
438-
public function getJson()
439-
{
440-
return $this->raw_json;
441-
}
442-
443-
/**
444-
* @return float|null
445-
*/
446-
public function getSeconds()
447-
{
448-
return $this->seconds;
449-
}
450-
451-
/**
452-
* @return float|null
453-
*/
454-
public function getMbOut()
455-
{
456-
return $this->mbOut;
457-
}
458-
459-
/**
460-
* @return float|null
461-
*/
462-
public function getCost()
463-
{
464-
return $this->cost;
465-
}
466-
467-
/**
468-
* @return string|null
469-
*/
470-
public function getResponseId()
471-
{
472-
return $this->responseId;
473-
}
474-
}
475-
476-
class Api2PdfException extends Exception { }

src/Api2PdfException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Api2Pdf;
4+
5+
use Exception;
6+
7+
class Api2PdfException extends Exception
8+
{
9+
}

src/Api2PdfInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Api2Pdf;
4+
5+
interface Api2PdfInterface
6+
{
7+
public function chromeUrlToPdf($url, $inline = true, $filename = null, $options = null);
8+
public function chromeHtmlToPdf($html, $inline = true, $filename = null, $options = null);
9+
public function chromeUrlToImage($url, $inline = true, $filename = null, $options = null);
10+
public function chromeHtmlToImage($html, $inline = true, $filename = null, $options = null);
11+
public function wkUrlToPdf($url, $inline = true, $filename = null, $options = null, $enableToc = false);
12+
public function wkHtmlToPdf($html, $inline = true, $filename = null, $options = null, $enableToc = false);
13+
public function libreOfficeAnyToPdf($url, $inline = true, $filename = null);
14+
public function libreOfficeThumbnail($url, $inline = true, $filename = null);
15+
public function libreOfficePdfToHtml($url, $inline = true, $filename = null);
16+
public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null);
17+
public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null);
18+
public function pdfsharpMerge($urls, $inline = true, $filename = null);
19+
public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename = null);
20+
public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null, $inline = true, $filename = null);
21+
public function utilityDelete($responseId);
22+
}

0 commit comments

Comments
 (0)