1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* repository of known profiles and the corresponding schemas. |
7
|
|
|
*/ |
8
|
|
|
class Registry |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* get the profile which should be used for validation from the given resource descriptor. |
12
|
|
|
*/ |
13
|
|
|
public static function getResourceValidationProfile($descriptor) |
14
|
|
|
{ |
15
|
|
|
$descriptor = Utils::objectify($descriptor); |
16
|
|
|
if (isset($descriptor->profile) && $descriptor->profile != 'default') { |
17
|
|
|
return $descriptor->profile; |
18
|
|
|
} else { |
19
|
|
|
return 'data-resource'; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* get the profile which should be used for validation from the given datapackage descriptor |
25
|
|
|
* corresponds to the id from the registry. |
26
|
|
|
*/ |
27
|
|
|
public static function getDatapackageValidationProfile($descriptor) |
28
|
|
|
{ |
29
|
|
|
if (isset($descriptor->profile) && $descriptor->profile != 'default') { |
30
|
|
|
return $descriptor->profile; |
31
|
|
|
} else { |
32
|
|
|
return 'data-package'; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* given a normalized profile - get the corresponding schema file for known schema in the registry |
38
|
|
|
* returns false in case of unknown schema |
39
|
|
|
* works the same for both datapackage schema and resource schemas. |
40
|
|
|
*/ |
41
|
|
|
public static function getJsonSchemaFile($profile) |
42
|
|
|
{ |
43
|
|
|
foreach (static::getAllSchemas() as $schema) { |
44
|
|
|
if ($schema->id != 'registry' && $schema->id == $profile) { |
45
|
|
|
if (isset($schema->schema_path)) { |
46
|
|
|
return realpath(dirname(__FILE__)).'/Validators/schemas/'.$schema->schema_path; |
47
|
|
|
} else { |
48
|
|
|
return $schema->schema_filename; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function registerSchema($profile, $filename) |
57
|
|
|
{ |
58
|
|
|
static::$registeredSchemas[$profile] = ['filename' => $filename]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function clearRegisteredSchemas() |
62
|
|
|
{ |
63
|
|
|
static::$registeredSchemas = []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* returns array of all known schemas in the registry. |
68
|
|
|
*/ |
69
|
|
|
public static function getAllSchemas() |
70
|
|
|
{ |
71
|
|
|
// registry schema |
72
|
|
|
$registrySchemaFilename = dirname(__FILE__).'/Validators/schemas/registry.json'; |
73
|
|
|
$registry = [ |
74
|
|
|
(object) [ |
75
|
|
|
'id' => 'registry', |
76
|
|
|
'schema' => 'https://specs.frictionlessdata.io/schemas/registry.json', |
77
|
|
|
'schema_path' => 'registry.json', |
78
|
|
|
], |
79
|
|
|
]; |
80
|
|
|
// schemas from the registry (currently contains only the datapackage scheams) |
81
|
|
|
$schemaIds = []; |
82
|
|
|
if (file_exists($registrySchemaFilename)) { |
83
|
|
|
foreach (json_decode(file_get_contents($registrySchemaFilename)) as $schema) { |
84
|
|
|
$schemaIds[] = $schema->id; |
85
|
|
|
$registry[] = $schema; |
86
|
|
|
} |
87
|
|
|
// resource schemas - currently not in the registry |
88
|
|
|
foreach (['data-resource', 'tabular-data-resource'] as $id) { |
89
|
|
|
if (!in_array($id, $schemaIds)) { |
90
|
|
|
$registry[] = (object) [ |
91
|
|
|
'id' => $id, |
92
|
|
|
'schema' => "https://specs.frictionlessdata.io/schemas/{$id}.json", |
93
|
|
|
'schema_path' => "{$id}.json", |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
// custom registered schemas |
99
|
|
|
foreach (static::$registeredSchemas as $profile => $schema) { |
100
|
|
|
$registry[] = (object) [ |
101
|
|
|
'id' => $profile, |
102
|
|
|
'schema_filename' => $schema['filename'], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $registry; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected static $registeredSchemas = []; |
110
|
|
|
} |
111
|
|
|
|