|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage\Datapackages; |
|
4
|
|
|
|
|
5
|
|
|
use frictionlessdata\datapackage\Factory; |
|
6
|
|
|
use frictionlessdata\datapackage\Registry; |
|
7
|
|
|
use frictionlessdata\datapackage\Utils; |
|
8
|
|
|
use frictionlessdata\datapackage\Validators\DatapackageValidator; |
|
9
|
|
|
use frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException; |
|
10
|
|
|
|
|
11
|
|
|
abstract class BaseDatapackage implements \Iterator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* BaseDatapackage constructor. |
|
15
|
|
|
* |
|
16
|
|
|
* @param object $descriptor |
|
17
|
|
|
* @param null|string $basePath |
|
18
|
|
|
* |
|
19
|
|
|
* @throws DatapackageValidationFailedException |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($descriptor, $basePath = null, $skipValidations = false) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->descriptor = $descriptor; |
|
24
|
|
|
$this->basePath = $basePath; |
|
25
|
|
|
$this->skipValidations = $skipValidations; |
|
26
|
|
|
if (!$this->skipValidations) { |
|
27
|
|
|
$this->revalidate(); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function create($name, $resources, $basePath = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$datapackage = new static((object) [ |
|
34
|
|
|
'name' => $name, |
|
35
|
|
|
'resources' => [], |
|
36
|
|
|
], $basePath, true); |
|
37
|
|
|
foreach ($resources as $resource) { |
|
38
|
|
|
$datapackage->addResource($resource); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $datapackage; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function revalidate() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->rewind(); |
|
47
|
|
|
$validationErrors = $this->datapackageValidate(); |
|
48
|
|
|
if (count($validationErrors) > 0) { |
|
49
|
|
|
throw new DatapackageValidationFailedException($validationErrors); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function handlesDescriptor($descriptor) |
|
54
|
|
|
{ |
|
55
|
|
|
return static::handlesProfile(Registry::getDatapackageValidationProfile($descriptor)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* returns the descriptor as-is, without adding default values or normalizing. |
|
60
|
|
|
* |
|
61
|
|
|
* @return object |
|
62
|
|
|
*/ |
|
63
|
|
|
public function descriptor() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->descriptor; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function resources() |
|
69
|
|
|
{ |
|
70
|
|
|
$resources = []; |
|
71
|
|
|
foreach ($this->descriptor->resources as $resourceDescriptor) { |
|
72
|
|
|
$resources[$resourceDescriptor->name] = $this->initResource($resourceDescriptor); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $resources; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function resource($name, $resource = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if ($resource) { |
|
81
|
|
|
if (is_a($resource, 'frictionlessdata\\datapackage\\Resources\\BaseResource')) { |
|
82
|
|
|
$resource = $resource->descriptor(); |
|
83
|
|
|
} else { |
|
84
|
|
|
$resource = Utils::objectify($resource); |
|
85
|
|
|
} |
|
86
|
|
|
$resource->name = $name; |
|
87
|
|
|
$resourceDescriptors = []; |
|
88
|
|
|
$gotMatch = false; |
|
89
|
|
|
foreach ($this->descriptor->resources as $resourceDescriptor) { |
|
90
|
|
|
if ($resourceDescriptor->name == $resource->name) { |
|
91
|
|
|
$resourceDescriptors[] = $resource; |
|
92
|
|
|
$gotMatch = true; |
|
93
|
|
|
} else { |
|
94
|
|
|
$resourceDescriptors[] = $resourceDescriptor; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
if (!$gotMatch) { |
|
98
|
|
|
$resourceDescriptors[] = $resource; |
|
99
|
|
|
} |
|
100
|
|
|
$this->descriptor->resources = $resourceDescriptors; |
|
101
|
|
|
if (!$this->skipValidations) { |
|
102
|
|
|
$this->revalidate(); |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
foreach ($this->descriptor->resources as $resourceDescriptor) { |
|
106
|
|
|
if ($resourceDescriptor->name == $name) { |
|
107
|
|
|
return $this->initResource($resourceDescriptor); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
throw new \Exception("couldn't find matching resource with name = '{$name}'"); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function deleteResource($name) |
|
115
|
|
|
{ |
|
116
|
|
|
$resourceDescriptors = []; |
|
117
|
|
|
foreach ($this->descriptor->resources as $resourceDescriptor) { |
|
118
|
|
|
if ($resourceDescriptor->name != $name) { |
|
119
|
|
|
$resourceDescriptors[] = $resourceDescriptor; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
$this->descriptor->resources = $resourceDescriptors; |
|
123
|
|
|
if (!$this->skipValidations) { |
|
124
|
|
|
$this->revalidate(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function saveDescriptor($filename) |
|
129
|
|
|
{ |
|
130
|
|
|
return file_put_contents($filename, json_encode($this->descriptor())); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// standard iterator functions - to iterate over the resources |
|
134
|
|
|
public function rewind() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->currentResourcePosition = 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function current() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->initResource($this->descriptor()->resources[$this->currentResourcePosition]); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function key() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->currentResourcePosition; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function next() |
|
150
|
|
|
{ |
|
151
|
|
|
++$this->currentResourcePosition; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function valid() |
|
155
|
|
|
{ |
|
156
|
|
|
return isset($this->descriptor()->resources[$this->currentResourcePosition]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected $descriptor; |
|
160
|
|
|
protected $currentResourcePosition = 0; |
|
161
|
|
|
protected $basePath; |
|
162
|
|
|
protected $skipValidations = false; |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* called by the resources iterator for each iteration. |
|
166
|
|
|
* |
|
167
|
|
|
* @param object $descriptor |
|
168
|
|
|
* |
|
169
|
|
|
* @return \frictionlessdata\datapackage\Resources\BaseResource |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function initResource($descriptor) |
|
172
|
|
|
{ |
|
173
|
|
|
return Factory::resource($descriptor, $this->basePath, $this->skipValidations); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
protected function datapackageValidate() |
|
177
|
|
|
{ |
|
178
|
|
|
return DatapackageValidator::validate($this->descriptor(), $this->basePath); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
protected static function handlesProfile($profile) |
|
182
|
|
|
{ |
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.