API client for Kitsu.io, with optional Anime collection, and optional Anilist syncing.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Collection.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php declare(strict_types=1);
  2. /**
  3. * Hummingbird Anime List Client
  4. *
  5. * An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
  6. *
  7. * PHP version 7
  8. *
  9. * @package HummingbirdAnimeClient
  10. * @author Timothy J. Warren <tim@timshomepage.net>
  11. * @copyright 2015 - 2018 Timothy J. Warren
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version 4.0
  14. * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
  15. */
  16. namespace Aviat\AnimeClient\Model;
  17. use Aviat\AnimeClient\Model\DB;
  18. use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
  19. use PDO;
  20. use PDOException;
  21. /**
  22. * Base model for anime and manga collections
  23. */
  24. class Collection extends DB {
  25. use ContainerAware;
  26. /**
  27. * Whether the database is valid for querying
  28. * @var boolean
  29. */
  30. protected $validDatabase = FALSE;
  31. /**
  32. * Create a new collection object
  33. *
  34. * @param ContainerInterface $container
  35. */
  36. public function __construct(ContainerInterface $container)
  37. {
  38. parent::__construct($container);
  39. try
  40. {
  41. $this->db = \Query($this->dbConfig['collection']);
  42. }
  43. catch (PDOException $e)
  44. {
  45. //$this->validDatabase = FALSE;
  46. //return FALSE;
  47. }
  48. // Is database valid? If not, set a flag so the
  49. // app can be run without a valid database
  50. if ($this->dbConfig['collection']['type'] === 'sqlite')
  51. {
  52. $dbFileName = $this->dbConfig['collection']['file'];
  53. if ($dbFileName !== ':memory:' && file_exists($dbFileName))
  54. {
  55. $dbFile = file_get_contents($dbFileName);
  56. $this->validDatabase = (strpos($dbFile, 'SQLite format 3') === 0);
  57. }
  58. else
  59. {
  60. $this->validDatabase = FALSE;
  61. }
  62. }
  63. else
  64. {
  65. $this->validDatabase = TRUE;
  66. }
  67. }
  68. /**
  69. * Get genres for anime collection items
  70. *
  71. * @param array $filter
  72. * @return array
  73. */
  74. public function getGenreList($filter = [])
  75. {
  76. $this->db->select('hummingbird_id, genre')
  77. ->from('genre_anime_set_link gl')
  78. ->join('genres g', 'g.id=gl.genre_id', 'left');
  79. if ( ! empty($filter))
  80. {
  81. $this->db->where_in('hummingbird_id', $filter);
  82. }
  83. $query = $this->db->order_by('hummingbird_id')
  84. ->order_by('genre')
  85. ->get();
  86. $output = [];
  87. foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row)
  88. {
  89. $id = $row['hummingbird_id'];
  90. $genre = $row['genre'];
  91. // Empty genre names aren't useful
  92. if (empty($genre))
  93. {
  94. continue;
  95. }
  96. if (array_key_exists($id, $output))
  97. {
  98. array_push($output[$id], $genre);
  99. }
  100. else
  101. {
  102. $output[$id] = [$genre];
  103. }
  104. }
  105. return $output;
  106. }
  107. }
  108. // End of Collection.php