1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage\Validators; |
4
|
|
|
|
5
|
|
|
use frictionlessdata\datapackage\Registry; |
6
|
|
|
use frictionlessdata\datapackage\Factory; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* validate a resource descriptor |
10
|
|
|
* checks the profile attribute to determine which schema to validate with. |
11
|
|
|
*/ |
12
|
|
|
class ResourceValidator extends BaseValidator |
13
|
|
|
{ |
14
|
|
|
protected function getSchemaValidationErrorClass() |
15
|
|
|
{ |
16
|
|
|
return 'frictionlessdata\\datapackage\\Validators\\ResourceValidationError'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function getValidationProfile() |
20
|
|
|
{ |
21
|
|
|
return Registry::getResourceValidationProfile($this->descriptor); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function getDescriptorForValidation() |
25
|
|
|
{ |
26
|
|
|
// add base path to uri fields |
27
|
|
|
// need to ensure all attributes exist because we are before schema validation |
28
|
|
|
// TODO: find a more elegant way to do it with support for registring custom url fields |
29
|
|
|
$descriptor = clone $this->descriptor; |
30
|
|
|
if (isset($descriptor->path) && is_array($descriptor->path)) { |
31
|
|
|
foreach ($descriptor->path as &$url) { |
32
|
|
|
$url = 'file://'.$url; |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $descriptor; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function getValidationErrorMessage($error) |
40
|
|
|
{ |
41
|
|
|
$property = $error['property']; |
42
|
|
|
// silly hack to only show properties within the resource of the fake datapackage |
43
|
|
|
// $property = str_replace("resources[0].", "", $property); |
|
|
|
|
44
|
|
|
return sprintf('[%s] %s', $property, $error['message']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function getResourceClass() |
48
|
|
|
{ |
49
|
|
|
return Factory::getResourceClass($this->descriptor); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function validateKeys() |
53
|
|
|
{ |
54
|
|
|
$resourceClass = $this->getResourceClass(); |
55
|
|
|
foreach ($this->descriptor->path as $dataSource) { |
56
|
|
|
foreach ($resourceClass::validateDataSource($dataSource, $this->basePath) as $error) { |
57
|
|
|
$this->errors[] = $error; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function getJsonSchemaFileFromRegistry($profile) |
63
|
|
|
{ |
64
|
|
|
if ($filename = Registry::getJsonSchemaFile($profile)) { |
65
|
|
|
return $filename; |
66
|
|
|
} else { |
67
|
|
|
return parent::getJsonSchemaFileFromRegistry($profile); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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: