|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jellybool\MikeCRMEmailParser; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Parser |
|
7
|
|
|
* @package Jellybool\MikecrmEmailParse |
|
8
|
|
|
*/ |
|
9
|
|
|
class Parser { |
|
10
|
|
|
/** |
|
11
|
|
|
* @var mixed |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $body; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $rules; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Parser constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($rules = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->body = json_decode(file_get_contents('php://input'), true); |
|
25
|
|
|
$this->rules = count($rules) > 0 ? $rules : []; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* return all html from email. Just in case. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function html() |
|
34
|
|
|
{ |
|
35
|
|
|
return isset($this->body['html']) ? $this->body['html'] : ''; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* return all text from email. You could do anything with this text. |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
|
|
public function text() |
|
44
|
|
|
{ |
|
45
|
|
|
return isset($this->body['text']) ? $this->body['text'] : ''; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* return basic order information from mikecrm email. |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function order() |
|
54
|
|
|
{ |
|
55
|
|
|
$mikeRule = isset($this->rules['mike']) ? $this->rules['mike'] : '/麦客订单号:IFP\-CN091\-\d{8,30}\-\d+/'; |
|
56
|
|
|
$platformRule = isset($this->rules['platform']) ? $this->rules['platform'] : '/支付平台交易号:\d{4,16}/'; |
|
57
|
|
|
$tradeRule = isset($this->rules['trade']) ? $this->rules['trade'] : '/订单号\n\d{8,36}/'; |
|
58
|
|
|
|
|
59
|
|
|
return [ |
|
60
|
|
|
'mike_no' => $this->getMatchText($mikeRule), |
|
61
|
|
|
'platform_no' => $this->getMatchText($platformRule), |
|
62
|
|
|
'trade_no' => $this->getMatchText($tradeRule), |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function verify() |
|
70
|
|
|
{ |
|
71
|
|
|
return isset($this->body['from']) && $this->body['from'] == '[email protected]'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param $rule |
|
76
|
|
|
* @return string|string[]|null |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getMatchText($rule) |
|
79
|
|
|
{ |
|
80
|
|
|
preg_match_all($rule, $this->text(), $matches); |
|
81
|
|
|
if (count($matches[0]) > 0) { |
|
82
|
|
|
return preg_replace('/([\n\x80-\xff]*)/i', '', $matches[0][0]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return ''; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|