1 | <?php |
||
2 | |||
3 | namespace LeoCarmo\TelegramBot\Model; |
||
4 | |||
5 | use LeoCarmo\TelegramBot\Exceptions\RequiredParameterException; |
||
6 | use LeoCarmo\TelegramBot\Traits\Fillable; |
||
7 | use LeoCarmo\TelegramBot\Traits\Request; |
||
8 | |||
9 | abstract class Model |
||
10 | { |
||
11 | |||
12 | use Fillable; |
||
13 | |||
14 | use Request; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | |||
16 | /** |
||
17 | * @url https://core.telegram.org/bots/api#available-methods |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $method; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $required = []; |
||
26 | |||
27 | /** |
||
28 | * Fill with parameters |
||
29 | * |
||
30 | * @param array $data |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function fill(array $data = []) |
||
34 | { |
||
35 | $this->fillData($data); |
||
36 | return $this; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Send action/request |
||
41 | * |
||
42 | * @return array |
||
43 | * @throws RequiredParameterException |
||
44 | */ |
||
45 | public function send() |
||
46 | { |
||
47 | $this->checkRequired(); |
||
48 | |||
49 | return $this->sendRequest( |
||
50 | $this->collectParameters() |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return array |
||
56 | */ |
||
57 | private function collectParameters() |
||
58 | { |
||
59 | $filtered = array_filter(get_object_vars($this)); |
||
60 | |||
61 | return array_diff_key($filtered, get_class_vars(self::class)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @throws RequiredParameterException |
||
66 | */ |
||
67 | private function checkRequired() |
||
68 | { |
||
69 | if (count($this->required)) { |
||
70 | |||
71 | foreach ($this->required as $required) { |
||
72 | |||
73 | if (! $this->$required) { |
||
74 | throw new RequiredParameterException($required); |
||
75 | } |
||
76 | |||
77 | } |
||
78 | |||
79 | } |
||
80 | } |
||
81 | |||
82 | } |