diff --git a/src/ApiV1/Traits/FormattingHelpers.php b/src/ApiV1/Traits/FormattingHelpers.php index f25c162..761591d 100644 --- a/src/ApiV1/Traits/FormattingHelpers.php +++ b/src/ApiV1/Traits/FormattingHelpers.php @@ -93,28 +93,83 @@ public function ago($timestamp): string } // todo redo these helpers + + /** + * @param object|array|string $user + * + * @return string + */ public function linkUser($user): string { - return 'https://twitter.com/' . (is_object($user) ? $user->screen_name : $user); + $screenName = is_string($user) ? $user : $this->objectToArray($user)['screen_name']; + + return 'https://twitter.com/' . $screenName; } + /** + * @param object|array $tweet + * + * @return string + */ public function linkTweet($tweet): string { - return $this->linkUser($tweet->user) . '/status/' . $tweet->id_str; + $tweet = $this->objectToArray($tweet); + + return $this->linkUser($tweet['user']) . '/status/' . $tweet['id_str']; } + /** + * @param object|array $tweet + * + * @return string + */ public function linkRetweet($tweet): string { - return 'https://twitter.com/intent/retweet?tweet_id=' . $tweet->id_str; + $tweet = $this->objectToArray($tweet); + + return 'https://twitter.com/intent/retweet?tweet_id=' . $tweet['id_str']; } + /** + * @param object|array $tweet + * + * @return string + */ public function linkAddTweetToFavorites($tweet): string { - return 'https://twitter.com/intent/favorite?tweet_id=' . $tweet->id_str; + $tweet = $this->objectToArray($tweet); + + return 'https://twitter.com/intent/favorite?tweet_id=' . $tweet['id_str']; } + /** + * @param object|array $tweet + * + * @return string + */ public function linkReply($tweet): string { - return 'https://twitter.com/intent/tweet?in_reply_to=' . $tweet->id_str; + $tweet = $this->objectToArray($tweet); + + return 'https://twitter.com/intent/tweet?in_reply_to=' . $tweet['id_str']; + } + + /** + * @param $data + * + * @return array|mixed + */ + protected function objectToArray($data) + { + if (is_array($data)) { + return $data; + } + + if (is_object($data)) { + return json_decode(json_encode($data), true); + } + + // Fallback for non objects + return $data; } } diff --git a/tests/Unit/ApiV1/Traits/ConcernTestCase.php b/tests/Unit/ApiV1/Traits/ConcernTestCase.php new file mode 100644 index 0000000..09d4efd --- /dev/null +++ b/tests/Unit/ApiV1/Traits/ConcernTestCase.php @@ -0,0 +1,26 @@ +subject = $this->getMockForTrait($this->getTraitName()); + } + + abstract protected function getTraitName(): string; +} diff --git a/tests/Unit/ApiV1/Traits/FormattingHelpersTest.php b/tests/Unit/ApiV1/Traits/FormattingHelpersTest.php new file mode 100644 index 0000000..8b869f0 --- /dev/null +++ b/tests/Unit/ApiV1/Traits/FormattingHelpersTest.php @@ -0,0 +1,57 @@ +assertSame( + 'https://twitter.com/atymic', + $this->subject->linkUser($user) + ); + } + + public function dataGetUserLink(): array + { + return [ + 'string' => ['atymic'], + 'object' => [(object) ['screen_name' => 'atymic']], + 'array' => [['screen_name' => 'atymic']], + ]; + } + + /** + * @dataProvider dataLinkAddTweetToFavorites + */ + public function testLinkAddTweetToFavorites($tweet): void + { + $this->assertSame( + 'https://twitter.com/intent/favorite?tweet_id=1381031025053155332', + $this->subject->linkAddTweetToFavorites($tweet) + ); + } + + public function dataLinkAddTweetToFavorites(): array + { + return [ + 'object' => [(object) ['id_str' => '1381031025053155332']], + 'array' => [['id_str' => '1381031025053155332']], + ]; + } + + protected function getTraitName(): string + { + return FormattingHelpers::class; + } +}