Adapter.php
1 month ago
CtfOauthConnect.php
1 month ago
MediaAdapter.php
1 month ago
TweetAdapter.php
1 month ago
TwitterHelperApi.php
1 month ago
UserAdapter.php
1 month ago
MediaAdapter.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TwitterFeed\V2; |
| 4 | |
| 5 | /** |
| 6 | * Class for converting media attribute from Twitter API v1.1 to v2. |
| 7 | * |
| 8 | * @package twitter-api-v2 |
| 9 | */ |
| 10 | class MediaAdapter |
| 11 | { |
| 12 | /** |
| 13 | * Tweet data. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $tweet; |
| 18 | |
| 19 | /** |
| 20 | * Included media. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $included_media; |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | * |
| 29 | * @param array $tweet Tweet data. |
| 30 | * @param array $included_media Included media data. |
| 31 | */ |
| 32 | public function __construct($tweet, $included_media) |
| 33 | { |
| 34 | $this->tweet = $tweet; |
| 35 | $this->included_media = $included_media; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get media keys. |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function getMediaKeys() |
| 44 | { |
| 45 | return ! empty($this->tweet['attachments']['media_keys']) ? $this->tweet['attachments']['media_keys'] : []; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Convert. |
| 50 | * |
| 51 | * @return array |
| 52 | */ |
| 53 | public function convert() |
| 54 | { |
| 55 | $media_array = []; |
| 56 | |
| 57 | foreach ($this->getMediaKeys() as $media_key_index => $media_key_value) { |
| 58 | $media_index = array_search( |
| 59 | $this->tweet['attachments']['media_keys'][ $media_key_index ], |
| 60 | array_column($this->included_media, 'media_key') |
| 61 | ); |
| 62 | |
| 63 | if ($media_index !== false) { |
| 64 | $media = [ |
| 65 | 'media_url_https' => !empty($this->included_media[ $media_index ]['url']) |
| 66 | ? $this->included_media[ $media_index ]['url'] |
| 67 | : $this->included_media[ $media_index ]['preview_image_url'], |
| 68 | 'type' => $this->included_media[ $media_index ]['type'], |
| 69 | ]; |
| 70 | |
| 71 | if (isset($new_media['type']) && $new_media['type'] === 'video') { |
| 72 | $media['video_info'] = [ |
| 73 | 'variants' => $this->included_media[ $media_index ]['variants'], |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | $media_array[] = $media; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $media_array; |
| 82 | } |
| 83 | } |
| 84 |