Skip to content

Commit e62a464

Browse files
authored
refactoring, added Repository and Factory classes, stream validation support (#6)
refactoring, added Repository and Factory classes, stream validation support
1 parent 4afa0f3 commit e62a464

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3263
-324
lines changed

CONTRIBUTING.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ $ composer test
2121

2222
## Phpunit - for unit tests
2323

24+
[![Travis](https://travis-ci.org/frictionlessdata/datapackage-php.svg?branch=master)](https://travis-ci.org/frictionlessdata/datapackage-php)
25+
2426
Phpunit is used for unit tests, you can find the tests under tests directory
2527

2628
Running Phpunit directly: `vendor/bin/phpunit --bootstrap tests/autoload.php tests/`
@@ -35,8 +37,22 @@ when running `composer test` phpunit generates coverage report in coverage-clove
3537

3638
## Scrutinizer-ci - for code analysis
3739

38-
[Scrutinizer-ci](https://scrutinizer-ci.com/) integrates with GitHub and runs on commits.
40+
[![Scrutinizer-ci](https://scrutinizer-ci.com/g/OriHoch/datapackage-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/OriHoch/datapackage-php/)
41+
42+
Scrutinizer-ci integrates with GitHub and runs on commits.
3943

4044
It does static code analysis and ensure confirmation to the coding stnadards.
4145

4246
At the moment, the integration with frictionlessdata repo is not working, you can setup a Scrutinizer-ci account for your fork and run against that.
47+
48+
49+
## Publishing a release and updating Packagist
50+
51+
[![Packagist](https://img.shields.io/packagist/dm/frictionlessdata/datapackage.svg)](https://packagist.org/packages/frictionlessdata/datapackage)
52+
[![SemVer](https://img.shields.io/badge/versions-SemVer-brightgreen.svg)](http://semver.org/)
53+
54+
* Publish a release (using SemVer) on GitHub
55+
* go to https://packagist.org/packages/frictionlessdata/datapackage
56+
* Login with GitHub which has permissions
57+
* click "Update"
58+
* all releases from GitHub appear as releases on Packagist

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ $ composer require frictionlessdata/datapackage
2323
```php
2424
use frictionlessdata\datapackage;
2525

26+
// get a datapackage object
27+
$datapackage = datapackage\Factory::datapackage("tests/fixtures/multi_data_datapackage.json");
28+
2629
// iterate over the data
27-
$datapackage = new datapackage\Datapackage("tests/fixtures/multi_data_datapackage.json");
2830
foreach ($datapackage as $resource) {
2931
print("-- ".$resource->name()." --");
3032
$i = 0;
@@ -36,12 +38,12 @@ foreach ($datapackage as $resource) {
3638
}
3739
}
3840

39-
// validate the descriptor
40-
$validationErrors = datapackage\Datapackage::validate("tests/fixtures/simple_invalid_datapackage.json");
41+
// validate a datapackage descriptor
42+
$validationErrors = datapackage\Factory::validate("tests/fixtures/simple_invalid_datapackage.json");
4143
if (count($validationErrors) == 0) {
4244
print("descriptor is valid");
4345
} else {
44-
print(datapackage\DatapackageValidationError::getErrorMessages($validationErrors));
46+
print(datapackage\Validators\DatapackageValidationError::getErrorMessages($validationErrors));
4547
}
4648
```
4749

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": ">=5.4",
77
"justinrainbow/json-schema": "^5.2",
8-
"frictionlessdata/tableschema": "^0.1.2"
8+
"frictionlessdata/tableschema": "^0.1.3"
99
},
1010
"require-dev": {
1111
"phpunit/phpunit": "^4.8.35",

src/DataStream.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/DataStreams/BaseDataStream.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace frictionlessdata\datapackage\DataStreams;
3+
4+
/**
5+
* Provides a standard interface for streaming
6+
*
7+
* functionality could mostly be replaced by php generators (http://php.net/manual/en/language.generators.syntax.php)
8+
* however, they are only supported on PHP 5.5 and above
9+
*/
10+
abstract class BaseDataStream implements \Iterator
11+
{
12+
/**
13+
* @param string $dataSource
14+
* @param mixed $dataSourceOptions
15+
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
16+
*/
17+
abstract public function __construct($dataSource, $dataSourceOptions=null);
18+
19+
/**
20+
* @return mixed
21+
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
22+
*/
23+
abstract public function current();
24+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace frictionlessdata\datapackage\DataStreams;
3+
4+
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
5+
6+
/**
7+
* streams the raw data without processing - used for default data package resources
8+
*/
9+
class DefaultDataStream extends BaseDataStream
10+
{
11+
/**
12+
* @param string $dataSource
13+
* @throws DataStreamOpenException
14+
*/
15+
public function __construct($dataSource, $dataSourceOptions=null)
16+
{
17+
try {
18+
$this->fopenResource = fopen($dataSource, "r");
19+
} catch (\Exception $e) {
20+
throw new DataStreamOpenException("Failed to open data source ".json_encode($dataSource).": ".json_encode($e->getMessage()));
21+
}
22+
}
23+
24+
public function __destruct()
25+
{
26+
fclose($this->fopenResource);
27+
}
28+
29+
public function rewind() {
30+
if ($this->currentLineNumber == 0) {
31+
// starting iterations
32+
$this->currentLineNumber = 1;
33+
} else {
34+
throw new \Exception("DataStream does not support rewinding a stream, sorry");
35+
}
36+
}
37+
38+
public function current() {
39+
return fgets($this->fopenResource);
40+
}
41+
42+
public function key() {
43+
return $this->currentLineNumber;
44+
}
45+
46+
public function next() {
47+
$this->currentLineNumber++;
48+
}
49+
50+
public function valid() {
51+
return (!feof($this->fopenResource));
52+
}
53+
54+
protected $currentLineNumber = 0;
55+
protected $fopenResource;
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace frictionlessdata\datapackage\DataStreams;
3+
4+
use frictionlessdata\datapackage\Exceptions\DataStreamValidationException;
5+
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
6+
use frictionlessdata\tableschema\DataSources\CsvDataSource;
7+
use frictionlessdata\tableschema\Schema;
8+
use frictionlessdata\tableschema\Table;
9+
use frictionlessdata\tableschema\Exceptions\TableRowValidationException;
10+
11+
12+
class TabularDataStream extends BaseDataStream
13+
{
14+
public function __construct($dataSource, $schema=null)
15+
{
16+
if (empty($schema)) {
17+
throw new \Exception("schema is required for tabular data stream");
18+
} else {
19+
try {
20+
$dataSource = new CsvDataSource($dataSource);
21+
$schema = new Schema($schema);
22+
$this->table = new Table($dataSource, $schema);
23+
} catch (\Exception $e) {
24+
throw new DataStreamOpenException("Failed to open tabular data source ".json_encode($dataSource).": ".json_encode($e->getMessage()));
25+
}
26+
}
27+
}
28+
29+
protected $table;
30+
31+
public function rewind() {
32+
$this->table->rewind();
33+
}
34+
35+
/**
36+
* @return array
37+
* @throws DataStreamValidationException
38+
*/
39+
public function current() {
40+
try {
41+
return $this->table->current();
42+
} catch (TableRowValidationException $e) {
43+
throw new DataStreamValidationException($e->getMessage());
44+
}
45+
}
46+
47+
public function key() {
48+
return $this->table->key();
49+
}
50+
51+
public function next() {
52+
$this->table->next();
53+
}
54+
55+
public function valid() {
56+
return $this->table->valid();
57+
}
58+
}

src/Datapackage.php

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)