Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions src/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ public function follow(string $username): stdClass
* Returns the most recent statuses.
* https://dev.twitter.com/rest/reference/get/statuses/user_timeline
* @param int $flags timeline (ME | ME_AND_FRIENDS | REPLIES) and optional (RETWEETS)
* @param bool clickable return clickable text?
* @return stdClass[]
* @throws Exception
*/
public function load(int $flags = self::ME, int $count = 20, array $data = null): array
public function load(int $flags = self::ME, int $count = 20, array $data = null, $clickable = false): array
{
static $timelines = [
self::ME => 'user_timeline',
Expand All @@ -162,10 +163,12 @@ public function load(int $flags = self::ME, int $count = 20, array $data = null)
throw new \InvalidArgumentException;
}

return $this->cachedRequest('statuses/' . $timelines[$flags & 3], (array) $data + [
'count' => $count,
'include_rts' => $flags & self::RETWEETS ? 1 : 0,
]);
return $this->formatTweets(
$this->cachedRequest('statuses/' . $timelines[$flags & 3], (array) $data + [
'count' => $count,
'include_rts' => $flags & self::RETWEETS ? 1 : 0,
]),
$clickable);
}


Expand Down Expand Up @@ -247,14 +250,16 @@ public function get($id)

/**
* Returns tweets that match a specified query.
* https://dev.twitter.com/rest/reference/get/search/tweets
* @param string|array
* @throws Exception
* @param bool return complete response?
* @param bool clickable return clickable text?
* @return stdClass see https://dev.twitter.com/rest/reference/get/search/tweets
* @throws TwitterException
*/
public function search($query, bool $full = false): stdClass
public function search($query, $full = false, $clickable = false)
{
$res = $this->request('search/tweets', 'GET', is_array($query) ? $query : ['q' => $query]);
return $full ? $res : $res->statuses;
return $this->formatTweets($full ? $res : $res->statuses, $clickable);
}


Expand Down Expand Up @@ -413,6 +418,34 @@ public static function clickable(stdClass $status): string
}
return $s;
}


/**
* format tweets if extended mode is set or if clickable is called
* @param stdClass[] tweets
* @param bool clickable return clickable text?
* @return stdClass[]
*/
protected function formatTweets($tweets, $clickable = false)
{
// for full tweets, must add this to query: 'tweet_mode' => 'extended',
// this will replace text by full_text, need to copy it
if (!empty($tweets)) {
// look at each tweet
foreach ($tweets as &$tweet) {
// get full_text as text for compatibility
if (isset($tweet->full_text)) {
$tweet->text = $tweet->full_text;
}
// while we are here, make links clickable
if ($clickable) {
$tweet->text = $this->clickable($tweet);
}
}
}

return $tweets;
}
}


Expand Down