All in GraphQL #34

Merged
timw4mail merged 87 commits from develop into master 2020-12-01 10:07:49 -05:00
5 changed files with 150 additions and 53 deletions
Showing only changes of commit 09734515ca - Show all commits

View File

@ -43,15 +43,14 @@ use Aviat\AnimeClient\API\Kitsu;
<article class="media">
<?php
$link = $url->generate('anime.details', ['id' => $anime['slug']]);
$titles = Kitsu::getTitles($anime['titles']);
?>
<a href="<?= $link ?>">
<?= $helper->picture("images/anime/{$anime['id']}.webp") ?>
</a>
<div class="name">
<a href="<?= $link ?>">
<?= $anime['titles']['canonical'] ?>
<?php foreach ($titles as $title): ?>
<?= $anime['title'] ?>
<?php foreach ($anime['titles'] as $title): ?>
<br />
<small><?= $title ?></small>
<?php endforeach ?>
@ -71,15 +70,14 @@ use Aviat\AnimeClient\API\Kitsu;
<article class="media">
<?php
$link = $url->generate('manga.details', ['id' => $manga['slug']]);
$titles = Kitsu::getTitles($manga['titles']);
?>
<a href="<?= $link ?>">
<?= $helper->picture("images/manga/{$manga['id']}.webp") ?>
</a>
<div class="name">
<a href="<?= $link ?>">
<?= $manga['titles']['canonical'] ?>
<?php foreach ($titles as $title): ?>
<?= $manga['title'] ?>
<?php foreach ($manga['titles'] as $title): ?>
<br />
<small><?= $title ?></small>
<?php endforeach ?>
@ -172,12 +170,12 @@ use Aviat\AnimeClient\API\Kitsu;
<th>Cast Member</th>
<th>Series</th>
</tr>
<?php foreach ($casting as $cid => $c): ?>
<?php foreach ($casting as $c): ?>
<tr>
<td>
<article class="character">
<?php
$link = $url->generate('person', ['id' => $c['person']['id']]);
$link = $url->generate('person', ['id' => $c['person']['id'], 'slug' => $c['person']['slug']]);
?>
<a href="<?= $link ?>">
<?= $helper->picture(getLocalImg($c['person']['image'])) ?>
@ -192,16 +190,15 @@ use Aviat\AnimeClient\API\Kitsu;
<?php foreach ($c['series'] as $series): ?>
<article class="media">
<?php
$link = $url->generate('anime.details', ['id' => $series['attributes']['slug']]);
$titles = Kitsu::filterTitles($series['attributes']);
$link = $url->generate('anime.details', ['id' => $series['slug']]);
?>
<a href="<?= $link ?>">
<?= $helper->picture(getLocalImg($series['attributes']['posterImage']['small'], TRUE)) ?>
<?= $helper->picture(getLocalImg($series['posterImage'], TRUE)) ?>
</a>
<div class="name">
<a href="<?= $link ?>">
<?= array_shift($titles) ?>
<?php foreach ($titles as $title): ?>
<?= $series['title'] ?>
<?php foreach ($series['titles'] as $title): ?>
<br />
<small><?= $title ?></small>
<?php endforeach ?>

View File

@ -43,6 +43,7 @@
"danielstjules/stringy": "^3.1.0",
"ext-dom": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-json": "*",
"ext-gd": "*",
"ext-pdo": "*",

View File

@ -292,6 +292,34 @@ final class Kitsu {
return $valid;
}
/**
* Filter out duplicate and very similar titles from a GraphQL response
*
* @param array $titles
* @return array
*/
public static function getFilteredTitles(array $titles): array
{
// The 'canonical' title is always considered
$valid = [$titles['canonical']];
if (array_key_exists('localized', $titles) && is_array($titles['localized']))
{
foreach($titles['localized'] as $alternateTitle)
{
if (self::titleIsUnique($alternateTitle, $valid))
{
$valid[] = $alternateTitle;
}
}
}
// Don't return the canonical titles
array_shift($valid);
return $valid;
}
/**
* Get the name and logo for the streaming service of the current link
*

View File

@ -1,6 +1,7 @@
query ($id: ID!) {
findPersonById(id: $id) {
id
description
birthday
image {
original {
@ -22,9 +23,7 @@ query ($id: ID!) {
localized
}
voices {
licensor {
name
}
nodes {
locale
mediaCharacter {
role
@ -67,3 +66,4 @@ query ($id: ID!) {
}
}
}
}

View File

@ -17,9 +17,11 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\JsonAPI;
use Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\Types\Character;
use Aviat\Ion\Transformer\AbstractTransformer;
use Locale;
/**
* Data transformation class for character pages
@ -70,21 +72,90 @@ final class CharacterTransformer extends AbstractTransformer {
return [[], []];
}
$titleSort = fn ($a, $b) => $a['title'] <=> $b['title'];
$rawMedia = array_column($data, 'media');
$rawAnime = array_filter($rawMedia, fn ($item) => $item['type'] === 'Anime');
$rawManga = array_filter($rawMedia, fn ($item) => $item['type'] === 'Manga');
uasort($rawAnime, fn ($a, $b) => $a['titles']['canonical'] <=> $b['titles']['canonical']);
uasort($rawManga, fn ($a, $b) => $a['titles']['canonical'] <=> $b['titles']['canonical']);
$anime = array_map(static function ($item) {
$output = $item;
unset($output['titles']);
$output['title'] = $item['titles']['canonical'];
$output['titles'] = Kitsu::getFilteredTitles($item['titles']);
return $output;
}, $rawAnime);
$manga = array_map(static function ($item) {
$output = $item;
unset($output['titles']);
$output['title'] = $item['titles']['canonical'];
$output['titles'] = Kitsu::getFilteredTitles($item['titles']);
return $output;
}, $rawManga);
uasort($anime, $titleSort);
uasort($manga, $titleSort);
$media = [
'anime' => $rawAnime,
'manga' => $rawManga,
'anime' => $anime,
'manga' => $manga,
];
$rawVoices = array_filter($data, fn($item) => count((array)$item['voices']['nodes']) > 0);
if (empty($rawVoices))
{
return [$media, []];
}
$castings = [
'Voice Actor' => [],
];
foreach ($rawVoices as $voiceMap)
{
foreach ($voiceMap['voices']['nodes'] as $voice)
{
$lang = Locale::getDisplayLanguage($voice['locale'], 'en');
$id = $voice['person']['name'];
$seriesId = $voiceMap['media']['id'];
if ( ! array_key_exists($lang, $castings['Voice Actor']))
{
$castings['Voice Actor'][$lang] = [];
}
if ( ! array_key_exists($id, $castings['Voice Actor'][$lang]))
{
$castings['Voice Actor'][$lang][$id] = [
'person' => [
'id' => $voice['person']['id'],
'slug' => $voice['person']['slug'],
'image' => $voice['person']['image']['original']['url'],
'name' => $voice['person']['name'],
],
'series' => []
];
}
$castings['Voice Actor'][$lang][$id]['series'][$seriesId] = [
'id' => $seriesId,
'slug' => $voiceMap['media']['slug'],
'title' => $voiceMap['media']['titles']['canonical'],
'titles' => Kitsu::getFilteredTitles($voiceMap['media']['titles']),
'posterImage' => $voiceMap['media']['posterImage']['views'][1]['url'],
];
uasort($castings['Voice Actor'][$lang][$id]['series'], $titleSort);
ksort($castings['Voice Actor'][$lang]);
}
}
return [$media, $castings];
}
/**
* @param array $characterData
* @return Character