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
Copy file name to clipboardExpand all lines: README.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Mapper
6
6
[![Test coverage][Coverage image]][Coverage]
7
7
[![Code style][Style image]][Style]
8
8
9
-
Mapper transforms arrays from one format to another using an object composition DSL. An application often receives data from a foreign source structured differently than it wants. We can use Mapper to transform data into a more suitable format by using a `Mapping` as shown in the following example.
9
+
Mapper transforms arrays from one format to another using an object composition DSL. An application often receives data from a foreign source structured differently than it wants. We can use Mapper to transform foreign data into a more suitable format for our application using a `Mapping` as shown in the following example.
10
10
11
11
```php
12
12
$mappedData = (new Mapper)->map($data, new MyMapping);
@@ -39,7 +39,7 @@ $barData = (new Mapper)->map($fooData, new FooToBarMapping);
39
39
40
40
> ['bar' => 123]
41
41
42
-
In this example we declare a mapping, `FooToBarMapping`, and pass it to the `Mapper::map` method to transform `$fooData` into `$barData`.
42
+
In this example we declare a mapping, `FooToBarMapping`, and pass it to the `Mapper::map` method to transform `$fooData` into `$barData`. As mentioned, this is just a contrived example to demonstrate how Mapper works; one may like to see a more [practical example](#practical-example).
43
43
44
44
This mapping introduces the `Copy` strategy that copies a value from the input data to the output. Strategies are just one type of *expression* we can specify as mapping values.
45
45
@@ -105,7 +105,7 @@ It is recommended to name custom strategies with a *Strategy* suffix to help dis
105
105
106
106
## Practical example
107
107
108
-
Suppose we receive two different address formats from two different third-party providers. The first provider, FooBook, provides a single UK addresses. The second provider, BarBucket, provides a collection of US addresses. We are tasked with converting both types to the same uniform address format for our application using mappings. Sample data from each provider is shown below.
108
+
Suppose we receive two different postal address formats from two different third-party providers. The first provider, FooBook, provides a single UK addresses. The second provider, BarBucket, provides a collection of US addresses. We are tasked with converting both types to the same uniform address format for our application using mappings.
109
109
110
110
The address format for our application must be a flat array with the following fields.
111
111
@@ -115,7 +115,7 @@ The address format for our application must be a flat array with the following f
115
115
* postcode
116
116
* country
117
117
118
-
### FooBook
118
+
### FooBook address mapping
119
119
120
120
A sample of the data we receive from FooBook is shown below.
121
121
@@ -167,7 +167,7 @@ $address = (new Mapper)->map($fooBookAddress, new FooBookAddressToAddresesMappin
167
167
]
168
168
```
169
169
170
-
### BarBucket
170
+
### BarBucket address mapping
171
171
172
172
A sample of the data we receive from BarBucket is show below.
173
173
@@ -702,19 +702,19 @@ Requirements
702
702
-[PHP 5.5](http://php.net/)
703
703
-[Composer](https://getcomposer.org/)
704
704
705
-
Testing
706
-
-------
707
-
708
-
Mapper is fully unit tested. Run the tests with `bin/test` from a shell. All examples
709
-
in this document can be found in `DocumentationTest`.
710
-
711
705
Limitations
712
706
-----------
713
707
714
708
- Strategies do not know the name of the key they are assigned to because `Mapper` does not forward the key name.
715
709
- Strategies do not know where they sit in a `Mapping` and therefore cannot traverse a mapping relative to their position.
716
710
- The `Collection` strategy overwrites context making any previous context inaccessible to descendants.
717
711
712
+
Testing
713
+
-------
714
+
715
+
Mapper is fully unit tested. Run the tests with `bin/test` from a shell. All examples
716
+
in this document can be found in `DocumentationTest`.
0 commit comments