2018-11-08 11:36:42 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2020-12-10 17:06:50 -05:00
|
|
|
* PHP version 7.4+
|
2018-11-08 11:36:42 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2018-11-08 11:36:42 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2018-11-08 11:36:42 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
2020-08-26 15:22:14 -04:00
|
|
|
use Aviat\AnimeClient\Kitsu;
|
2018-11-08 11:36:42 -05:00
|
|
|
use function Aviat\AnimeClient\getLocalImg;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\Types\User;
|
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform user profile data for display
|
2019-12-09 16:17:25 -05:00
|
|
|
*
|
|
|
|
* @param array|object $profileData
|
|
|
|
* @return User
|
2018-11-08 11:36:42 -05:00
|
|
|
*/
|
|
|
|
final class UserTransformer extends AbstractTransformer {
|
2019-12-09 16:17:25 -05:00
|
|
|
public function transform($profileData): User
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2020-08-24 15:20:07 -04:00
|
|
|
$base = $profileData['data']['findProfileBySlug'] ?? [];
|
|
|
|
$favorites = $base['favorites']['nodes'] ?? [];
|
|
|
|
$stats = $base['stats'] ?? [];
|
|
|
|
$waifu = (array_key_exists('waifu', $base)) ? [
|
|
|
|
'label' => $base['waifuOrHusbando'],
|
|
|
|
'character' => $base['waifu'],
|
2020-07-30 15:16:24 -04:00
|
|
|
] : [];
|
2018-11-08 11:36:42 -05:00
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
return User::from([
|
2020-08-24 15:20:07 -04:00
|
|
|
'about' => $base['about'],
|
|
|
|
'avatar' => getLocalImg($base['avatarImage']['original']['url'], FALSE),
|
2018-11-08 11:36:42 -05:00
|
|
|
'favorites' => $this->organizeFavorites($favorites),
|
2020-08-24 15:20:07 -04:00
|
|
|
'location' => $base['location'],
|
|
|
|
'name' => $base['name'],
|
|
|
|
'slug' => $base['slug'],
|
|
|
|
'stats' => $this->organizeStats($stats),
|
2018-11-08 11:36:42 -05:00
|
|
|
'waifu' => $waifu,
|
2020-08-24 15:20:07 -04:00
|
|
|
'website' => $base['siteLinks']['nodes'][0]['url'],
|
2018-11-08 11:36:42 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reorganize favorites data to be more useful
|
|
|
|
*
|
|
|
|
* @param array $rawFavorites
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function organizeFavorites(array $rawFavorites): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach ($rawFavorites as $item)
|
|
|
|
{
|
2020-08-24 15:20:07 -04:00
|
|
|
$type = strtolower($item['item']['__typename']);
|
|
|
|
$output[$type][$item['id']] = $item['item'];
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:04 -05:00
|
|
|
private function organizeStats(array $stats, $data = []): array
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2018-12-07 10:22:16 -05:00
|
|
|
$animeStats = [];
|
|
|
|
$mangaStats = [];
|
|
|
|
$otherStats = [];
|
|
|
|
|
2020-08-24 15:20:07 -04:00
|
|
|
if (array_key_exists('animeAmountConsumed', $stats))
|
2018-12-07 10:22:16 -05:00
|
|
|
{
|
|
|
|
$animeStats = [
|
2020-08-24 15:20:07 -04:00
|
|
|
'Time spent watching anime:' => Kitsu::friendlyTime($stats['animeAmountConsumed']['time']),
|
|
|
|
'Anime series watched:' => number_format($stats['animeAmountConsumed']['media']),
|
|
|
|
'Anime episodes watched:' => number_format($stats['animeAmountConsumed']['units']),
|
2018-12-07 10:22:16 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:20:07 -04:00
|
|
|
if (array_key_exists('mangaAmountConsumed', $stats))
|
2018-12-07 10:22:16 -05:00
|
|
|
{
|
|
|
|
$mangaStats = [
|
2020-08-24 15:20:07 -04:00
|
|
|
'Manga series read:' => number_format($stats['mangaAmountConsumed']['media']),
|
|
|
|
'Manga chapters read:' => number_format($stats['mangaAmountConsumed']['units']),
|
2018-12-07 10:22:16 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty($data))
|
|
|
|
{
|
|
|
|
$otherStats = [
|
|
|
|
'Posts:' => number_format($data['postsCount']),
|
|
|
|
'Comments:' => number_format($data['commentsCount']),
|
|
|
|
'Media Rated:' => number_format($data['ratingsCount']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge($animeStats, $mangaStats, $otherStats);
|
2018-11-08 11:36:42 -05:00
|
|
|
}
|
|
|
|
}
|