| 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 | 11 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 11 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 8 |  |     public function jsonSerialize() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 8 |  |             'msgtype'       => $this->bodyName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |             $this->bodyName => $this->getBody(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 7 |  |         ] + $this->getAt() ?: []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     abstract protected function getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     abstract protected function bodyFields(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 9 |  |     protected function validate() | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 9 |  |         if (empty($this->bodyFields())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 8 |  |         foreach ($this->bodyFields() as $field => $config) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 8 |  |             $content  = isset($this->body[$field]) ? $this->body[$field] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 8 |  |             $required = isset($config['required']) ? $config['required'] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 8 |  |             $type     = isset($config['type']) ? $config['type'] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 8 |  |             if ($required && !$content) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |                 throw new \InvalidArgumentException("{$field} is required"); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 8 |  |             if ($type == 'string' && $content && !is_string($content)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |                 throw new \InvalidArgumentException("{$field} must be string"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 7 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * 发送 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string            $address 接口地址 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param RequesterContract $requester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 9 |  |     public function send($address = '', RequesterContract $requester = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |         $this->validate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 8 |  |         if ($requester) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 8 |  |             return $requester->request($address, json_encode($this->jsonSerialize())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return (new CurlRequester())->request($address, json_encode($this->jsonSerialize())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  |     public function __call($name, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 9 |  |         if (array_key_exists($name, $this->bodyFields())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 8 |  |             $this->body[$name] = isset($args[0]) ? $args[0] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             throw new \BadMethodCallException("field {$name} not found"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 8 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     public static function __callStatic($name, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         return (new static)->$name(...$args); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  | } |