forked from openfintechio/openfintech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataProvider.php
59 lines (43 loc) · 1.61 KB
/
DataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Oft\Provider;
final class DataProvider
{
private static $cache = [];
public static function getResources(string $resourceType): array
{
return self::loadData($resourceType);
}
public static function getResourceData(string $resourceType, string $resourceCode)
{
if (false === self::dataExists($resourceType, $resourceCode)) {
throw new \RuntimeException(
sprintf('Resource with type %s and code %s not found.', $resourceType, $resourceCode)
);
}
$data = self::loadData($resourceType);
return $data[$resourceCode];
}
public static function dataExists(string $resourceType, string $resourceCode): bool
{
$data = self::loadData($resourceType);
return array_key_exists($resourceCode, $data);
}
private static function loadData(string $resourceType): array
{
if (false === array_key_exists($resourceType, self::$cache)) {
$path = __DIR__ . '/data/' . $resourceType . '.json';
if (false === file_exists($path)) {
throw new \RuntimeException('Resource does not exist.');
}
$data = json_decode(file_get_contents($path), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Malformed json data provided.');
}
$codes = array_column($data, 'code');
$data = array_combine($codes, $data);
self::$cache[$resourceType] = $data;
}
return self::$cache[$resourceType];
}
}