|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* 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 AnimeListClient |
|
10
|
|
|
* @author Timothy J. Warren <[email protected]> |
|
11
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren |
|
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
13
|
|
|
* @version 4.0 |
|
14
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Aviat\AnimeClient\Model; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
use Aviat\Ion\Di\ContainerAware; |
|
21
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
|
22
|
|
|
use Aviat\Ion\Model\DB; |
|
|
|
|
|
|
23
|
|
|
use PDO; |
|
24
|
|
|
use PDOException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Base model for anime and manga collections |
|
28
|
|
|
*/ |
|
29
|
|
|
class Collection extends DB { |
|
30
|
|
|
|
|
31
|
|
|
use ContainerAware; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Anime API Model |
|
35
|
|
|
* @var object $anime_model |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $anime_model; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whether the database is valid for querying |
|
41
|
|
|
* @var boolean |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $valid_database = FALSE; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new collection object |
|
47
|
|
|
* |
|
48
|
|
|
* @param ContainerInterface $container |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ContainerInterface $container) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->container = $container; |
|
53
|
|
|
|
|
54
|
|
|
parent::__construct($container->get('config')); |
|
55
|
|
|
|
|
56
|
|
|
try |
|
57
|
|
|
{ |
|
58
|
|
|
$this->db = \Query($this->db_config['collection']); |
|
59
|
|
|
} |
|
60
|
|
|
catch (PDOException $e) |
|
61
|
|
|
{ |
|
62
|
|
|
//$this->valid_database = FALSE; |
|
|
|
|
|
|
63
|
|
|
//return FALSE; |
|
64
|
|
|
} |
|
65
|
|
|
$this->anime_model = $container->get('anime-model'); |
|
66
|
|
|
|
|
67
|
|
|
// Is database valid? If not, set a flag so the |
|
68
|
|
|
// app can be run without a valid database |
|
69
|
|
|
if ($this->db_config['collection']['type'] === 'sqlite') |
|
70
|
|
|
{ |
|
71
|
|
|
$db_file_name = $this->db_config['collection']['file']; |
|
72
|
|
|
|
|
73
|
|
|
if ($db_file_name !== ':memory:' && file_exists($db_file_name)) |
|
74
|
|
|
{ |
|
75
|
|
|
$db_file = file_get_contents($db_file_name); |
|
76
|
|
|
$this->valid_database = (strpos($db_file, 'SQLite format 3') === 0); |
|
77
|
|
|
} |
|
78
|
|
|
else |
|
79
|
|
|
{ |
|
80
|
|
|
$this->valid_database = FALSE; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
else |
|
84
|
|
|
{ |
|
85
|
|
|
$this->valid_database = TRUE; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get genres for anime collection items |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $filter |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function get_genre_list($filter = []) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->db->select('hummingbird_id, genre') |
|
98
|
|
|
->from('genre_anime_set_link gl') |
|
99
|
|
|
->join('genres g', 'g.id=gl.genre_id', 'left'); |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if ( ! empty($filter)) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->db->where_in('hummingbird_id', $filter); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$query = $this->db->order_by('hummingbird_id') |
|
108
|
|
|
->order_by('genre') |
|
109
|
|
|
->get(); |
|
110
|
|
|
|
|
111
|
|
|
$output = []; |
|
112
|
|
|
|
|
113
|
|
|
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $row) |
|
114
|
|
|
{ |
|
115
|
|
|
$id = $row['hummingbird_id']; |
|
116
|
|
|
$genre = $row['genre']; |
|
117
|
|
|
|
|
118
|
|
|
// Empty genre names aren't useful |
|
119
|
|
|
if (empty($genre)) |
|
120
|
|
|
{ |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (array_key_exists($id, $output)) |
|
125
|
|
|
{ |
|
126
|
|
|
array_push($output[$id], $genre); |
|
127
|
|
|
} |
|
128
|
|
|
else |
|
129
|
|
|
{ |
|
130
|
|
|
$output[$id] = [$genre]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $output; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
// End of Collection.php |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: