|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace frictionlessdata\datapackage\Resources; |
|
4
|
|
|
|
|
5
|
|
|
use frictionlessdata\datapackage\DataStreams\TabularDataStream; |
|
6
|
|
|
use frictionlessdata\datapackage\DataStreams\TabularInlineDataStream; |
|
7
|
|
|
use frictionlessdata\datapackage\Registry; |
|
8
|
|
|
|
|
9
|
|
|
class TabularResource extends DefaultResource |
|
10
|
|
|
{ |
|
11
|
|
|
public function schema() |
|
12
|
|
|
{ |
|
13
|
|
|
// TODO: change to table schema object |
|
14
|
|
|
return $this->descriptor()->schema; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function getFileExtension() |
|
18
|
|
|
{ |
|
19
|
|
|
return '.csv'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $dataSource |
|
24
|
|
|
* |
|
25
|
|
|
* @return TabularDataStream |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function getDataStream($dataSource, $dataSourceOptions = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$dataSourceOptions = array_merge([ |
|
30
|
|
|
'schema' => $this->schema(), |
|
31
|
|
|
'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null, |
|
32
|
|
|
], (array) $dataSourceOptions); |
|
33
|
|
|
|
|
34
|
|
|
return new TabularDataStream($this->normalizeDataSource($dataSource, $this->basePath), $dataSourceOptions); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function getInlineDataStream($data) |
|
38
|
|
|
{ |
|
39
|
|
|
return new TabularInlineDataStream($data, [ |
|
40
|
|
|
'schema' => $this->schema(), |
|
41
|
|
|
'dialect' => isset($this->descriptor()->dialect) ? $this->descriptor()->dialect : null, |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function handlesDescriptor($descriptor) |
|
46
|
|
|
{ |
|
47
|
|
|
return ( |
|
48
|
|
|
Registry::getResourceValidationProfile($descriptor) == 'tabular-data-resource' |
|
49
|
|
|
|| (isset($descriptor->format) && in_array($descriptor->format, ['csv', 'tsv'])) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.