forked from mindee/mindee-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJob.php
More file actions
94 lines (89 loc) · 3.04 KB
/
Copy pathJob.php
File metadata and controls
94 lines (89 loc) · 3.04 KB
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
<?php
namespace Mindee\Parsing\Common;
use DateTimeImmutable;
use Mindee\Error\ErrorCode;
use Mindee\Error\MindeeApiException;
/**
* Job class for asynchronous requests.
*
* Will hold information on the queue a document has been submitted to.
*/
class Job
{
/**
* @var string|null ID of the job sent by the API in response to an enqueue request.
*/
public ?string $id;
/**
* @var DateTimeImmutable|null Timestamp of the request reception by the API.
*/
public ?DateTimeImmutable $issuedAt;
/**
* @var DateTimeImmutable|null Timestamp of the request after it has been completed.
*/
public ?DateTimeImmutable $availableAt;
/**
* @var string|null Status of the request, as seen by the API.
*/
public ?string $status;
/**
* @var integer|null Time (ms) taken for the request to be processed by the API.
*/
public ?int $millisecsTaken;
/**
* @var array|null Information about an error that occurred during the job processing.
*/
public ?array $error;
/**
* @param array $rawResponse Raw prediction array.
* @throws MindeeApiException Throws if a date is faulty.
*/
public function __construct(array $rawResponse)
{
try {
$this->issuedAt = new DateTimeImmutable($rawResponse['issued_at']);
} catch (\Exception $e) {
try {
$this->issuedAt = new DateTimeImmutable(strtotime($rawResponse['issued_at']));
} catch (\Exception $e2) {
throw new MindeeApiException(
"Could not create date from " . $rawResponse['issued_at'],
ErrorCode::API_UNPROCESSABLE_ENTITY,
$e2
);
}
}
$this->id = $rawResponse['id'];
$this->status = $rawResponse['status'];
if (array_key_exists('available_at', $rawResponse) && $rawResponse['available_at'] !== null && strtotime($rawResponse['available_at'])) {
try {
$this->availableAt = new DateTimeImmutable($rawResponse['available_at']);
} catch (\Exception $e) {
try {
$this->availableAt = new DateTimeImmutable(strtotime($rawResponse['available_at']));
} catch (\Exception $e2) {
throw new MindeeApiException(
"Could not create date from " . $rawResponse['available_at'],
ErrorCode::API_UNPROCESSABLE_ENTITY,
$e
);
}
}
$ts1 = (int)$this->availableAt->format('Uv');
$ts2 = (int)$this->issuedAt->format('Uv');
$this->millisecsTaken = $ts2 - $ts1;
} else {
$this->availableAt = null;
$this->millisecsTaken = null;
}
}
/**
* @return string
*/
public function __toString(): string
{
$objAsJson = get_object_vars($this);
ksort($objAsJson);
return json_encode($objAsJson, JSON_PRETTY_PRINT);
}
}