AccountTransformer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Transformer\Api;
  3. use Auth;
  4. use App\Profile;
  5. use League\Fractal;
  6. class AccountTransformer extends Fractal\TransformerAbstract
  7. {
  8. protected $defaultIncludes = [
  9. // 'relationship',
  10. ];
  11. public function transform(Profile $profile)
  12. {
  13. $local = $profile->domain == null;
  14. $is_admin = !$local ? false : $profile->user->is_admin;
  15. $acct = $local ? $profile->username : substr($profile->username, 1);
  16. $username = $local ? $profile->username : explode('@', $acct)[0];
  17. return [
  18. 'id' => (string) $profile->id,
  19. 'username' => $username,
  20. 'acct' => $acct,
  21. 'display_name' => $profile->name,
  22. 'locked' => (bool) $profile->is_private,
  23. 'followers_count' => $profile->followerCount(),
  24. 'following_count' => $profile->followingCount(),
  25. 'statuses_count' => (int) $profile->statusCount(),
  26. 'note' => $profile->bio ?? '',
  27. 'url' => $profile->url(),
  28. 'avatar' => $profile->avatarUrl(),
  29. 'website' => $profile->website,
  30. 'local' => (bool) $local,
  31. 'is_admin' => (bool) $is_admin,
  32. 'created_at' => $profile->created_at->toJSON(),
  33. 'header_bg' => $profile->header_bg,
  34. 'last_fetched_at' => optional($profile->last_fetched_at)->toJSON()
  35. ];
  36. }
  37. protected function includeRelationship(Profile $profile)
  38. {
  39. return $this->item($profile, new RelationshipTransformer());
  40. }
  41. }