1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Tmdb package. |
4
|
|
|
* |
5
|
|
|
* (c) Vincent Faliès <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Vincent Faliès <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2017 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace VfacTmdb; |
16
|
|
|
|
17
|
|
|
use VfacTmdb\Exceptions\NotFoundException; |
18
|
|
|
use VfacTmdb\Exceptions\IncorrectParamException; |
19
|
|
|
use VfacTmdb\Interfaces\TmdbInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Media class |
23
|
|
|
* @package Tmdb |
24
|
|
|
* @author Vincent Faliès <[email protected]> |
25
|
|
|
* @copyright Copyright (c) 2017 |
26
|
|
|
*/ |
27
|
|
|
class Media |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Tmdb object |
31
|
|
|
* @var TmdbInterface |
32
|
|
|
*/ |
33
|
|
|
protected $tmdb = null; |
34
|
|
|
/** |
35
|
|
|
* Configuration |
36
|
|
|
* @var \stdClass |
37
|
|
|
*/ |
38
|
|
|
protected $conf = null; |
39
|
|
|
/** |
40
|
|
|
* Logger |
41
|
|
|
* @var \Psr\Log\LoggerInterface |
42
|
|
|
*/ |
43
|
|
|
protected $logger = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* @param TmdbInterface $tmdb |
48
|
|
|
*/ |
49
|
3 |
|
public function __construct(TmdbInterface $tmdb) |
50
|
|
|
{ |
51
|
3 |
|
$this->tmdb = $tmdb; |
52
|
3 |
|
$this->logger = $tmdb->getLogger(); |
53
|
3 |
|
$this->conf = $this->tmdb->getConfiguration(); |
54
|
3 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get backdrop url |
58
|
|
|
* @param string $path |
59
|
|
|
* @param string $size |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
3 |
|
public function getBackdropUrl(string $path, string $size = 'w780') : ?string |
63
|
|
|
{ |
64
|
3 |
|
return $this->getImage('backdrop', $size, $path); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get poster url |
69
|
|
|
* @param string $path |
70
|
|
|
* @param string $size |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
1 |
|
public function getPosterUrl(string $path, string $size = 'w185') : ?string |
74
|
|
|
{ |
75
|
1 |
|
return $this->getImage('poster', $size, $path); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get logos url |
80
|
|
|
* @param string $path |
81
|
|
|
* @param string $size |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
1 |
|
public function getlogoUrl(string $path, string $size = 'w92') : ?string |
85
|
|
|
{ |
86
|
1 |
|
return $this->getImage('logo', $size, $path); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get profile url |
91
|
|
|
* @param string $path |
92
|
|
|
* @param string $size |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
1 |
|
public function getProfileUrl(string $path, string $size = 'w185') : ?string |
96
|
|
|
{ |
97
|
1 |
|
return $this->getImage('profile', $size, $path); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get poster url |
102
|
|
|
* @param string $path |
103
|
|
|
* @param string $size |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
public function getStillUrl(string $path, string $size = 'w185') : ?string |
107
|
|
|
{ |
108
|
1 |
|
return $this->getImage('still', $size, $path); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get image url from type and size |
113
|
|
|
* @param string $type |
114
|
|
|
* @param string $size |
115
|
|
|
* @param string $filepath |
116
|
|
|
* @return string |
117
|
|
|
* @throws NotFoundException |
118
|
|
|
* @throws IncorrectParamException |
119
|
|
|
*/ |
120
|
3 |
|
private function getImage(string $type, string $size, string $filepath) : string |
121
|
|
|
{ |
122
|
3 |
|
if (!isset($this->conf->images->base_url)) { |
123
|
1 |
|
$this->logger->error('No image base url found from configuration'); |
124
|
1 |
|
throw new NotFoundException; |
125
|
|
|
} |
126
|
2 |
|
$sizes = $type . '_sizes'; |
127
|
2 |
|
if (!in_array($size, $this->conf->images->$sizes)) { |
128
|
1 |
|
$this->logger->error('Incorrect param image size', array('type' => $type, 'size' => $size, 'filepath' => $filepath)); |
129
|
1 |
|
throw new IncorrectParamException; |
130
|
|
|
} |
131
|
1 |
|
return $this->conf->images->base_url . $size . $filepath; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|