Skip to content

Commit

Permalink
refactoring, added Repository and Factory classes, stream validation …
Browse files Browse the repository at this point in the history
…support (#6)

refactoring, added Repository and Factory classes, stream validation support
  • Loading branch information
OriHoch authored Apr 27, 2017
1 parent 4afa0f3 commit e62a464
Show file tree
Hide file tree
Showing 44 changed files with 3,263 additions and 324 deletions.
18 changes: 17 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ $ composer test

## Phpunit - for unit tests

[![Travis](https://travis-ci.org/frictionlessdata/datapackage-php.svg?branch=master)](https://travis-ci.org/frictionlessdata/datapackage-php)

Phpunit is used for unit tests, you can find the tests under tests directory

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

## Scrutinizer-ci - for code analysis

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

Scrutinizer-ci integrates with GitHub and runs on commits.

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

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.


## Publishing a release and updating Packagist

[![Packagist](https://img.shields.io/packagist/dm/frictionlessdata/datapackage.svg)](https://packagist.org/packages/frictionlessdata/datapackage)
[![SemVer](https://img.shields.io/badge/versions-SemVer-brightgreen.svg)](http://semver.org/)

* Publish a release (using SemVer) on GitHub
* go to https://packagist.org/packages/frictionlessdata/datapackage
* Login with GitHub which has permissions
* click "Update"
* all releases from GitHub appear as releases on Packagist
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ $ composer require frictionlessdata/datapackage
```php
use frictionlessdata\datapackage;

// get a datapackage object
$datapackage = datapackage\Factory::datapackage("tests/fixtures/multi_data_datapackage.json");

// iterate over the data
$datapackage = new datapackage\Datapackage("tests/fixtures/multi_data_datapackage.json");
foreach ($datapackage as $resource) {
print("-- ".$resource->name()." --");
$i = 0;
Expand All @@ -36,12 +38,12 @@ foreach ($datapackage as $resource) {
}
}

// validate the descriptor
$validationErrors = datapackage\Datapackage::validate("tests/fixtures/simple_invalid_datapackage.json");
// validate a datapackage descriptor
$validationErrors = datapackage\Factory::validate("tests/fixtures/simple_invalid_datapackage.json");
if (count($validationErrors) == 0) {
print("descriptor is valid");
} else {
print(datapackage\DatapackageValidationError::getErrorMessages($validationErrors));
print(datapackage\Validators\DatapackageValidationError::getErrorMessages($validationErrors));
}
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": ">=5.4",
"justinrainbow/json-schema": "^5.2",
"frictionlessdata/tableschema": "^0.1.2"
"frictionlessdata/tableschema": "^0.1.3"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35",
Expand Down
50 changes: 0 additions & 50 deletions src/DataStream.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/DataStreams/BaseDataStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace frictionlessdata\datapackage\DataStreams;

/**
* Provides a standard interface for streaming
*
* functionality could mostly be replaced by php generators (http://php.net/manual/en/language.generators.syntax.php)
* however, they are only supported on PHP 5.5 and above
*/
abstract class BaseDataStream implements \Iterator
{
/**
* @param string $dataSource
* @param mixed $dataSourceOptions
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
*/
abstract public function __construct($dataSource, $dataSourceOptions=null);

/**
* @return mixed
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
*/
abstract public function current();
}
56 changes: 56 additions & 0 deletions src/DataStreams/DefaultDataStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace frictionlessdata\datapackage\DataStreams;

use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;

/**
* streams the raw data without processing - used for default data package resources
*/
class DefaultDataStream extends BaseDataStream
{
/**
* @param string $dataSource
* @throws DataStreamOpenException
*/
public function __construct($dataSource, $dataSourceOptions=null)
{
try {
$this->fopenResource = fopen($dataSource, "r");
} catch (\Exception $e) {
throw new DataStreamOpenException("Failed to open data source ".json_encode($dataSource).": ".json_encode($e->getMessage()));
}
}

public function __destruct()
{
fclose($this->fopenResource);
}

public function rewind() {
if ($this->currentLineNumber == 0) {
// starting iterations
$this->currentLineNumber = 1;
} else {
throw new \Exception("DataStream does not support rewinding a stream, sorry");
}
}

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

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

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

public function valid() {
return (!feof($this->fopenResource));
}

protected $currentLineNumber = 0;
protected $fopenResource;
}
58 changes: 58 additions & 0 deletions src/DataStreams/TabularDataStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace frictionlessdata\datapackage\DataStreams;

use frictionlessdata\datapackage\Exceptions\DataStreamValidationException;
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
use frictionlessdata\tableschema\DataSources\CsvDataSource;
use frictionlessdata\tableschema\Schema;
use frictionlessdata\tableschema\Table;
use frictionlessdata\tableschema\Exceptions\TableRowValidationException;


class TabularDataStream extends BaseDataStream
{
public function __construct($dataSource, $schema=null)
{
if (empty($schema)) {
throw new \Exception("schema is required for tabular data stream");
} else {
try {
$dataSource = new CsvDataSource($dataSource);
$schema = new Schema($schema);
$this->table = new Table($dataSource, $schema);
} catch (\Exception $e) {
throw new DataStreamOpenException("Failed to open tabular data source ".json_encode($dataSource).": ".json_encode($e->getMessage()));
}
}
}

protected $table;

public function rewind() {
$this->table->rewind();
}

/**
* @return array
* @throws DataStreamValidationException
*/
public function current() {
try {
return $this->table->current();
} catch (TableRowValidationException $e) {
throw new DataStreamValidationException($e->getMessage());
}
}

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

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

public function valid() {
return $this->table->valid();
}
}
130 changes: 0 additions & 130 deletions src/Datapackage.php

This file was deleted.

Loading

0 comments on commit e62a464

Please sign in to comment.