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
|
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2018-11-08 11:36:42 -05:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2019-12-06 09:16:35 -05:00
|
|
|
* @copyright 2015 - 2019 Timothy J. Warren
|
2018-11-08 11:36:42 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2018-11-08 11:36:42 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
|
|
|
use function Aviat\AnimeClient\getLocalImg;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\API\JsonAPI;
|
|
|
|
use Aviat\AnimeClient\Types\User;
|
|
|
|
use Aviat\Ion\Transformer\AbstractTransformer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform user profile data for display
|
|
|
|
*/
|
|
|
|
final class UserTransformer extends AbstractTransformer {
|
|
|
|
public function transform($profileData): User
|
|
|
|
{
|
|
|
|
$orgData = JsonAPI::organizeData($profileData)[0];
|
|
|
|
$attributes = $orgData['attributes'];
|
|
|
|
|
|
|
|
$rels = $orgData['relationships'] ?? [];
|
|
|
|
$favorites = array_key_exists('favorites', $rels) ? $rels['favorites'] : [];
|
|
|
|
|
|
|
|
$stats = [];
|
|
|
|
foreach ($rels['stats'] as $sid => &$item)
|
|
|
|
{
|
|
|
|
$key = $item['attributes']['kind'];
|
|
|
|
$stats[$key] = $item['attributes']['statsData'];
|
|
|
|
unset($item);
|
|
|
|
}
|
2019-12-09 14:34:23 -05:00
|
|
|
unset($item);
|
2018-11-08 11:36:42 -05:00
|
|
|
|
|
|
|
$waifu = [];
|
|
|
|
if (array_key_exists('waifu', $rels))
|
|
|
|
{
|
|
|
|
$waifu = [
|
|
|
|
'label' => $attributes['waifuOrHusbando'],
|
|
|
|
'character' => $rels['waifu']['attributes'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new User([
|
|
|
|
'about' => $attributes['about'],
|
|
|
|
'avatar' => getLocalImg($attributes['avatar']['original'], FALSE),
|
|
|
|
'favorites' => $this->organizeFavorites($favorites),
|
|
|
|
'location' => $attributes['location'],
|
|
|
|
'name' => $attributes['name'],
|
|
|
|
'slug' => $attributes['slug'],
|
|
|
|
'stats' => $this->organizeStats($stats, $attributes),
|
|
|
|
'waifu' => $waifu,
|
|
|
|
'website' => $attributes['website'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reorganize favorites data to be more useful
|
|
|
|
*
|
|
|
|
* @param array $rawFavorites
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function organizeFavorites(array $rawFavorites): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
unset($rawFavorites['data']);
|
|
|
|
|
|
|
|
foreach ($rawFavorites as $item)
|
|
|
|
{
|
|
|
|
$rank = $item['attributes']['favRank'];
|
|
|
|
foreach ($item['relationships']['item'] as $key => $fav)
|
|
|
|
{
|
|
|
|
$output[$key] = $output[$key] ?? [];
|
|
|
|
foreach ($fav as $id => $data)
|
|
|
|
{
|
|
|
|
$output[$key][$rank] = array_merge(['id' => $id], $data['attributes']);
|
|
|
|
}
|
|
|
|
|
|
|
|
ksort($output[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the time spent on anime in a more readable format
|
|
|
|
*
|
2018-12-06 16:21:02 -05:00
|
|
|
* @param int $seconds
|
2018-11-08 11:36:42 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-12-06 16:21:02 -05:00
|
|
|
private function formatAnimeTime(int $seconds): string
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2018-12-06 16:21:02 -05:00
|
|
|
// All the seconds left
|
|
|
|
$remSeconds = $seconds % 60;
|
|
|
|
$minutes = ($seconds - $remSeconds) / 60;
|
|
|
|
|
2018-11-08 11:36:42 -05:00
|
|
|
$minutesPerDay = 1440;
|
|
|
|
$minutesPerYear = $minutesPerDay * 365;
|
|
|
|
|
|
|
|
// Minutes short of a year
|
|
|
|
$years = (int)floor($minutes / $minutesPerYear);
|
|
|
|
$minutes %= $minutesPerYear;
|
|
|
|
|
|
|
|
// Minutes short of a day
|
|
|
|
$extraMinutes = $minutes % $minutesPerDay;
|
|
|
|
$days = ($minutes - $extraMinutes) / $minutesPerDay;
|
|
|
|
|
|
|
|
// Minutes short of an hour
|
|
|
|
$remMinutes = $extraMinutes % 60;
|
|
|
|
$hours = ($extraMinutes - $remMinutes) / 60;
|
|
|
|
|
2018-12-06 16:21:02 -05:00
|
|
|
$output = "{$days} days, {$hours} hours, {$remMinutes} minutes, and {$remSeconds} seconds.";
|
2018-11-08 11:36:42 -05:00
|
|
|
|
|
|
|
if ($years > 0)
|
|
|
|
{
|
|
|
|
$output = "{$years} year(s),{$output}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2018-12-07 10:22:16 -05:00
|
|
|
private function organizeStats($stats, $data = []): array
|
2018-11-08 11:36:42 -05:00
|
|
|
{
|
2018-12-07 10:22:16 -05:00
|
|
|
$animeStats = [];
|
|
|
|
$mangaStats = [];
|
|
|
|
$otherStats = [];
|
|
|
|
|
|
|
|
if (array_key_exists('anime-amount-consumed', $stats))
|
|
|
|
{
|
|
|
|
$animeStats = [
|
|
|
|
'Time spent watching anime:' => $this->formatAnimeTime($stats['anime-amount-consumed']['time']),
|
|
|
|
'Anime series watched:' => number_format($stats['anime-amount-consumed']['media']),
|
|
|
|
'Anime episodes watched:' => number_format($stats['anime-amount-consumed']['units']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('manga-amount-consumed', $stats))
|
|
|
|
{
|
|
|
|
$mangaStats = [
|
|
|
|
'Manga series read:' => number_format($stats['manga-amount-consumed']['media']),
|
|
|
|
'Manga chapters read:' => number_format($stats['manga-amount-consumed']['units']),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|