1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace PeeHaa\AsyncTwitter\Api\Request\Media; |
4
|
|
|
|
5
|
|
|
use PeeHaa\AsyncTwitter\Api\Request\Media\Response\UploadResponse; |
6
|
|
|
use PeeHaa\AsyncTwitter\Exception; |
7
|
|
|
use PeeHaa\AsyncTwitter\Request\FieldParameter; |
8
|
|
|
use PeeHaa\AsyncTwitter\Request\FileParameter; |
9
|
|
|
|
10
|
|
|
class Upload extends BaseRequest |
11
|
|
|
{ |
12
|
|
|
const METHOD = 'POST'; |
13
|
|
|
const ENDPOINT = '/media/upload.json'; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
parent::__construct(self::METHOD, self::ENDPOINT); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function setAdditionalOwners(array $userIds) |
21
|
|
|
{ |
22
|
|
|
if (count($userIds) > 100) { |
23
|
|
|
throw new Exception('A maximum of 100 addition owners can be specified for a media resource'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
foreach ($userIds as $id) { |
27
|
|
|
if (!\ctype_digit((string)$id)) { |
28
|
|
|
throw new Exception('User IDs must be numeric'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->parameters['additional_owners'] = implode(',', $userIds); |
33
|
|
|
|
34
|
|
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getParameters(): array |
38
|
|
|
{ |
39
|
|
|
$parameters = [new FileParameter('media', $this->parameters['file_path'])]; |
40
|
|
|
|
41
|
|
|
if (isset($this->parameters['additional_owners'])) { |
42
|
|
|
$parameters[] = new FieldParameter('additional_owners', $this->parameters['additional_owners']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $parameters; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function handleResponse(array $responseData) |
49
|
|
|
{ |
50
|
|
|
$mediaId = $responseData['media_id_string'] ?? ''; |
|
|
|
|
51
|
|
|
$size = (int)($responseData['size'] ?? 0); |
|
|
|
|
52
|
|
|
$expiryTime = new \DateTimeImmutable('@' . (time() + ($responseData['expires_after_secs'] ?? 0))); |
53
|
|
|
$mimeType = $responseData['image']['image_type'] ?? ''; |
|
|
|
|
54
|
|
|
$width = (int)($responseData['image']['w'] ?? 0); |
|
|
|
|
55
|
|
|
$height = (int)($responseData['image']['h'] ?? 0); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return new UploadResponse($mediaId, $size, $expiryTime, $mimeType, $width, $height); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.