Skip to content

Commit 640bbe6

Browse files
authored
Merge pull request #21 from OriHoch/master
[WIP] finishing up
2 parents 45435bf + 297a370 commit 640bbe6

67 files changed

Lines changed: 4443 additions & 1582 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
/.idea/
33
/coverage-clover.xml
44
/composer.lock
5+
/.php_cs
6+
/.php_cs.cache
7+
/php-cs-fixer

.install_cs_fixer.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
3+
if [ ! -f ./php-cs-fixer ]; then
4+
wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.3.2/php-cs-fixer.phar -O php-cs-fixer
5+
chmod +x php-cs-fixer
6+
fi

.php_cs.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* code style configuration
5+
* you may additionally create .php_cs file which will extend the configuration in this file
6+
*/
7+
8+
$finder = new PhpCsFixer\Finder();
9+
$config = new PhpCsFixer\Config('datapackage-php', 'datapackage-php style guide');
10+
$finder
11+
->exclude("tests/fixtures")
12+
->exclude("src/Validators/schemas")
13+
->in(__DIR__)
14+
;
15+
$config
16+
->setRules([
17+
'@PSR2' => true,
18+
'@Symfony' => true
19+
])
20+
->setFinder($finder)
21+
;
22+
return $config;

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ php:
66
- '7.0'
77
- '7.1'
88
- nightly
9-
- hhvm
109
before_script:
11-
- composer install --prefer-dist
10+
- COMPOSER_DISABLE_XDEBUG_WARN=1 composer install --prefer-dist
1211
script:
13-
- composer test
12+
- COMPOSER_DISABLE_XDEBUG_WARN=1 composer test
13+
- '[ "$TRAVIS_PHP_VERSION" != "7.1" ] || composer style-check'
1414
after_success:
1515
- '[ "$TRAVIS_PHP_VERSION" == "7.1" ] && vendor/bin/coveralls'
1616
dist: trusty

CONTRIBUTING.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ It does static code analysis and ensure confirmation to the coding stnadards.
4545

4646
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.
4747

48+
## php-cs-fixer - code style check & autofix
49+
50+
[php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) can be used to check and fix code style
51+
52+
you need to manually install it, then you can run : `composer style-check` or `composer style-fix`
4853

4954
## Publishing a release and updating Packagist
5055

README.md

Lines changed: 88 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,89 +10,119 @@
1010
A utility library for working with [Data Package](https://specs.frictionlessdata.io/data-package/) in PHP.
1111

1212

13-
## Getting Started
13+
## Features summary and Usage guide
1414

1515
### Installation
1616

1717
```bash
1818
$ composer require frictionlessdata/datapackage
1919
```
2020

21-
### Usage
21+
### Package
22+
23+
Load a data package conforming to the specs
2224

2325
```php
24-
use frictionlessdata\datapackage;
25-
26-
// get a datapackage object
27-
$datapackage = datapackage\Factory::datapackage("tests/fixtures/multi_data_datapackage.json");
28-
29-
// iterate over the data - it will raise exceptions in case of any problems
30-
foreach ($datapackage as $resource) {
31-
print("-- ".$resource->name()." --");
32-
$i = 0;
33-
foreach ($resource as $dataStream) {
34-
print("-dataStream ".++$i);
35-
foreach ($dataStream as $line) {
36-
print($line);
37-
}
26+
use frictionlessdata\datapackage\Package;
27+
$package = Package::load("tests/fixtures/multi_data_datapackage.json");
28+
```
29+
30+
Iterate over the resources and the data
31+
32+
```php
33+
foreach ($package as $resource) {
34+
echo $resource->name();
35+
foreach ($resource as $row) {
36+
echo $row;
3837
}
3938
}
39+
```
40+
41+
Get all the data as an array (loads all the data into memory, not recommended for large data sets)
4042

41-
// validate a datapackage descriptor
42-
$validationErrors = datapackage\Factory::validate("tests/fixtures/simple_invalid_datapackage.json");
43-
if (count($validationErrors) == 0) {
44-
print("descriptor is valid");
45-
} else {
46-
print(datapackage\Validators\DatapackageValidationError::getErrorMessages($validationErrors));
43+
```php
44+
foreach ($package as $resource) {
45+
var_dump($resource->read());
4746
}
47+
```
4848

49-
// get and manipulate resources
50-
$resources = $datapackage->resources();
51-
$resources["resource-name"]->name() == "resource-name"
52-
$resources["another-resource-name"] // BaseResource based object (e.g. DefaultResource / TabularResource)
49+
All data and schemas are validated and throws exceptions in case of any problems.
5350

54-
// get a single resource by name
55-
$datapackage->resource("resource-name")
51+
Validate the data explicitly and get a list of errors
5652

57-
// delete a resource by name - will raise exception in case of validation failure for the new descriptor
58-
$datapackage->deleteResource("resource-name");
53+
```php
54+
Package::validate("tests/fixtures/simple_invalid_datapackage.json"); // array of validation errors
55+
```
5956

60-
// add a resource - will raise exception in case of validation error for the new descriptor
61-
$resource = Factory::resource((object)[
62-
"name" => "new-resource", "data" => ["tests/fixtures/foo.txt", "tests/fixtures/baz.txt"]
63-
])
64-
$datapackage->addResource($resource);
57+
The package object has some useful methods to access and manipulate the resources
6558

66-
// register custom datapackage or resource classes which can override / extend core classes
67-
// these custom classes run a test against the schema to decide whether to handle a given descriptor or not
68-
Factory::registerDatapackageClass("my\\custom\\DatapackageClass");
69-
Factory::registerResourceClass("my\\custom\\ResourceClass");
59+
```php
60+
$package = Package::load("tests/fixtures/multi_data_datapackage.json");
61+
$package->resources(); // array of resource name => Resource object (see below for Resource class reference)
62+
$package->resource("first-resource"); // Resource object matching the given name
63+
$package->deleteResource("first-resource");
64+
// add a tabular resource
65+
$package->resource("tabular-resource-name", [
66+
"profile" => "tabular-data-resource",
67+
"schema" => [
68+
"fields" => [
69+
["name" => "id", "type" => "integer"],
70+
["name" => "name", "type" => "string"]
71+
]
72+
],
73+
"path" => [
74+
"tests/fixtures/simple_tabular_data.csv",
75+
]
76+
]);
77+
```
7078

71-
// register custom profiles and related schemas for validation
72-
Registry::registerSchema("my-custom-profile-id", "path/to/my-custom-profile.schema.json");
79+
Create a new package from scratch
7380

74-
// create a new datapackage from scratch
75-
$datapackage = TabularDatapackage::create("my-tabular-datapackage", [
76-
TabularResource::create("my-tabular-resource")
81+
```php
82+
$package = Package::create([
83+
"name" => "datapackage-name",
84+
"profile" => "tabular-data-package"
7785
]);
86+
// add a resource
87+
$package->resource("resource-name", [
88+
"profile" => "tabular-data-resource",
89+
"schema" => [
90+
"fields" => [
91+
["name" => "id", "type" => "integer"],
92+
["name" => "name", "type" => "string"]
93+
]
94+
],
95+
"path" => "tests/fixtures/simple_tabular_data.csv"
96+
]);
97+
// save the package descriptor to a file
98+
$package->saveDescriptor("datapackage.json");
99+
```
78100

79-
// set the tabular data schema
80-
$datapackage->resource("my-tabular-resource")->descriptor()->schema = (object)[
81-
"fields" => [
82-
(object)["name" => "id", "type" => "integer"],
83-
(object)["name" => "data", "type" => "string"],
84-
]
85-
];
101+
### Resource
102+
103+
Resource objects can be accessed from a Package as described above
104+
105+
```php
106+
$resource = $package->resource("resource-name")
107+
```
86108

87-
// add data files
88-
$datapackage->resource("my-tabular-resource")->descriptor()->data[] = "/path/to/file-1.csv";
89-
$datapackage->resource("my-tabular-resource")->descriptor()->data[] = "/path/to/file-2.csv";
109+
or instantiated directly
90110

91-
// re-validate the new descriptor
92-
$datapackage->revalidate();
111+
```php
112+
use frictionlessdata\datapackage\Resource;
113+
$resource = Resource::create([
114+
"name" => "my-resource",
115+
"profile" => "tabular-data-resource",
116+
"path" => "tests/fixtures/simple_tabular_data.csv",
117+
"schema" => ["fields" => [["name" => "id", "type" => "integer"], ["name" => "name", "type" => "string"]]]
118+
]);
119+
```
120+
121+
Iterating or reading over the resource produces combined rows from all the path or data elements
93122

94-
// save the datapackage descriptor to a file
95-
$datapackage->saveDescriptor("datapackage.json");
123+
```php
124+
foreach ($resource as $row) {}; // iterating
125+
$resource->read(); // get all the data as an array
96126
```
97127

98128

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
"require": {
66
"php": ">=5.4",
77
"justinrainbow/json-schema": "^5.2",
8-
"frictionlessdata/tableschema": "^0.1.5"
8+
"frictionlessdata/tableschema": "^0.1.6"
99
},
1010
"require-dev": {
1111
"phpunit/phpunit": "^4.8.35",
12-
"satooshi/php-coveralls": "^1.0"
12+
"satooshi/php-coveralls": "^1.0",
13+
"psy/psysh": "@stable"
1314
},
1415
"autoload": {
1516
"psr-4": {
@@ -18,6 +19,8 @@
1819
},
1920
"scripts": {
2021
"test": "phpunit --debug --coverage-clover coverage-clover.xml --bootstrap tests/autoload.php tests/",
21-
"update_registry": "php update_registry.php"
22+
"update_registry": "php update_registry.php",
23+
"style-check": "./.install_cs_fixer.sh && ./php-cs-fixer fix --dry-run --verbose --diff",
24+
"style-fix": "./.install_cs_fixer.sh && ./php-cs-fixer fix --verbose"
2225
}
2326
}

src/DataStreams/BaseDataStream.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
<?php
2+
23
namespace frictionlessdata\datapackage\DataStreams;
34

45
/**
5-
* Provides a standard interface for streaming from a data stream (Read-only)
6+
* Provides a standard interface for streaming from a data stream (Read-only).
67
*
78
* functionality could mostly be replaced by php generators (http://php.net/manual/en/language.generators.syntax.php)
89
* however, they are only supported on PHP 5.5 and above
910
*/
1011
abstract class BaseDataStream implements \Iterator
1112
{
13+
public $dataSource;
14+
public $dataSourceOptions;
15+
1216
/**
1317
* @param string $dataSource
14-
* @param mixed $dataSourceOptions
18+
* @param mixed $dataSourceOptions
19+
*
1520
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamOpenException
1621
*/
17-
abstract public function __construct($dataSource, $dataSourceOptions=null);
22+
public function __construct($dataSource, $dataSourceOptions = null)
23+
{
24+
$this->dataSource = $dataSource;
25+
$this->dataSourceOptions = $dataSourceOptions;
26+
}
1827

1928
/**
2029
* @return mixed
30+
*
2131
* @throws \frictionlessdata\datapackage\Exceptions\DataStreamValidationException
2232
*/
2333
abstract public function current();
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
<?php
2+
23
namespace frictionlessdata\datapackage\DataStreams;
34

45
use frictionlessdata\datapackage\Exceptions\DataStreamOpenException;
56

67
/**
7-
* streams the raw data without processing - used for default data package resources
8+
* streams the raw data without processing - used for default data package resources.
89
*/
910
class DefaultDataStream extends BaseDataStream
1011
{
12+
public $fopenResource;
13+
1114
/**
1215
* @param string $dataSource
16+
*
1317
* @throws DataStreamOpenException
1418
*/
15-
public function __construct($dataSource, $dataSourceOptions=null)
19+
public function __construct($dataSource, $dataSourceOptions = null)
1620
{
21+
parent::__construct($dataSource, $dataSourceOptions);
1722
try {
18-
$this->fopenResource = fopen($dataSource, "r");
23+
$this->fopenResource = fopen($this->dataSource, 'r');
1924
} catch (\Exception $e) {
20-
throw new DataStreamOpenException("Failed to open data source ".json_encode($dataSource).": ".json_encode($e->getMessage()));
25+
throw new DataStreamOpenException('Failed to open data source '.json_encode($this->dataSource).': '.json_encode($e->getMessage()));
2126
}
2227
}
2328

@@ -26,31 +31,35 @@ public function __destruct()
2631
fclose($this->fopenResource);
2732
}
2833

29-
public function rewind() {
34+
public function rewind()
35+
{
3036
if ($this->currentLineNumber == 0) {
3137
// starting iterations
3238
$this->currentLineNumber = 1;
3339
} else {
34-
throw new \Exception("DataStream does not support rewinding a stream, sorry");
40+
throw new \Exception('DataStream does not support rewinding a stream, sorry');
3541
}
3642
}
3743

38-
public function current() {
44+
public function current()
45+
{
3946
return fgets($this->fopenResource);
4047
}
4148

42-
public function key() {
49+
public function key()
50+
{
4351
return $this->currentLineNumber;
4452
}
4553

46-
public function next() {
47-
$this->currentLineNumber++;
54+
public function next()
55+
{
56+
++$this->currentLineNumber;
4857
}
4958

50-
public function valid() {
51-
return (!feof($this->fopenResource));
59+
public function valid()
60+
{
61+
return !feof($this->fopenResource);
5262
}
5363

5464
protected $currentLineNumber = 0;
55-
protected $fopenResource;
5665
}

0 commit comments

Comments
 (0)