HummingBirdAnimeClient/src/API/JsonAPI.php

203 lines
4.3 KiB
PHP
Raw Normal View History

2017-01-10 21:13:44 -05:00
<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
2017-01-10 21:13:44 -05:00
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2017-01-10 21:13:44 -05:00
* @author Timothy J. Warren <tim@timshomepage.net>
2017-01-11 10:30:53 -05:00
* @copyright 2015 - 2017 Timothy J. Warren
2017-01-10 21:13:44 -05:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API;
2017-01-10 21:13:44 -05:00
/**
* Class encapsulating Json API data structure for a request or response
*/
class JsonAPI {
/**
* The full data array
*
* Basic structure is generally like so:
2017-02-17 10:55:17 -05:00
* [
2017-01-10 21:13:44 -05:00
* 'id' => '12016665',
* 'type' => 'libraryEntries',
* 'links' => [
* 'self' => 'https://kitsu.io/api/edge/library-entries/13016665'
* ],
* 'attributes' => [
*
* ]
* ]
*
* @var array
*/
protected $data = [];
2017-01-12 15:41:20 -05:00
2017-01-31 12:52:43 -05:00
public static function inlineRawIncludes(array &$data, string $key): array
{
foreach($data['data'] as $i => &$item)
{
$item[$key] = $data['included'][$i];
}
return $data['data'];
}
2017-01-12 15:41:20 -05:00
/**
* Take organized includes and inline them, where applicable
*
* @param array $included
* @param string $key The key of the include to inline the other included values into
* @return array
*/
public static function inlineIncludedRelationships(array $included, string $key): array
{
$inlined = [
$key => []
];
foreach ($included[$key] as $itemId => $item)
{
// Duplicate the item for the output
$inlined[$key][$itemId] = $item;
foreach($item['relationships'] as $type => $ids)
{
$inlined[$key][$itemId]['relationships'][$type] = [];
foreach($ids as $id)
{
$inlined[$key][$itemId]['relationships'][$type][$id] = $included[$type][$id];
}
}
}
return $inlined;
}
/**
* Reorganizes 'included' data to be keyed by
* type => [
* id => data/attributes,
* ]
*
* @param array $includes
* @return array
*/
public static function organizeIncludes(array $includes): array
{
$organized = [];
foreach ($includes as $item)
{
$type = $item['type'];
$id = $item['id'];
$organized[$type] = $organized[$type] ?? [];
$organized[$type][$id] = $item['attributes'];
if (array_key_exists('relationships', $item))
{
$organized[$type][$id]['relationships'] = static::organizeRelationships($item['relationships']);
}
}
return $organized;
}
2017-03-08 13:46:50 -05:00
2017-03-08 16:21:01 -05:00
/**
* Reorganize 'included' data
*
* @param array $includes
* @return array
*/
2017-03-08 13:46:50 -05:00
public static function lightlyOrganizeIncludes(array $includes): array
{
$organized = [];
foreach($includes as $item)
{
$type = $item['type'];
$id = $item['id'];
$organized[$type] = $organized[$type] ?? [];
$newItem = [];
foreach(['attributes', 'relationships'] as $key)
{
if (array_key_exists($key, $item))
{
$newItem[$key] = $item[$key];
}
}
$organized[$type][$id] = $newItem;
}
return $organized;
}
2017-01-12 15:41:20 -05:00
/**
* Reorganize relationship mappings to make them simpler to use
*
* Remove verbose structure, and just map:
* type => [ idArray ]
*
* @param array $relationships
* @return array
*/
public static function organizeRelationships(array $relationships): array
{
$organized = [];
foreach($relationships as $key => $data)
{
if ( ! array_key_exists('data', $data))
{
continue;
}
$organized[$key] = $organized[$key] ?? [];
foreach ($data['data'] as $item)
{
2017-03-20 13:14:01 -04:00
if (is_array($item) && array_key_exists('id', $item))
{
$organized[$key][] = $item['id'];
}
2017-01-12 15:41:20 -05:00
}
}
return $organized;
}
2017-03-08 16:21:01 -05:00
public static function fillRelationshipsFromIncludes(array $relationships, array $includes): array
{
$output = [];
foreach ($relationships as $key => $block)
{
if (array_key_exists('data', $block) && is_array($block['data']) && ! empty($block['data']))
{
2017-03-10 12:50:48 -05:00
$output[$key] = [];
if (array_key_exists('type', $block['data']) && array_key_exists('id', $block['data']))
{
$output[$key] = $includes[$block['data']['type']][$block['data']['id']];
}
else
{
foreach($block['data'] as $dBlock)
{
$output[$key][] = $includes[$dBlock['type']][$dBlock['id']];
}
}
2017-03-08 16:21:01 -05:00
}
}
return $output;
}
2017-01-10 21:13:44 -05:00
}