graphql-php/tests/StarWarsData.php

172 lines
3.8 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-02 14:39:30 +03:00
declare(strict_types=1);
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests;
2015-07-15 20:05:46 +03:00
2018-09-02 14:39:30 +03:00
use function array_map;
2015-07-15 20:05:46 +03:00
class StarWarsData
{
2018-09-02 14:39:30 +03:00
/**
* Helper function to get a character by ID.
*/
public static function getCharacter($id)
{
$humans = self::humans();
$droids = self::droids();
if (isset($humans[$id])) {
return $humans[$id];
}
if (isset($droids[$id])) {
return $droids[$id];
}
return null;
}
public static function humans()
{
return [
'1000' => self::luke(),
'1001' => self::vader(),
'1002' => self::han(),
'1003' => self::leia(),
'1004' => self::tarkin(),
];
}
2015-07-15 20:05:46 +03:00
private static function luke()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '1000',
'name' => 'Luke Skywalker',
'friends' => ['1002', '1003', '2000', '2001'],
'appearsIn' => [4, 5, 6],
2015-07-15 20:05:46 +03:00
'homePlanet' => 'Tatooine',
];
}
private static function vader()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '1001',
'name' => 'Darth Vader',
'friends' => ['1004'],
'appearsIn' => [4, 5, 6],
2015-07-15 20:05:46 +03:00
'homePlanet' => 'Tatooine',
];
}
private static function han()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '1002',
'name' => 'Han Solo',
'friends' => ['1000', '1003', '2001'],
2015-07-15 20:05:46 +03:00
'appearsIn' => [4, 5, 6],
];
}
private static function leia()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '1003',
'name' => 'Leia Organa',
'friends' => ['1000', '1002', '2000', '2001'],
'appearsIn' => [4, 5, 6],
2015-07-15 20:05:46 +03:00
'homePlanet' => 'Alderaan',
];
}
private static function tarkin()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '1004',
'name' => 'Wilhuff Tarkin',
'friends' => ['1001'],
2015-07-15 20:05:46 +03:00
'appearsIn' => [4],
];
}
2018-09-02 14:39:30 +03:00
public static function droids()
2015-07-15 20:05:46 +03:00
{
return [
2018-09-02 14:39:30 +03:00
'2000' => self::threepio(),
'2001' => self::artoo(),
2015-07-15 20:05:46 +03:00
];
}
private static function threepio()
{
return [
2018-09-02 14:39:30 +03:00
'id' => '2000',
'name' => 'C-3PO',
'friends' => ['1000', '1002', '1003', '2001'],
'appearsIn' => [4, 5, 6],
2015-07-15 20:05:46 +03:00
'primaryFunction' => 'Protocol',
];
}
/**
* We export artoo directly because the schema returns him
* from a root field, and hence needs to reference him.
*/
2018-09-02 14:39:30 +03:00
private static function artoo()
2015-07-15 20:05:46 +03:00
{
return [
2018-09-02 14:39:30 +03:00
'id' => '2001',
'name' => 'R2-D2',
'friends' => ['1000', '1002', '1003'],
'appearsIn' => [4, 5, 6],
2015-07-15 20:05:46 +03:00
'primaryFunction' => 'Astromech',
];
}
2016-10-22 20:46:15 +03:00
/**
* Allows us to query for a character's friends.
*/
2018-09-02 14:39:30 +03:00
public static function getFriends($character)
2016-10-22 20:46:15 +03:00
{
return array_map([__CLASS__, 'getCharacter'], $character['friends']);
}
2015-08-16 13:17:35 +03:00
/**
2018-09-02 14:39:30 +03:00
* @param int $episode
* @return mixed[]
2015-08-16 13:17:35 +03:00
*/
2018-09-02 14:39:30 +03:00
public static function getHero($episode)
2015-08-16 13:17:35 +03:00
{
if ($episode === 5) {
// Luke is the hero of Episode V.
return self::luke();
}
2018-09-02 14:39:30 +03:00
2015-08-16 13:17:35 +03:00
// Artoo is the hero otherwise.
return self::artoo();
}
2015-07-15 20:05:46 +03:00
/**
* @param string $id
2016-10-22 20:46:15 +03:00
* @return mixed|null
2015-07-15 20:05:46 +03:00
*/
2018-09-02 14:39:30 +03:00
public static function getHuman($id)
2015-07-15 20:05:46 +03:00
{
2016-10-22 20:46:15 +03:00
$humans = self::humans();
2018-09-02 14:39:30 +03:00
return $humans[$id] ?? null;
2016-10-22 20:46:15 +03:00
}
/**
* @param string $id
2016-10-22 20:46:15 +03:00
* @return mixed|null
*/
2018-09-02 14:39:30 +03:00
public static function getDroid($id)
2016-10-22 20:46:15 +03:00
{
$droids = self::droids();
2018-09-02 14:39:30 +03:00
return $droids[$id] ?? null;
2015-07-15 20:05:46 +03:00
}
}