-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathApi2PdfResult.php
116 lines (95 loc) · 2.13 KB
/
Api2PdfResult.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Api2Pdf;
class Api2PdfResult
{
/**
* @var string|null
*/
private $file = null;
/**
* @var float|null
*/
private $seconds = null;
/**
* @var float|null
*/
private $mbOut = null;
/**
* @var float|null
*/
private $cost = null;
/**
* @var string|null
*/
private $responseId = null;
private $raw_json = null;
public static function createFromResponse($response)
{
$data = json_decode($response, true);
if ($data === false) {
throw new ProtocolException('Error decoding API response');
}
if (!isset($data['Success']) || !$data['Success']) {
if (isset($data['Error'])) {
$errorMessage = $data['Error'];
} else {
$errorMessage = 'Error response received from API';
}
throw new Api2PdfException($errorMessage);
}
$apiResponse = new static();
$apiResponse->file = isset($data['FileUrl'])?$data['FileUrl']: null;
$apiResponse->seconds = $data['Seconds'];
$apiResponse->mbOut = $data['MbOut'];
$apiResponse->cost = $data['Cost'];
$apiResponse->responseId = $data['ResponseId'];
$apiResponse->raw_json = $response;
return $apiResponse;
}
/**
* @return string|null
*/
public function getFile()
{
return $this->file;
}
/**
* @return string|null
*/
public function getFileContents()
{
return $this->file ? file_get_contents($this->file) : null;
}
public function getJson()
{
return $this->raw_json;
}
/**
* @return float|null
*/
public function getSeconds()
{
return $this->seconds;
}
/**
* @return float|null
*/
public function getMbOut()
{
return $this->mbOut;
}
/**
* @return float|null
*/
public function getCost()
{
return $this->cost;
}
/**
* @return string|null
*/
public function getResponseId()
{
return $this->responseId;
}
}