Skip to content

Commit 1c3968e

Browse files
committed
Created Response class to manage the api response array
1 parent fded7db commit 1c3968e

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/Response.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,47 @@
1111
class Response
1212
{
1313

14+
/**
15+
* A persistent array to store the response in
16+
*
17+
* @var array
18+
*/
19+
public $store = [];
20+
21+
/**
22+
* Add (or modify) to the response $store
23+
*
24+
* @param string $key
25+
* @param string $value
26+
* @return array $store
27+
*/
28+
public function add(string $key, $value): array
29+
{
30+
// Add item to $store
31+
$this->store[$key] = $value;
32+
33+
return $this->store;
34+
}
35+
36+
/**
37+
* Get an individual value from the response $store
38+
*
39+
* @param string $key
40+
* @return $store[$key]
41+
*/
42+
public function get(string $key)
43+
{
44+
return $this->store[$key];
45+
}
46+
47+
/**
48+
* Returns the entire $store array
49+
*
50+
* @return array $store
51+
*/
52+
public function return(): array
53+
{
54+
return $this->store;
55+
}
56+
1457
}

src/imdb.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function populateOptions(array $options = []): array
4747
* film name is passed instead of film-id
4848
* @param string $film
4949
* @param array $options
50-
* @return array $filmData
50+
* @return array $response
5151
*/
5252
public function film(string $film, array $options = []): array
5353
{
@@ -58,15 +58,19 @@ public function film(string $film, array $options = []): array
5858
// -> handles what the api returns
5959
$response = new Response;
6060

61+
// Initiate dom object
62+
// -> handles page scraping
6163
$dom = new Dom;
64+
65+
// Load imdb page and parse the dom
6266
$page = $dom->fetch("https://www.imdb.com/title/tt0816692/", $options);
6367

64-
return [
65-
"id" => "tt0816692",
66-
"title" => "Interstellar",
67-
"length" => "2h 49min",
68-
"year" => "2014"
69-
];
68+
$response->add("id", "tt0816692");
69+
$response->add("title", "Interstellar");
70+
$response->add("length", "2h 49min");
71+
$response->add("year", "2014");
72+
73+
return $response->return();
7074
}
7175

7276
/**
@@ -80,11 +84,15 @@ public function search(string $search, array $options = []): array
8084
// Combine user options with default ones
8185
$options = $this->populateOptions($options);
8286

83-
return [
84-
"titles" => [],
85-
"names" => [],
86-
"companies" => []
87-
];
87+
// Initiate response object
88+
// -> handles what the api returns
89+
$response = new Response;
90+
91+
$response->add("titles", []);
92+
$response->add("names", []);
93+
$response->add("companies", []);
94+
95+
return $response->return();
8896
}
8997

9098
}

0 commit comments

Comments
 (0)