mirror of
https://github.com/elyby/php-tempmailbuster.git
synced 2024-11-08 13:42:37 +05:30
Implemented Loader class and added reference repository (for now as git repo, in future - as composer package)
This commit is contained in:
parent
ed82b4a986
commit
afad6a5b09
@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "ely/php-tempmailbuster",
|
||||
"license": "MIT",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "ErickSkrauch",
|
||||
@ -9,11 +10,26 @@
|
||||
],
|
||||
"require": {
|
||||
"php" : "~5.4|~7.0",
|
||||
"lib-curl" : "*"
|
||||
"lib-curl" : "*",
|
||||
"ely/anti-tempmail-repo" : "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.8 || ~5.0"
|
||||
},
|
||||
"repositories": [
|
||||
{
|
||||
"type": "package",
|
||||
"package": {
|
||||
"name": "ely/anti-tempmail-repo",
|
||||
"version": "0.0.0",
|
||||
"source": {
|
||||
"url": "https://github.com/elyby/anti-tempmail-repo",
|
||||
"type": "git",
|
||||
"reference": "master"
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Ely\\TempMailBuster\\": "src"
|
||||
|
38
src/Loader.php
Normal file
38
src/Loader.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace Ely\TempMailBuster;
|
||||
|
||||
use Exception;
|
||||
|
||||
class Loader
|
||||
{
|
||||
public static function getPaths()
|
||||
{
|
||||
return [
|
||||
__DIR__ . '/../vendor/ely/anti-tempmail-repo/data.json',
|
||||
__DIR__ . '/../../anti-tempmail-repo/data.json',
|
||||
];
|
||||
}
|
||||
|
||||
public static function load()
|
||||
{
|
||||
$paths = static::getPaths();
|
||||
$dataPath = null;
|
||||
foreach($paths as $path) {
|
||||
if (file_exists($path)) {
|
||||
$dataPath = $path;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($dataPath === null) {
|
||||
throw new Exception('Cannot find data file. Please check getPaths() implementation.');
|
||||
}
|
||||
|
||||
$data = json_decode(file_get_contents($dataPath), true);
|
||||
if (!is_array($data)) {
|
||||
throw new Exception('Cannot decode json from data file.');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
47
tests/Loadertest.php
Normal file
47
tests/Loadertest.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Ely\TempMailBuster;
|
||||
|
||||
class LoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetPaths()
|
||||
{
|
||||
$this->assertTrue(is_array(Loader::getPaths()));
|
||||
}
|
||||
|
||||
public function testLoad()
|
||||
{
|
||||
$this->assertTrue(is_array(Loader::load()));
|
||||
}
|
||||
|
||||
public function testLoadExceptionWrongPaths()
|
||||
{
|
||||
$this->expectException('Exception');
|
||||
LoaderWithWrongPaths::load();
|
||||
}
|
||||
|
||||
public function testLoadExceptionInvalidJson()
|
||||
{
|
||||
$this->expectException('Exception');
|
||||
LoaderWithInvalidJson::load();
|
||||
}
|
||||
}
|
||||
|
||||
class LoaderWithWrongPaths extends Loader
|
||||
{
|
||||
public static function getPaths()
|
||||
{
|
||||
return [
|
||||
__DIR__ . '/virtual_reality.json',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class LoaderWithInvalidJson extends Loader
|
||||
{
|
||||
public static function getPaths()
|
||||
{
|
||||
return [
|
||||
__DIR__ . '/LoaderTest.php',
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user