-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTabularInlineDataStream.php
More file actions
50 lines (44 loc) · 1.64 KB
/
TabularInlineDataStream.php
File metadata and controls
50 lines (44 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace frictionlessdata\datapackage\DataStreams;
use frictionlessdata\tableschema\DataSources\NativeDataSource;
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
class TabularInlineDataStream extends TabularDataStream
{
/**
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
*/
protected function getDataSourceObject()
{
$data = json_decode(json_encode($this->dataSource), true);
if (is_array($data)) {
$numFields = count($this->schema->fields());
$objRows = [];
if (!function_exists('array_is_list')) {
function array_is_list(array $arr):bool
{
if ($arr === []) {
return true;
}
return array_keys($arr) === range(0, count($arr) - 1);
}
}
if (array_is_list($data[0]) && (count($data[0])) == $numFields) {
// Row Arrays - convert to Row Objects
$header = array_shift($data);
foreach ($data as $row) {
$objRow = [];
foreach ($header as $fieldOrder => $fieldName) {
$objRow[$fieldName] = $row[$fieldOrder];
}
$objRows[] = $objRow;
}
} else {
// Row Objects - no processing needed
$objRows = $data;
}
return new NativeDataSource($objRows);
} else {
throw new DataStreamOpenException('inline tabular data must be an array');
}
}
}