|
10 | 10 | A utility library for working with [Data Package](https://specs.frictionlessdata.io/data-package/) in PHP. |
11 | 11 |
|
12 | 12 |
|
13 | | -## Getting Started |
| 13 | +## Features summary and Usage guide |
14 | 14 |
|
15 | 15 | ### Installation |
16 | 16 |
|
17 | 17 | ```bash |
18 | 18 | $ composer require frictionlessdata/datapackage |
19 | 19 | ``` |
20 | 20 |
|
21 | | -### Usage |
| 21 | +### Package |
| 22 | + |
| 23 | +Load a data package conforming to the specs |
22 | 24 |
|
23 | 25 | ```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; |
38 | 37 | } |
39 | 38 | } |
| 39 | +``` |
| 40 | + |
| 41 | +Get all the data as an array (loads all the data into memory, not recommended for large data sets) |
40 | 42 |
|
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()); |
47 | 46 | } |
| 47 | +``` |
48 | 48 |
|
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. |
53 | 50 |
|
54 | | -// get a single resource by name |
55 | | -$datapackage->resource("resource-name") |
| 51 | +Validate the data explicitly and get a list of errors |
56 | 52 |
|
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 | +``` |
59 | 56 |
|
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 |
65 | 58 |
|
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 | +``` |
70 | 78 |
|
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 |
73 | 80 |
|
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" |
77 | 85 | ]); |
| 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 | +``` |
78 | 100 |
|
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 | +``` |
86 | 108 |
|
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 |
90 | 110 |
|
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 |
93 | 122 |
|
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 |
96 | 126 | ``` |
97 | 127 |
|
98 | 128 |
|
|
0 commit comments