Skip to content

Commit cf2f629

Browse files
authored
Merge pull request #2 from hmerritt/v1.0.0
Major rewrite of entire API
2 parents 5b0981c + fbf5dd9 commit cf2f629

File tree

12 files changed

+771
-426
lines changed

12 files changed

+771
-426
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hmerritt/imdb-api",
33
"description": "IMDB API that can fetch film data and search results",
4-
"keywords": ["imdb", "api", "films", "search"],
4+
"keywords": ["imdb", "api", "film", "movie", "search"],
55
"license": "Apache-2.0",
66
"authors": [
77
{
@@ -16,11 +16,11 @@
1616
"tmarois/filebase": "^1.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "6.*"
19+
"phpunit/phpunit": "9.0"
2020
},
2121
"autoload": {
2222
"psr-4": {
23-
"hmerritt\\Imdb\\": "src/"
23+
"hmerritt\\": "src/"
2424
}
2525
}
2626
}

phpunit.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Register The Composer Auto Loader
6+
|--------------------------------------------------------------------------
7+
|
8+
| Composer provides a convenient, automatically generated class loader
9+
| for our application. We just need to utilize it! We'll require it
10+
| into the script here so that we do not have to worry about the
11+
| loading of any our classes "manually". Feels great to relax.
12+
|
13+
*/
14+
15+
require __DIR__.'/vendor/autoload.php';
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| Set The Default Timezone
20+
|--------------------------------------------------------------------------
21+
|
22+
| Here we will set the default timezone for PHP. PHP is notoriously mean
23+
| if the timezone is not explicitly set. This will be used by each of
24+
| the PHP date and date-time functions throughout the application.
25+
|
26+
*/
27+
28+
date_default_timezone_set('UTC');

phpunit.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="phpunit.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Repository Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<filter>
19+
<whitelist addUncoveredFilesFromWhitelist="false">
20+
<directory suffix=".php">src</directory>
21+
<exclude>
22+
<directory suffix=".php">vendor</directory>
23+
</exclude>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

src/Cache.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
namespace hmerritt;
3+
4+
/**
5+
* Class Cache
6+
*
7+
*
8+
* @package hmerritt/imdb-api
9+
* @author Harry Merritt
10+
*/
11+
class Cache
12+
{
13+
14+
function __construct()
15+
{
16+
/**
17+
* Initiate cache database
18+
*
19+
* @var \Filebase\Database
20+
*/
21+
$this->cache = new \Filebase\Database([
22+
'dir' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/',
23+
'backupLocation' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/backups/',
24+
'format' => \Filebase\Format\Json::class,
25+
'cache' => true,
26+
'cache_expires' => 31540000,
27+
'pretty' => false
28+
]);
29+
}
30+
31+
/**
32+
* Add (or modify) an item in the cache
33+
*
34+
* @param string $key
35+
* @param string $value
36+
* @return bool
37+
*/
38+
public function add(string $key, $value)
39+
{
40+
$file = $this->get($key);
41+
$file->film = $value;
42+
$file->save();
43+
return true;
44+
}
45+
46+
/**
47+
* Counts all files in the cache
48+
*
49+
* @return int
50+
*/
51+
public function count(): int
52+
{
53+
return $this->cache->count();
54+
}
55+
56+
/**
57+
* Deletes an item from the cache
58+
*
59+
* @return bool
60+
*/
61+
public function delete(string $key): bool
62+
{
63+
$file = $this->get($key);
64+
$file->delete();
65+
return true;
66+
}
67+
68+
/**
69+
* Get an item from the cache
70+
*
71+
* @param string $key
72+
* @return object
73+
*/
74+
public function get(string $key): object
75+
{
76+
return $this->cache->get($key);
77+
}
78+
79+
/**
80+
* Check if an item exists in the cache
81+
*
82+
* @param string $key
83+
* @return bool
84+
*/
85+
public function has(string $key): bool
86+
{
87+
return $this->cache->has($key);
88+
}
89+
90+
}

src/Dom.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace hmerritt;
3+
4+
/**
5+
* Class Dom
6+
*
7+
*
8+
* @package hmerritt/imdb-api
9+
* @author Harry Merritt
10+
*/
11+
class Dom
12+
{
13+
14+
/**
15+
* Fetch and parse the DOM of a remote site
16+
*
17+
* @param string $url
18+
*
19+
* @return \PHPHtmlParser\Dom
20+
*/
21+
public function fetch(string $url, array $options)
22+
{
23+
$dom = new \PHPHtmlParser\Dom;
24+
$dom->loadFromUrl($url, [
25+
"curlHeaders" => $options["curlHeaders"]
26+
]);
27+
return $dom;
28+
}
29+
30+
/**
31+
* Find object within DOM (if it exists) and reutrn an attribute
32+
*
33+
* @param object $dom
34+
* @param string $selection
35+
*
36+
* @return array|object
37+
*/
38+
public function find(object $dom, string $selection)
39+
{
40+
$found = $dom->find($selection);
41+
if (count($found) > 0) {
42+
return $found;
43+
}
44+
else {
45+
return $this->emptyElement();
46+
}
47+
}
48+
49+
/**
50+
* Create and parse an empty html string as a DOM element
51+
*
52+
* @return \PHPHtmlParser\Dom
53+
*/
54+
private function emptyElement()
55+
{
56+
$dom = new \PHPHtmlParser\Dom;
57+
$dom->load('<a emptyElement="true" src="" href="" data-video=""></a>');
58+
return $dom;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)