Skip to content

Commit e1f1325

Browse files
committed
v2.0.0
1 parent 94f74b2 commit e1f1325

File tree

8 files changed

+232
-117
lines changed

8 files changed

+232
-117
lines changed

README.md

+82-35
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,91 @@ composer require slvler/tmdb
1515
## Usage
1616
- first step is to start client client with required url, version, api key
1717
```php
18-
$option['base_url'] = 'https://api.themoviedb.org/3/'
19-
$option['api_key'] = 'xxxx'
20-
$option['version'] = '3'
18+
use Slvler\Tmdb\Tmdb;
19+
use Slvler\Tmdb\Factory;
2120

22-
$client = new Client($option);
21+
$request = Tmdb::factory()
22+
->withBaseUrl('https://api.themoviedb.org/3')
23+
->withHttpHeader('Authorization', 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
24+
->withPath('movie/11')
25+
->make();
2326
```
24-
- You need to make a request to the tmdb service with the service point we created.
25-
```php
26-
$tmdb = new Tmdb($client);
27-
```
28-
- Then you can send a request like this to bring the information of the movie we want.
29-
```php
30-
$tmdb->movie("550");
31-
```
32-
- You can it to fetch player information.
33-
```php
34-
$tmdb->people("13");
35-
```
36-
- to search on the specified keyword
37-
```php
38-
$tmdb->keyword("550");
39-
```
40-
- to search in the specified condition
41-
```php
42-
$tmdb->network("550");
43-
```
44-
- You can it to search in specified situations.
45-
```php
46-
$tmdb->review("58aa82f09251416f92006a3a");
47-
```
48-
- You want this service when you want a TV program.
49-
```php
50-
$tmdb->tv("111");
51-
```
52-
- movies with collection
53-
```php
54-
$tmdb->collection("10");
27+
- response
28+
```json
29+
{
30+
"adult": false,
31+
"backdrop_path": "/zqkmTXzjkAgXmEWLRsY4UpTWCeo.jpg",
32+
"belongs_to_collection": {
33+
"id": 10,
34+
"name": "Star Wars Collection",
35+
"poster_path": "/r8Ph5MYXL04Qzu4QBbq2KjqwtkQ.jpg",
36+
"backdrop_path": "/zZDkgOmFMVYpGAkR9Tkxw0CRnxX.jpg"
37+
},
38+
"budget": 11000000,
39+
"genres": [
40+
{
41+
"id": 12,
42+
"name": "Adventure"
43+
},
44+
{
45+
"id": 28,
46+
"name": "Action"
47+
},
48+
{
49+
"id": 878,
50+
"name": "Science Fiction"
51+
}
52+
],
53+
"homepage": "http://www.starwars.com/films/star-wars-episode-iv-a-new-hope",
54+
"id": 11,
55+
"imdb_id": "tt0076759",
56+
"origin_country": [
57+
"US"
58+
],
59+
"original_language": "en",
60+
"original_title": "Star Wars",
61+
"overview": "Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire.",
62+
"popularity": 91.205,
63+
"poster_path": "/6FfCtAuVAW8XJjZ7eWeLibRLWTw.jpg",
64+
"production_companies": [
65+
{
66+
"id": 1,
67+
"logo_path": "/tlVSws0RvvtPBwViUyOFAO0vcQS.png",
68+
"name": "Lucasfilm Ltd.",
69+
"origin_country": "US"
70+
},
71+
{
72+
"id": 25,
73+
"logo_path": "/qZCc1lty5FzX30aOCVRBLzaVmcp.png",
74+
"name": "20th Century Fox",
75+
"origin_country": "US"
76+
}
77+
],
78+
"production_countries": [
79+
{
80+
"iso_3166_1": "US",
81+
"name": "United States of America"
82+
}
83+
],
84+
"release_date": "1977-05-25",
85+
"revenue": 775398007,
86+
"runtime": 121,
87+
"spoken_languages": [
88+
{
89+
"english_name": "English",
90+
"iso_639_1": "en",
91+
"name": "English"
92+
}
93+
],
94+
"status": "Released",
95+
"tagline": "A long time ago in a galaxy far, far away...",
96+
"title": "Star Wars",
97+
"video": false,
98+
"vote_average": 8.2,
99+
"vote_count": 20635
100+
}
55101
```
102+
56103
### Testing
57104
```bash
58105
vendor\bin\phpunit

composer.json

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
11
{
22
"name": "slvler/tmdb",
3-
"license": "MIT",
43
"description": "TMDB.com api service for php",
5-
"type": "package",
4+
"license": "MIT",
5+
"keywords": [
6+
"php",
7+
"tmdb",
8+
"movie service"
9+
],
610
"authors": [
711
{
812
"name": "slvler",
913
"email": "[email protected]"
1014
}
1115
],
16+
"homepage": "https://github.com/slvler/the-movie-database-service",
17+
"require": {
18+
"php": "^8.1",
19+
"php-http/discovery": "^1.19.2"
20+
},
21+
"require-dev": {
22+
"guzzlehttp/guzzle": "^7.8.1",
23+
"guzzlehttp/psr7": "^2.6.2",
24+
"phpunit/phpunit": "^9.5"
25+
},
1226
"autoload": {
1327
"psr-4": {
14-
"slvler\\tmdb\\": "src/"
28+
"Slvler\\Tmdb\\": "src/"
1529
}
1630
},
1731
"autoload-dev": {
1832
"psr-4": {
19-
"slvler\\tmdb\\Tests\\": "tests/"
33+
"Slvler\\Tmdb\\Tests\\": "tests/"
2034
}
2135
},
22-
"require": {},
23-
"require-dev": {
24-
"phpunit/phpunit": "^9.5"
25-
},
2636
"scripts": {
2737
"test": "vendor/bin/phpunit tests"
2838
},
2939
"minimum-stability": "stable",
30-
"prefer-stable": true
40+
"prefer-stable": true,
41+
"config": {
42+
"allow-plugins": {
43+
"php-http/discovery": true
44+
}
45+
}
3146
}

example/index.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22

33
require __DIR__ . '/../vendor/autoload.php';
44

5-
use Qwerty\Tmdb\Client;
6-
use Qwerty\Tmdb\Tmdb;
5+
use Slvler\Tmdb\Tmdb;
6+
use Slvler\Tmdb\Factory;
77

8+
$request = Tmdb::factory()
9+
->withBaseUrl('https://api.themoviedb.org/3')
10+
->withHttpHeader('Authorization', 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
11+
->withPath('movie/11')
12+
->make();
813

9-
$option = [
10-
'base_url' => "https://api.themoviedb.org/3/movie/550",
11-
'api_key' => "b1931f4b871deeec6800ccde48a5f744",
12-
'version' => '3'
13-
];
14-
15-
$client = new Client($option);
16-
$ext = new Tmdb($client);
17-
18-
//echo $ext->movie("550");
19-
//echo $ext->people("13");
20-
//echo $ext->keyword("550");
21-
//echo $ext->network("550");
22-
//echo $ext->review("58aa82f09251416f92006a3a");
23-
//echo $ext->tv("111");
24-
echo $ext->collection("10");
14+
print_r($request);
2515

src/BuildRequest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Slvler\Tmdb;
4+
5+
use Psr\Http\Message\MessageInterface;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\StreamInterface;
8+
use Psr\Http\Message\UriInterface;
9+
10+
class BuildRequest
11+
{
12+
public $baseUrl;
13+
public $headers;
14+
public $queryParams;
15+
public $path;
16+
public function __construct($baseUrl, $headers, $queryParams, $path)
17+
{
18+
$this->baseUrl = $baseUrl;
19+
$this->headers = $headers;
20+
$this->queryParams = $queryParams;
21+
$this->path = $baseUrl.'/'.$path;
22+
}
23+
}

src/Client.php

-47
This file was deleted.

src/ClientInterface.php

-8
This file was deleted.

src/Contracts/TransporterContract.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Slvler\Tmdb\Contracts;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Slvler\Tmdb\BuildRequest;
7+
8+
interface TransporterContract
9+
{
10+
public function request(BuildRequest $request);
11+
12+
/**
13+
* Sends a stream request to a server.
14+
*/
15+
public function requestStream(BuildRequest $request);
16+
}

src/Factory.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Slvler\Tmdb;
4+
5+
use Http\Discovery\Psr18ClientDiscovery;
6+
use Psr\Http\Client\ClientInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
9+
class Factory
10+
{
11+
private ?ClientInterface $httpClient = null;
12+
private string $baseUrl = 'https://api.themoviedb.org/3';
13+
private array $headers = [
14+
'Content-Type' => 'application/json',
15+
];
16+
private array $queryParams = [];
17+
private string $path;
18+
public function withHttpClient(ClientInterface $client): self
19+
{
20+
$this->httpClient = $client;
21+
22+
return $this;
23+
}
24+
public function withBaseUrl(string $baseUrl): self
25+
{
26+
$this->baseUrl = $baseUrl;
27+
28+
return $this;
29+
}
30+
public function withHttpHeader(string $name, string $value): self
31+
{
32+
$this->headers[$name] = $value;
33+
34+
return $this;
35+
}
36+
public function withQueryParam(string $name, string $value): self
37+
{
38+
$this->queryParams[$name] = $value;
39+
40+
return $this;
41+
}
42+
public function withPath(string $path): self
43+
{
44+
$this->path = $path;
45+
46+
return $this;
47+
}
48+
public function make(): string
49+
{
50+
$client = $this->httpClient ??= Psr18ClientDiscovery::find();
51+
52+
$transporter = new BuildRequest(
53+
baseUrl: $this->baseUrl,
54+
headers: $this->headers,
55+
queryParams: $this->queryParams,
56+
path: $this->path
57+
);
58+
59+
$response = $client->sendRequest(new \GuzzleHttp\Psr7\Request('GET', $transporter->path, $transporter->headers));
60+
61+
$contents = $response->getBody()->getContents();
62+
63+
64+
$this->throwIfJsonError(response: $response, contents: $contents);
65+
return $contents;
66+
}
67+
68+
private function throwIfJsonError(ResponseInterface $response, string|ResponseInterface $contents)
69+
{
70+
if ($response->getStatusCode() < 400) {
71+
return false;
72+
}
73+
74+
if ($contents instanceof ResponseInterface) {
75+
$contents = $contents->getBody()->getContents();
76+
}
77+
return $contents;
78+
}
79+
}

0 commit comments

Comments
 (0)