1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace unreal4u\InternalFunctionality; |
4
|
|
|
|
5
|
|
|
use unreal4u\Abstracts\TelegramMethods; |
6
|
|
|
|
7
|
|
|
class FormConstructor |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param TelegramMethods $method |
11
|
|
|
* @return mixed |
12
|
|
|
*/ |
13
|
|
|
private function constructFormData(TelegramMethods $method): array |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$result = $this->checkSpecialConditions($method); |
16
|
|
|
|
17
|
|
|
switch ($this->formType) { |
|
|
|
|
18
|
|
|
case 'application/x-www-form-urlencoded': |
19
|
|
|
$formData = [ |
20
|
|
|
'form_params' => get_object_vars($method), |
21
|
|
|
]; |
22
|
|
|
break; |
23
|
|
|
case 'multipart/form-data': |
24
|
|
|
$formData = $this->buildMultipartFormData(get_object_vars($method), $result['id'], $result['stream']); |
25
|
|
|
break; |
26
|
|
|
default: |
27
|
|
|
$formData = []; |
28
|
|
|
break; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $formData; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Can perform any special checks needed to be performed before sending the actual request to Telegram |
36
|
|
|
* |
37
|
|
|
* This will return an array with data that will be different in each case (for now). This can be changed in the |
38
|
|
|
* future. |
39
|
|
|
* |
40
|
|
|
* @param TelegramMethods $method |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
private function checkSpecialConditions(TelegramMethods $method): array |
44
|
|
|
{ |
45
|
|
|
$return = [false]; |
46
|
|
|
|
47
|
|
|
foreach ($method as $key => $value) { |
|
|
|
|
48
|
|
|
if (is_object($value)) { |
49
|
|
|
if (get_class($value) == 'unreal4u\\Telegram\\Types\\Custom\\InputFile') { |
50
|
|
|
// If we are about to send a file, we must use the multipart/form-data way |
51
|
|
|
$this->formType = 'multipart/form-data'; |
52
|
|
|
$return = [ |
53
|
|
|
'id' => $key, |
54
|
|
|
'stream' => $value->getStream(), |
55
|
|
|
]; |
56
|
|
|
} elseif (in_array('unreal4u\\InternalFunctionality\\KeyboardMethods', class_parents($value))) { |
57
|
|
|
// If we are about to send a KeyboardMethod, we must send a serialized object |
58
|
|
|
$method->$key = json_encode($value); |
59
|
|
|
$return = [true]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Builds up a multipart form-like array for Guzzle |
69
|
|
|
* |
70
|
|
|
* @param array $data The original object in array form |
71
|
|
|
* @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
72
|
|
|
* @param resource $stream The actual file handler |
73
|
|
|
* @return array Returns the actual formdata to be sent |
74
|
|
|
*/ |
75
|
|
|
private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
76
|
|
|
{ |
77
|
|
|
$formData = [ |
78
|
|
|
'multipart' => [], |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
foreach ($data as $id => $value) { |
82
|
|
|
// Always send as a string unless it's a file |
83
|
|
|
$multiPart = [ |
84
|
|
|
'name' => $id, |
85
|
|
|
'contents' => null, |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
if ($id === $fileKeyName) { |
89
|
|
|
$multiPart['contents'] = $stream; |
90
|
|
|
} else { |
91
|
|
|
$multiPart['contents'] = (string)$value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$formData['multipart'][] = $multiPart; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $formData; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|