-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCanal.php
69 lines (64 loc) · 1.68 KB
/
Canal.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
<?php
/**
* The common usages of canal.
*
* @author wgrape <https://github.com/WGrape>
* @license https://github.com/WGrape/esupdater/blob/master/LICENSE MIT Licence
*/
namespace framework;
class Canal
{
/**
* Encode the canal data to string.
*
* @param string $unEncodedCanalData the json format data in kafka queue (canal put it into kafka)
*
* @return string
*/
public function encode(string $unEncodedCanalData): string
{
return urlencode($unEncodedCanalData);
}
/**
* Decode the canal data to string.
*
* @param string $encodedCanalData the urlencoded canal data
*
* @return string
*/
public function decode(string $encodedCanalData): string
{
return urldecode($encodedCanalData);
}
/**
* Parse the canal data to array.
*
* @param string $encodedCanalData the urlencoded canal data
*
* @return array
*/
public function parse(string $encodedCanalData): array
{
return json_decode($this->decode($encodedCanalData), true);
}
/**
* Check the canal data format.
*
* @param array $parsedCanalData
*
* @return bool
*/
public function checkParsedCanalData(array $parsedCanalData): bool
{
if (empty($parsedCanalData) || empty($parsedCanalData['data'])) {
return false;
}
if (!isset($parsedCanalData['database']) || !isset($parsedCanalData['table']) || !isset($parsedCanalData['type'])) {
return false;
}
if (!isset($parsedCanalData['id']) || !isset($parsedCanalData['ts'])) {
return false;
}
return true;
}
}