Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.1', '7.4']
php-versions: ['8.1', '8.3']

name: frictionlessdata/datapackage-php PHP ${{ matrix.php-versions }} test on ${{ matrix.operating-system }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v5

- name: Cache Composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
Expand All @@ -33,15 +33,15 @@ jobs:
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}
php_extensions: zip
php_extensions: zip json
version: 2
command: validate --strict

- name: Run Code Style Check for PHP ${{ matrix.php-versions }}
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}
php_extensions: zip
php_extensions: zip json
version: 2
dev: yes
command: style-check
Expand All @@ -50,7 +50,7 @@ jobs:
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}
php_extensions: zip
php_extensions: zip json
dev: yes
command: test

Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"description": "A utility library for working with Data Packages",
"license": "MIT",
"require": {
"php": ">=7.1",
"ext-zip": "*",
"justinrainbow/json-schema": "^5.2",
"frictionlessdata/tableschema": "^v1.0.0"
"php": ">=8",
"justinrainbow/json-schema": "^5.3 || ^6.6",
"frictionlessdata/tableschema": "^v1",
"ext-json": "*",
"ext-zip": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5.20",
"satooshi/php-coveralls": "^1.0",
"phpunit/phpunit": ">=7.5 <10.0",
"php-coveralls/php-coveralls": "^2.4",
"psy/psysh": "@stable",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/DataStreams/BaseDataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ abstract public function save($filename);
*
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
*/
abstract public function current();
abstract public function current():mixed;
}
13 changes: 8 additions & 5 deletions src/DataStreams/DefaultDataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function __destruct()
fclose($this->fopenResource);
}

public function rewind()
/**
* @throws \Exception
*/
public function rewind():void
{
if ($this->currentLineNumber == 0) {
// starting iterations
Expand All @@ -50,22 +53,22 @@ public function save($filename)
fclose($target);
}

public function current()
public function current():mixed
{
return fgets($this->fopenResource);
}

public function key()
public function key():mixed
{
return $this->currentLineNumber;
}

public function next()
public function next():void
{
++$this->currentLineNumber;
}

public function valid()
public function valid():bool
{
return !feof($this->fopenResource);
}
Expand Down
18 changes: 10 additions & 8 deletions src/DataStreams/TabularDataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class TabularDataStream extends BaseDataStream
public $table;
public $schema;

/**
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
* @throws \Exception
*/
public function __construct($dataSource, $dataSourceOptions = null)
{
parent::__construct($dataSource, $dataSourceOptions);
Expand All @@ -38,7 +42,7 @@ protected function getDataSourceObject()
return new CsvDataSource($this->dataSource);
}

public function rewind()
public function rewind():void
{
$this->table->rewind();
}
Expand All @@ -53,28 +57,26 @@ public function save($filename)
*
* @throws DataStreamValidationException
*/
public function current()
public function current():mixed
{
try {
return $this->table->current();
} catch (DataSourceException $e) {
throw new DataStreamValidationException($e->getMessage());
} catch (FieldValidationException $e) {
} catch (DataSourceException|FieldValidationException $e) {
throw new DataStreamValidationException($e->getMessage());
}
}

public function key()
public function key():mixed
{
return $this->table->key();
}

public function next()
public function next():void
{
$this->table->next();
}

public function valid()
public function valid(): bool
{
return $this->table->valid();
}
Expand Down
16 changes: 15 additions & 1 deletion src/DataStreams/TabularInlineDataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@

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 (array_sum(array_keys($data[0])) == array_sum(range(0, $numFields - 1))) {
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) {
Expand Down
40 changes: 32 additions & 8 deletions src/Datapackages/BaseDatapackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace frictionlessdata\datapackage\Datapackages;

use Exception;
use frictionlessdata\datapackage\Factory;
use frictionlessdata\datapackage\Package;
use frictionlessdata\datapackage\Registry;
use frictionlessdata\datapackage\Utils;
use frictionlessdata\datapackage\Validators\DatapackageValidator;
use frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException;
use frictionlessdata\datapackage\Exceptions\DatapackageInvalidSourceException;
use Iterator;
use ZipArchive;

abstract class BaseDatapackage implements \Iterator
abstract class BaseDatapackage implements Iterator
{

/**
Expand All @@ -34,6 +36,9 @@ public function __construct($descriptor, $basePath = null, $skipValidations = fa
}
}

/**
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
*/
public static function create($name, $resources, $basePath = null)
{
$datapackage = new static((object) [
Expand All @@ -47,6 +52,9 @@ public static function create($name, $resources, $basePath = null)
return $datapackage;
}

/**
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
*/
public function revalidate()
{
$this->rewind();
Expand Down Expand Up @@ -81,16 +89,23 @@ public function resources()
return $resources;
}

/**
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
* @throws \Exception
*/
public function getResource($name)
{
foreach ($this->descriptor->resources as $resourceDescriptor) {
if ($resourceDescriptor->name == $name) {
return $this->initResource($resourceDescriptor);
}
}
throw new \Exception("couldn't find matching resource with name = '{$name}'");
throw new Exception("couldn't find matching resource with name = '{$name}'");
}

/**
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
*/
public function addResource($name, $resource)
{
if (is_a($resource, 'frictionlessdata\\datapackage\\Resources\\BaseResource')) {
Expand Down Expand Up @@ -119,6 +134,10 @@ public function addResource($name, $resource)
}

// TODO: remove this function and use the getResource / addResource directly (will need to modify a lot of tests code)

/**
* @throws \Exception
*/
public function resource($name, $resource = null)
{
if ($resource) {
Expand All @@ -128,6 +147,9 @@ public function resource($name, $resource = null)
}
}

/**
* @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException
*/
public function removeResource($name)
{
$resourceDescriptors = [];
Expand All @@ -148,27 +170,30 @@ public function saveDescriptor($filename)
}

// standard iterator functions - to iterate over the resources
public function rewind()
public function rewind():void
{
$this->currentResourcePosition = 0;
}

public function current()
/**
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
*/
public function current():mixed
{
return $this->initResource($this->descriptor()->resources[$this->currentResourcePosition]);
}

public function key()
public function key():mixed
{
return $this->currentResourcePosition;
}

public function next()
public function next():void
{
++$this->currentResourcePosition;
}

public function valid()
public function valid():bool
{
return isset($this->descriptor()->resources[$this->currentResourcePosition]);
}
Expand Down Expand Up @@ -242,7 +267,6 @@ protected function copy()
* @param object $descriptor
*
* @return \frictionlessdata\datapackage\Resources\BaseResource
* @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException
*/
protected function initResource($descriptor)
{
Expand Down
9 changes: 4 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use frictionlessdata\datapackage\Datapackages\BaseDatapackage;
use frictionlessdata\datapackage\Resources\BaseResource;
use frictionlessdata\tableschema\SchemaValidationError;
use ZipArchive;

/**
Expand Down Expand Up @@ -39,9 +40,7 @@ public static function datapackage($source, $basePath = null)
$descriptor = $source->descriptor;
$basePath = $source->basePath;
$datapackageClass = static::getDatapackageClass($descriptor);
$datapackage = new $datapackageClass($descriptor, $basePath);

return $datapackage;
return new $datapackageClass($descriptor, $basePath);
}

/**
Expand Down Expand Up @@ -94,7 +93,7 @@ public static function validate($source, $basePath = null)
// return a list containing a single LOAD_FAILED validation error
return [
new Validators\DatapackageValidationError(
Validators\DatapackageValidationError::LOAD_FAILED,
SchemaValidationError::LOAD_FAILED,
$e->getMessage()
),
];
Expand Down Expand Up @@ -291,7 +290,7 @@ protected static function loadSource($source, $basePath)
$basePath = null;
}
} else {
// not a json string and not a url - assume it's a file path
// not a json string and not an url - assume it's a file path
if (static::isFileZipSource($source)) {
return static::loadFileZipSource($source);
} else {
Expand Down
Loading
Loading