|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ThallesDKoester\Entregas; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Sunra\PhpSimple\HtmlDomParser; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Thalles D. Koester | Class Rastreio |
|
10
|
|
|
* |
|
11
|
|
|
* @author Thalles D. koester <[email protected]> |
|
12
|
|
|
* @package ThallesDKoester\Entregas |
|
13
|
|
|
*/ |
|
14
|
|
|
class Rastreio |
|
15
|
|
|
{ |
|
16
|
|
|
use Curl; |
|
17
|
|
|
|
|
18
|
|
|
const ALTERNATIVE = 'https://www2.correios.com.br/sistemas/rastreamento/resultado_semcontent.cfm'; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $code; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
private $error; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $options = []; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Rastreio constructor. |
|
31
|
|
|
* @param string $code |
|
32
|
|
|
* @param array|null $options |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(string $code, ?array $options = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->setCode($code); |
|
37
|
|
|
|
|
38
|
|
|
if (!empty($options)) { |
|
39
|
|
|
$this->setOptions($options); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array|null |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getPath(): ?array |
|
47
|
|
|
{ |
|
48
|
|
|
$url = self::ALTERNATIVE; |
|
49
|
|
|
try { |
|
50
|
|
|
$data = $this->request($url, 'POST', ['Objetos' => $this->code]); |
|
51
|
|
|
} catch (Exception $e) { |
|
52
|
|
|
$this->error = $e; |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->parseHtml($data); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $code |
|
61
|
|
|
*/ |
|
62
|
|
|
private function setCode(string $code): void |
|
63
|
|
|
{ |
|
64
|
|
|
if (empty($code)) { |
|
65
|
|
|
$this->error = 'Insira o código de rastreio para proceguir'; |
|
66
|
|
|
} |
|
67
|
|
|
$this->code = $code; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $options |
|
72
|
|
|
*/ |
|
73
|
|
|
private function setOptions(array $options): void |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($options as $key => $value) { |
|
76
|
|
|
$this->options[$key] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $hmtl |
|
82
|
|
|
* @return array|null |
|
83
|
|
|
*/ |
|
84
|
|
|
private function parseHtml(string $hmtl): ?array |
|
85
|
|
|
{ |
|
86
|
|
|
$dom = HtmlDomParser::str_get_html($hmtl); |
|
87
|
|
|
$table = $dom->find('.listEvent'); |
|
88
|
|
|
|
|
89
|
|
|
if (!$table) { |
|
90
|
|
|
$this->error = 'Erro no parsing'; |
|
91
|
|
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$return = []; |
|
95
|
|
|
foreach ($table->children() as $key => $value) { |
|
96
|
|
|
$datetime = explode('<br />', $value->find('.sroDtEvent')->plaintext); |
|
97
|
|
|
$return[] = [ |
|
98
|
|
|
'date' => $datetime[0], |
|
99
|
|
|
'time' => $datetime[1], |
|
100
|
|
|
'location' => strip_tags($datetime[2]), |
|
101
|
|
|
'action' => strip_tags($value->find('.sroLbEvent')->plaintext) |
|
102
|
|
|
]; |
|
103
|
|
|
} |
|
104
|
|
|
return $return; |
|
105
|
|
|
} |
|
106
|
|
|
} |