1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DingRobot\Message; |
4
|
|
|
|
5
|
|
|
use DingRobot\Contract\RequesterContract; |
6
|
|
|
use DingRobot\Message\Traits\At; |
7
|
|
|
use DingRobot\Requester\CurlRequester; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Base Class |
11
|
|
|
* |
12
|
|
|
* @package DingRobot\Message |
13
|
|
|
*/ |
14
|
|
|
abstract class Base implements \JsonSerializable |
15
|
|
|
{ |
16
|
|
|
use At; |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $bodyName; |
21
|
|
|
protected $at; |
22
|
|
|
protected $body; |
23
|
|
|
|
24
|
13 |
|
public function __construct() |
25
|
|
|
{ |
26
|
13 |
|
} |
27
|
|
|
|
28
|
9 |
|
public function jsonSerialize() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
9 |
|
'msgtype' => $this->bodyName, |
32
|
9 |
|
$this->bodyName => $this->getBody(), |
33
|
8 |
|
] + $this->getAt() ?: []; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
abstract protected function getBody(); |
37
|
|
|
|
38
|
|
|
abstract protected function bodyFields(); |
39
|
|
|
|
40
|
10 |
|
protected function validate() |
41
|
|
|
{ |
42
|
10 |
|
if (empty($this->bodyFields())) { |
43
|
1 |
|
return; |
44
|
|
|
} |
45
|
9 |
|
foreach ($this->bodyFields() as $field => $config) { |
46
|
9 |
|
$content = isset($this->body[$field]) ? $this->body[$field] : null; |
47
|
9 |
|
if ($config['required'] && $content === null) { |
48
|
1 |
|
throw new \InvalidArgumentException("{$field} is required"); |
49
|
|
|
} |
50
|
9 |
|
if ($config['type'] == 'string' && $content && !is_string($content)) { |
51
|
1 |
|
throw new \InvalidArgumentException("{$field} must be string"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 发送 |
58
|
|
|
* send |
59
|
|
|
* |
60
|
|
|
* @param string $wen_hook 接口地址 |
61
|
|
|
* @param RequesterContract $requester |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
10 |
|
public function send($wen_hook = '', RequesterContract $requester = null) |
66
|
|
|
{ |
67
|
10 |
|
$wen_hook = $wen_hook? $wen_hook : d_web_hook(); |
68
|
|
|
|
69
|
10 |
|
if (!$wen_hook) { |
70
|
1 |
|
throw new \InvalidArgumentException("set web hook"); |
71
|
|
|
} |
72
|
|
|
|
73
|
10 |
|
$this->validate(); |
74
|
|
|
|
75
|
9 |
|
if ($requester) { |
76
|
9 |
|
return $requester->request($wen_hook, json_encode($this->jsonSerialize())); |
77
|
|
|
} else { |
78
|
|
|
return (new CurlRequester())->request($wen_hook, json_encode($this->jsonSerialize())); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
11 |
|
public function __call($name, $args) |
83
|
|
|
{ |
84
|
11 |
|
if (array_key_exists($name, $this->bodyFields())) { |
85
|
10 |
|
$this->body[$name] = isset($args[0]) ? $args[0] : ''; |
86
|
|
|
} else { |
87
|
1 |
|
throw new \BadMethodCallException("field {$name} not found"); |
88
|
|
|
} |
89
|
|
|
|
90
|
10 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public static function __callStatic($name, $args) |
94
|
|
|
{ |
95
|
1 |
|
return (new static)->$name(...$args); |
96
|
|
|
} |
97
|
|
|
} |