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
|
|
|
/** |
11
|
|
|
* @link https://dev.twitter.com/rest/reference/post/media/upload-append |
12
|
|
|
*/ |
13
|
|
|
class Upload extends BaseRequest |
14
|
|
|
{ |
15
|
|
|
const METHOD = 'POST'; |
16
|
|
|
const ENDPOINT = '/media/upload.json'; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
parent::__construct(self::METHOD, self::ENDPOINT); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setAdditionalOwners(array $userIds) |
24
|
|
|
{ |
25
|
|
|
if (count($userIds) > 100) { |
26
|
|
|
throw new Exception('A maximum of 100 addition owners can be specified for a media resource'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
foreach ($userIds as $id) { |
30
|
|
|
if (!\ctype_digit((string)$id)) { |
31
|
|
|
throw new Exception('User IDs must be numeric'); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->parameters['additional_owners'] = implode(',', $userIds); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getParameters(): array |
41
|
|
|
{ |
42
|
|
|
$parameters = [new FileParameter('media', $this->parameters['file_path'])]; |
43
|
|
|
|
44
|
|
|
if (isset($this->parameters['additional_owners'])) { |
45
|
|
|
$parameters[] = new FieldParameter('additional_owners', $this->parameters['additional_owners']); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $parameters; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function handleResponse(array $responseData) |
52
|
|
|
{ |
53
|
|
|
$mediaId = $responseData['media_id_string'] ?? ''; |
|
|
|
|
54
|
|
|
$size = (int)($responseData['size'] ?? 0); |
|
|
|
|
55
|
|
|
$expiryTime = new \DateTimeImmutable('@' . (time() + ($responseData['expires_after_secs'] ?? 0))); |
56
|
|
|
$mimeType = $responseData['image']['image_type'] ?? ''; |
|
|
|
|
57
|
|
|
$width = (int)($responseData['image']['w'] ?? 0); |
|
|
|
|
58
|
|
|
$height = (int)($responseData['image']['h'] ?? 0); |
|
|
|
|
59
|
|
|
|
60
|
|
|
return new UploadResponse($mediaId, $size, $expiryTime, $mimeType, $width, $height); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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.