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