Get character details page from GraphQL, still need to do castings section, see #27

This commit is contained in:
Timothy Warren 2020-08-17 14:01:55 -04:00
parent 67d3b7c1dc
commit 681a70fd92
4 changed files with 128 additions and 20 deletions

View File

@ -42,15 +42,15 @@ use Aviat\AnimeClient\API\Kitsu;
<?php foreach ($data['media']['anime'] as $id => $anime): ?> <?php foreach ($data['media']['anime'] as $id => $anime): ?>
<article class="media"> <article class="media">
<?php <?php
$link = $url->generate('anime.details', ['id' => $anime['attributes']['slug']]); $link = $url->generate('anime.details', ['id' => $anime['slug']]);
$titles = Kitsu::filterTitles($anime['attributes']); $titles = Kitsu::getTitles($anime['titles']);
?> ?>
<a href="<?= $link ?>"> <a href="<?= $link ?>">
<?= $helper->picture("images/anime/{$id}.webp") ?> <?= $helper->picture("images/anime/{$anime['id']}.webp") ?>
</a> </a>
<div class="name"> <div class="name">
<a href="<?= $link ?>"> <a href="<?= $link ?>">
<?= array_shift($titles) ?> <?= $anime['titles']['canonical'] ?>
<?php foreach ($titles as $title): ?> <?php foreach ($titles as $title): ?>
<br /> <br />
<small><?= $title ?></small> <small><?= $title ?></small>
@ -70,15 +70,15 @@ use Aviat\AnimeClient\API\Kitsu;
<?php foreach ($data['media']['manga'] as $id => $manga): ?> <?php foreach ($data['media']['manga'] as $id => $manga): ?>
<article class="media"> <article class="media">
<?php <?php
$link = $url->generate('manga.details', ['id' => $manga['attributes']['slug']]); $link = $url->generate('manga.details', ['id' => $manga['slug']]);
$titles = Kitsu::filterTitles($manga['attributes']); $titles = Kitsu::getTitles($manga['titles']);
?> ?>
<a href="<?= $link ?>"> <a href="<?= $link ?>">
<?= $helper->picture("images/manga/{$id}.webp") ?> <?= $helper->picture("images/manga/{$manga['id']}.webp") ?>
</a> </a>
<div class="name"> <div class="name">
<a href="<?= $link ?>"> <a href="<?= $link ?>">
<?= array_shift($titles) ?> <?= $manga['titles']['canonical'] ?>
<?php foreach ($titles as $title): ?> <?php foreach ($titles as $title): ?>
<br /> <br />
<small><?= $title ?></small> <small><?= $title ?></small>

View File

@ -190,17 +190,8 @@ final class Model {
*/ */
public function getCharacter(string $slug): array public function getCharacter(string $slug): array
{ {
return $this->requestBuilder->getRequest('characters', [ return $this->requestBuilder->runQuery('CharacterDetails', [
'query' => [ 'slug' => $slug
'filter' => [
'slug' => $slug,
],
'fields' => [ // For some characters, these filters cause issues...so leave them out
// 'anime' => 'canonicalTitle,abbreviatedTitles,titles,slug,posterImage',
// 'manga' => 'canonicalTitle,abbreviatedTitles,titles,slug,posterImage'
],
'include' => 'castings.person,castings.media'
]
]); ]);
} }

View File

@ -6,10 +6,11 @@ query ($slug: String!) {
url url
} }
} }
description
names { names {
alternatives alternatives
canonical canonical
#canonicalLocale canonicalLocale
localized localized
}, },
primaryMedia { primaryMedia {
@ -23,6 +24,63 @@ query ($slug: String!) {
} }
type type
}, },
media {
nodes {
media {
id
slug
titles {
alternatives
canonical
canonicalLocale
localized
}
posterImage {
original {
height
name
url
width
}
views {
height
name
url
width
}
}
type
}
voices {
nodes {
id
licensor {
name
}
locale
person {
id
slug
name
names {
alternatives
canonical
canonicalLocale
localized
}
image {
original {
height
width
url
name
}
}
}
}
}
}
}
slug slug
} }
} }

View File

@ -31,6 +31,65 @@ final class CharacterTransformer extends AbstractTransformer {
* @return Character * @return Character
*/ */
public function transform($characterData): Character public function transform($characterData): Character
{
$data = $characterData['data']['findCharacterBySlug'] ?? [];
$castings = [];
$media = [
'anime' => [],
'manga' => [],
];
$names = array_unique(
array_merge(
[$data['names']['canonical']],
array_values($data['names']['localized'])
)
);
$name = array_shift($names);
if (isset($data['media']['nodes']))
{
[$media, $castings] = $this->organizeMediaAndVoices($data['media']['nodes']);
}
return Character::from([
'castings' => $castings,
'description' => $data['description']['en'],
'id' => $data['id'],
'media' => $media,
'name' => $name,
'names' => $names,
'otherNames' => $data['names']['alternatives'],
]);
}
protected function organizeMediaAndVoices (array $data): array
{
if (empty($data))
{
return [[], []];
}
$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']);
$media = [
'anime' => $rawAnime,
'manga' => $rawManga,
];
return [$media, []];
}
/**
* @param array $characterData
* @return Character
*/
public function oldTransform($characterData): Character
{ {
$data = JsonAPI::organizeData($characterData); $data = JsonAPI::organizeData($characterData);
$attributes = $data[0]['attributes']; $attributes = $data[0]['attributes'];