|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\InternalFunctionality; |
|
6
|
|
|
|
|
7
|
|
|
use MultipartBuilder\Builder; |
|
8
|
|
|
use MultipartBuilder\MultipartData; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use unreal4u\TelegramAPI\Abstracts\TelegramMethods; |
|
11
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; |
|
12
|
|
|
|
|
13
|
|
|
class PostOptionsConstructor |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* With this flag we'll know what type of request to send to Telegram |
|
17
|
|
|
* |
|
18
|
|
|
* 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
|
19
|
|
|
* 'multipart/form-data' should be used only when you upload documents, photos, etc. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $formType = 'application/x-www-form-urlencoded'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $logger; |
|
29
|
|
|
|
|
30
|
36 |
|
public function __construct(LoggerInterface $logger = null) |
|
31
|
|
|
{ |
|
32
|
36 |
|
if ($logger === null) { |
|
33
|
36 |
|
$logger = new DummyLogger(); |
|
34
|
|
|
} |
|
35
|
36 |
|
$this->logger = $logger; |
|
36
|
36 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Builds up the form elements to be sent to Telegram |
|
40
|
|
|
* |
|
41
|
|
|
* @TODO Move this to apart function |
|
42
|
|
|
* |
|
43
|
|
|
* @param TelegramMethods $method |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws \unreal4u\TelegramAPI\Exceptions\MissingMandatoryField |
|
46
|
|
|
*/ |
|
47
|
27 |
|
public function constructOptions(TelegramMethods $method): array |
|
48
|
|
|
{ |
|
49
|
27 |
|
$result = $this->checkIsMultipart($method); |
|
50
|
|
|
|
|
51
|
27 |
|
if (!empty($result)) { |
|
52
|
|
|
return $this->constructMultipartOptions( |
|
53
|
|
|
$method->export(), |
|
54
|
|
|
$result['id'], |
|
55
|
|
|
$result['stream'], |
|
56
|
|
|
$result['filename'] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
27 |
|
$body = http_build_query($method->export(), '', '&'); |
|
61
|
|
|
|
|
62
|
|
|
return [ |
|
63
|
|
|
'headers' => [ |
|
64
|
25 |
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
65
|
25 |
|
'Content-Length' => strlen($body), |
|
66
|
25 |
|
'User-Agent: PHP7+ Bot API', |
|
67
|
|
|
], |
|
68
|
25 |
|
'body' => $body |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check if the given TelegramMethod should be handled as a multipart. |
|
74
|
|
|
* |
|
75
|
|
|
* @param TelegramMethods $method |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
27 |
|
private function checkIsMultipart(TelegramMethods $method): array |
|
79
|
|
|
{ |
|
80
|
27 |
|
$this->logger->debug('Checking whether to apply special conditions to this request'); |
|
81
|
27 |
|
$method->performSpecialConditions(); |
|
82
|
|
|
|
|
83
|
27 |
|
$return = []; |
|
84
|
|
|
|
|
85
|
27 |
|
foreach ($method as $key => $value) { |
|
|
|
|
|
|
86
|
23 |
|
if (is_object($value) && $value instanceof InputFile) { |
|
87
|
|
|
$this->logger->debug('About to send a file, so changing request to use multi-part instead'); |
|
88
|
|
|
// If we are about to send a file, we must use the multipart/form-data way |
|
89
|
|
|
$this->formType = 'multipart/form-data'; |
|
90
|
|
|
$return = [ |
|
91
|
|
|
'id' => $key, |
|
92
|
|
|
'filename' => $value->path, |
|
93
|
23 |
|
'stream' => $value->getStream() |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
27 |
|
return $return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Builds up a multipart form-like array for Guzzle |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $data The original object in array form |
|
105
|
|
|
* @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
|
106
|
|
|
* @param resource $stream The actual file handler |
|
107
|
|
|
* @param string $filename |
|
108
|
|
|
* @return array Returns the actual formdata to be sent |
|
109
|
|
|
*/ |
|
110
|
|
|
public function constructMultipartOptions(array $data, string $fileKeyName, $stream, string $filename): array |
|
111
|
|
|
{ |
|
112
|
|
|
$builder = new Builder(); |
|
113
|
|
|
$this->logger->debug('Creating multi-part form array data (complex and expensive)'); |
|
114
|
|
|
|
|
115
|
|
|
foreach ($data as $id => $value) { |
|
|
|
|
|
|
116
|
|
|
if ($id === $fileKeyName) { |
|
117
|
|
|
$data = new MultipartData($id, stream_get_contents($stream), pathinfo($filename, PATHINFO_BASENAME)); |
|
118
|
|
|
} else { |
|
119
|
|
|
$data = new MultipartData($id, $value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$builder->append($data); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$body = $builder->buildAll(); |
|
126
|
|
|
$array = [ |
|
127
|
|
|
'headers' => [ |
|
128
|
|
|
'Content-Type' => 'multipart/form-data; boundary="' . $builder->getBoundary() . '"', |
|
129
|
|
|
'Content-Length' => strlen($body) |
|
130
|
|
|
], |
|
131
|
|
|
'body' => $body |
|
132
|
|
|
]; |
|
133
|
|
|
return $array; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|