1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage\Validators; |
4
|
|
|
|
5
|
|
|
use frictionlessdata\datapackage\Registry; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* validate a datapackage descriptor |
9
|
|
|
* checks the profile attribute to determine which schema to validate with. |
10
|
|
|
*/ |
11
|
|
|
class DatapackageValidator extends BaseValidator |
12
|
|
|
{ |
13
|
|
|
protected function getSchemaValidationErrorClass() |
14
|
|
|
{ |
15
|
|
|
return 'frictionlessdata\\datapackage\\Validators\\DatapackageValidationError'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
protected function getValidationProfile() |
19
|
|
|
{ |
20
|
|
|
return Registry::getDatapackageValidationProfile($this->descriptor); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function getDescriptorForValidation() |
24
|
|
|
{ |
25
|
|
|
// add base path to uri fields - it runs before validations, so need to validate the attributes we need |
26
|
|
|
// TODO: find a more elegant way to do it with support for registring custom url fields |
27
|
|
|
$descriptor = clone $this->descriptor; |
28
|
|
|
if (isset($descriptor->resources) && is_array($descriptor->resources)) { |
29
|
|
|
foreach ($descriptor->resources as &$resource) { |
30
|
|
|
if (is_object($resource)) { |
31
|
|
|
$resource = clone $resource; |
32
|
|
|
if (isset($resource->path) && is_array($resource->path)) { |
33
|
|
|
foreach ($resource->path as &$url) { |
34
|
|
|
if (is_string($url)) { |
35
|
|
|
$url = 'file://'.$url; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $descriptor; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function validateSchema() |
47
|
|
|
{ |
48
|
|
|
parent::validateSchema(); |
49
|
|
|
if ($this->getValidationProfile() != 'data-package') { |
50
|
|
|
// all schemas must be an extension of datapackage spec |
51
|
|
|
$this->validateSchemaUrl( |
52
|
|
|
$this->convertValidationSchemaFilenameToUrl( |
53
|
|
|
$this->getJsonSchemaFileFromRegistry('data-package') |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function validateKeys() |
60
|
|
|
{ |
61
|
|
|
foreach ($this->descriptor->resources as $resourceDescriptor) { |
62
|
|
|
foreach ($this->resourceValidate($resourceDescriptor) as $error) { |
63
|
|
|
$this->errors[] = $error; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function resourceValidate($resourceDescriptor) |
69
|
|
|
{ |
70
|
|
|
return ResourceValidator::validate($resourceDescriptor, $this->basePath); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getJsonSchemaFileFromRegistry($profile) |
74
|
|
|
{ |
75
|
|
|
if ($filename = Registry::getJsonSchemaFile($profile)) { |
76
|
|
|
return $filename; |
77
|
|
|
} else { |
78
|
|
|
return parent::getJsonSchemaFileFromRegistry($profile); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: