-
Notifications
You must be signed in to change notification settings - Fork 2
/
Metadata.php
49 lines (36 loc) · 1.37 KB
/
Metadata.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
<?php
declare(strict_types=1);
namespace Oft\Provider;
final class Metadata
{
private static $cache = [];
public static function getResourceMetadata(string $resourceType, string $resourceCode)
{
if (false === self::metadataExists($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 metadataExists(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.');
}
self::$cache[$resourceType] = json_decode(file_get_contents($path), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Malformed json data provided.');
}
}
return self::$cache[$resourceType];
}
}