You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1.[Understanding this manual](#understanding-this-manual)
27
24
1.[Quick start](#quick-start)
25
+
1.[Understanding this manual](#understanding-this-manual)
28
26
1.[Usage](#usage)
29
27
1.[Porter's API](#porters-api)
28
+
1.[Overview](#overview)
30
29
1.[Import specifications](#import-specifications)
31
30
1.[Record collections](#record-collections)
32
31
1.[Transformers](#transformers)
@@ -46,27 +45,71 @@ Contents
46
45
Benefits
47
46
--------
48
47
49
-
Here are some of the benefits of using Porter to import data into your application.
50
-
51
48
* Formally defines a structured data import framework with the following concepts: [providers](#providers) represent one or more [resources](#resources) that fetch data from [connectors](#connectors).
52
49
* Provides efficient in-memory data processing interfaces to handle large data sets one record at a time, via iterators, which can be implemented using generators.
53
50
* Offers post-import [transformations](#transformers), such as [filtering](#filtering) and [mapping][MappingTransformer], to transform third-party into data useful for first-party applications.
54
51
* Protects against intermittent network failures with [durability](#durability) features.
55
52
* Supports PSR-6 [caching](#caching), at the connector level, for each fetch operation.
56
53
* Joins two or more linked data sets together using [sub-imports][Sub-imports] automatically.
57
54
58
-
Overview
59
-
--------
55
+
Quick start
56
+
-----------
60
57
61
-
The following data flow diagram gives a high level overview of Porter's main interfaces and the data flows between them when importing data. Note that we use the term *resource* for brevity, but the actual interface is called `ProviderResource`, because *resource* is a reserved word in PHP. Also note, I don't know how to draw data flow diagrams, so just go with it.
58
+
This quick start guide will walk through getting up and running with Porter from scratch and assumes you already have a PHP environment set up with Composer. Let's start by initializing our Composer file by running `composer init` in our project's root directory and accepting the defaults. We can skip defining dependencies interactively because we'll issue separate commands in a moment.
62
59
63
-
<divalign="center">
60
+
Let's start with the [European Central Bank][ECB provider] (ECB) provider by including it in our `composer.json` with the following command.
64
61
65
-
![Data flow diagram][Data flow diagram]
62
+
```sh
63
+
composer require provider/european-central-bank
64
+
```
66
65
67
-
</div>
66
+
We now have the provider installed along with all its dependencies, including Porter herself. We want to create a `new Porter` instance now, but we need to pass a `ContainerInterface` to her constructor. Any PSR-11 container is valid, but let's use Joomla DI for now.
68
67
69
-
Our application calls `Porter::import()` with an `ImportSpecification` and receives `PorterRecords` in return. Everything else happens internally and we don't need to worry about it unless writing custom providers and resources.
68
+
```sh
69
+
composer require joomla/di
70
+
```
71
+
72
+
Create a new container and register an instance of `EuropeanCentralBankProvider` with it. Pass the container to a new Porter instance. Don't forget to include the autoloader!
73
+
74
+
```php
75
+
use Joomla\DI\Container;
76
+
use ScriptFUSION\Porter\Porter;
77
+
use ScriptFUSION\Porter\Provider\EuropeanCentralBank\Provider\EuropeanCentralBankProvider;
78
+
79
+
require 'vendor/autoload.php';
80
+
81
+
$container = new Container;
82
+
$container->set(EuropeanCentralBankProvider::class, new EuropeanCentralBankProvider);
83
+
84
+
$porter = new Porter($container);
85
+
```
86
+
87
+
We're now ready to import any of the ECB's resources. Let's import the latest daily foreign exchange rates provided by `DailyForexRates`. Porter's `import()` method requires an `ImportSpecification` which accepts the resource we want to import.
Porter returns an iterator so we can now loop over the rates and print them out.
94
+
95
+
```php
96
+
foreach ($rates as $rate) {
97
+
echo "$rate[currency]: $rate[rate]\n";
98
+
}
99
+
```
100
+
101
+
This outputs something similar to the following, with today's current rates.
102
+
103
+
>USD: 1.2304
104
+
JPY: 131.66
105
+
BGN: 1.9558
106
+
CZK: 25.357
107
+
DKK: 7.4469
108
+
...
109
+
110
+
Since these rates come from the European Central Bank, they're relative to the Euro (EUR), which is assumed to always be `1`. We can use this information to write a currency converter that's always up-to-date with the latest exchange rates.
111
+
112
+
This just scratches the surface of Porter without going into any details. Explore the rest of this manual at your leisure to gain a fuller understanding of the features at your disposal.
70
113
71
114
Understanding this manual
72
115
-------------------------
@@ -75,13 +118,6 @@ The first half of this manual covers Porter's main features and how to use them.
75
118
76
119
Text marked as `inline code` denotes literal code, as it would appear in a PHP file. For example, `Porter` refers specifically to the class of the same name within this library, whereas *Porter* refers to this entire project as a whole.
77
120
78
-
Quick start
79
-
-----------
80
-
81
-
The quickest way to start is with a [ready-made provider][Provider]. [Usage](#usage) shows how to register a provider and import data from its resources. Even if there isn't an existing provider you want to use, it's still recommended to try out an existing one first, to get comfortable with Porter before writing your own.
82
-
83
-
If a provider does not yet exist for the service we want to use, we must write one from scratch. This isn't as quick, but don't worry, it's pretty easy! Start by writing the [provider](#providers) that supplies the appropriate connector to access the service. Next, define a [resource](#resources) that fetches data using the connector and `yield` it as an `array`. For more information, see [writing a provider](#writing-a-provider) and [writing a resource](#writing-a-resource).
84
-
85
121
Usage
86
122
-----
87
123
@@ -125,6 +161,19 @@ Porter's API
125
161
*`import(ImportSpecification) : PorterRecords|CountablePorterRecords`– Imports data according to the design of the specified import specification.
126
162
*`importOne(ImportSpecification) : ?array`– Imports one record according to the design of the specified import specification. If more than one record is imported, `ImportException` is thrown. Use this when you're sure a provider just returns a single record.
127
163
164
+
Overview
165
+
--------
166
+
167
+
The following data flow diagram gives a high level overview of Porter's main interfaces and the data flows between them when importing data. Note that we use the term *resource* for brevity, but the actual interface is called `ProviderResource`, because *resource* is a reserved word in PHP. Also note, I don't know how to draw data flow diagrams, so just go with it.
168
+
169
+
<divalign="center">
170
+
171
+
![Data flow diagram][Data flow diagram]
172
+
173
+
</div>
174
+
175
+
Our application calls `Porter::import()` with an `ImportSpecification` and receives `PorterRecords` in return. Everything else happens internally and we don't need to worry about it unless writing custom providers and resources.
0 commit comments