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
TweetAdapter.php
73 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class for converting tweet sub attribute from Twitter API v1.1 to v2. |
| 5 | * |
| 6 | * @package twitter-api-v2 |
| 7 | */ |
| 8 | |
| 9 | namespace TwitterFeed\V2; |
| 10 | |
| 11 | // Don't load directly. |
| 12 | if (! defined('ABSPATH')) { |
| 13 | die('-1'); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * TweetAdapter class. |
| 18 | */ |
| 19 | class TweetAdapter extends Adapter { |
| 20 | /** |
| 21 | * Get mapped fields. |
| 22 | * |
| 23 | * @return string[] |
| 24 | */ |
| 25 | public function getMappedFields() |
| 26 | { |
| 27 | return [ |
| 28 | 'id_str' => 'id', |
| 29 | 'conversation_id' => 'conversation_id_str', |
| 30 | ]; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Remove retweet prefix. |
| 35 | * |
| 36 | * @param string $text Tweet text. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function removeRetweetPrefix($text) |
| 41 | { |
| 42 | if (strpos($text, 'RT') !== 0) { |
| 43 | return $text; |
| 44 | } |
| 45 | |
| 46 | preg_match('/^RT\s*@[^:]*:(.*)/s', $text, $matches); |
| 47 | |
| 48 | if (! isset($matches[1])) { |
| 49 | return $text; |
| 50 | } |
| 51 | |
| 52 | return $matches[1]; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Convert entity from v1.1 to v2 format. |
| 57 | * |
| 58 | * @param array $entity Entity. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | public function convert($entity) |
| 63 | { |
| 64 | $entity['text'] = $this->removeRetweetPrefix($entity['text']); |
| 65 | |
| 66 | if (isset($entity['retweeted_status']['text'])) { |
| 67 | $entity['retweeted_status']['text'] = $this->removeRetweetPrefix($entity['retweeted_status']['text']); |
| 68 | } |
| 69 | |
| 70 | return parent::convert($entity); |
| 71 | } |
| 72 | } |
| 73 |