1 | <?php |
||
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 |
||
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 |
||
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 | } |
||
106 |