Packet.php
2.8 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
95
96
97
98
99
<?php
namespace Lackoxygen\GzCbec\Request;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Lackoxygen\GzCbec\Constant\XML;
use Lackoxygen\GzCbec\Contract\RequestInterface;
use Lackoxygen\GzCbec\Contract\XMLInterface;
use Lackoxygen\GzCbec\Exception\Exception;
use Lackoxygen\GzCbec\Request\Head\Head;
use Lackoxygen\GzCbec\Request\Packet\GzReportTransfer;
use Lackoxygen\GzCbec\Request\Report\Node\Receiver;
use Lackoxygen\GzCbec\Utils\RSA;
use Lackoxygen\GzCbec\Utils\Signature;
class Packet
{
/**
* @var RequestInterface $request
*/
protected $request;
/**
* @var array
*/
protected $config = [];
/**
* Packet constructor.
*
* @param RequestInterface $request
*/
public function __construct(RequestInterface $request)
{
$this->request = $request;
$this->config = config('gz-cbec');
}
/**
* @return string
*/
public function getContent(): string
{
try {
$head = new Head();
$head->setMessageId($this->request->getGuid());
$head->setMessageType($this->request->getMessageType());
$head->setSender($this->config['sender_sn']);
$receiver = new Receiver();
$receiver->setReceiver($this->config['receiver_sn']);
$head->pushReceivers($receiver);
$head->setSendTime(Carbon::now()->getPreciseTimestamp(4));
$head->setVersion($this->request->getVersion());
$head->setFileName('');
$head->setZipFlag(0);
$packet = new GzReportTransfer();
$packet->setHead($head);
if ($this->request instanceof XMLInterface) {
$firstLabel = $this->request->getFirstLabelPrefix() . ':' . $this->request->getFirstLabelName();
$xmlHead = sprintf(XML::DATA, $firstLabel, $this->request->getGuid(), $this->request->getVersion(),
$firstLabel);
$dataXml = $this->request->toXML($xmlHead);
file_put_contents('test.xml', $dataXml);
$data = base64_encode($dataXml);
$packet->setData($data);
}
$xml = $packet->toXML(XML::TRANSFER);
return $this->signature($xml);
} catch (Exception $exception) {
return '';
}
}
/**
* @param $signXml
*
* @return string
*/
protected function signature(string $signXml): string
{
$signature = Arr::get($this->config, 'signature');
if ($signature instanceof \Closure) {
$afterSignXml = call_user_func($signature, $signXml);
if (!is_null($afterSignXml)) {
return $afterSignXml;
}
} else {
return (new Signature($signature))($signXml);
}
return "";
}
}