AccountTransformer.php 1.5 KB

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