Skip to content

Commit 443cb44

Browse files
author
Paul
committed
Added CopyKey documentation and DocumentationTest case.
1 parent 674b4aa commit 443cb44

3 files changed

Lines changed: 76 additions & 23 deletions

File tree

README.md

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,29 @@ Contents
1919
--------
2020

2121
1. [Mappings](#mappings)
22-
2. [Strategies](#strategies)
23-
3. [Practical example](#practical-example)
24-
4. [Strategy reference](#strategy-reference)
22+
1. [Strategies](#strategies)
23+
1. [Practical example](#practical-example)
24+
1. [Strategy reference](#strategy-reference)
2525
1. [Copy](#copy)
26-
2. [CopyContext](#copycontext)
27-
3. [Callback](#callback)
28-
4. [Collection](#collection)
29-
5. [Context](#context)
30-
6. [Either](#either)
31-
7. [Filter](#filter)
32-
8. [Flatten](#flatten)
33-
9. [IfExists](#ifexists)
34-
10. [Merge](#merge)
35-
11. [TakeFirst](#takefirst)
36-
12. [ToList](#tolist)
37-
13. [TryCatch](#trycatch)
38-
14. [Type](#type)
39-
15. [Unique](#unique)
40-
16. [Walk](#walk)
41-
5. [Requirements](#requirements)
42-
6. [Limitations](#limitations)
43-
7. [Testing](#testing)
26+
1. [CopyContext](#copycontext)
27+
1. [CopyKey](#copykey)
28+
1. [Callback](#callback)
29+
1. [Collection](#collection)
30+
1. [Context](#context)
31+
1. [Either](#either)
32+
1. [Filter](#filter)
33+
1. [Flatten](#flatten)
34+
1. [IfExists](#ifexists)
35+
1. [Merge](#merge)
36+
1. [TakeFirst](#takefirst)
37+
1. [ToList](#tolist)
38+
1. [TryCatch](#trycatch)
39+
1. [Type](#type)
40+
1. [Unique](#unique)
41+
1. [Walk](#walk)
42+
1. [Requirements](#requirements)
43+
1. [Limitations](#limitations)
44+
1. [Testing](#testing)
4445

4546
Mappings
4647
--------
@@ -281,6 +282,7 @@ The following strategies ship with Mapper and provide a suite of commonly used f
281282

282283
- [Copy](#copy) – Copies a portion of input data.
283284
- [CopyContext](#copycontext) – Copies a portion of context data.
285+
- [CopyKey](#copykey) – Copies the current key.
284286

285287
#### Augmenters
286288

@@ -358,6 +360,34 @@ $context = ['foo' => 456];
358360

359361
> 456
360362
363+
### CopyKey
364+
365+
Copies the current key from the key context. This strategy requires the key context to be set by another strategy. By default the key context is `null`. Currently only the [collection strategy](#collection) sets a key context.
366+
367+
#### Signature
368+
369+
```php
370+
CopyKey()
371+
```
372+
373+
#### Example
374+
375+
```php
376+
(new Mapper)->map(
377+
[
378+
'foo' => [
379+
'bar' => 'baz',
380+
],
381+
],
382+
new Collection(
383+
new Copy('foo'),
384+
new CopyKey
385+
)
386+
)
387+
```
388+
389+
> ['bar' => 'bar']
390+
361391
### Callback
362392

363393
Augments data using the return value of the specified callback.
@@ -397,6 +427,8 @@ Callback(callable $callback)
397427

398428
Maps a collection of data by applying a transformation to each datum using a callback. The data collection must be an expression that maps to an array otherwise null is returned.
399429

430+
For each item in the collection, this strategy sets the context to the current datum and the key context to the current key, which can be retrieved using [CopyKey](#copykey).
431+
400432
#### Signature
401433

402434
```php

src/Mapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
class Mapper
1010
{
1111
/**
12-
* Maps the specified record according to the specified expression type. May be called recursively if the expression
13-
* embeds other expressions. If context is specified it is passed to the expression and any descendant expressions.
12+
* Maps the specified record according to the specified expression type. Optionally, used-defined contextual data
13+
* may be passed to the expression. The record key is for internal use only and represents the current array key.
14+
*
15+
* May be called recursively if the expression embeds more expressions.
1416
*
1517
* @param array $record Record.
1618
* @param Strategy|Mapping|array|mixed $expression Expression.

test/Functional/DocumentationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ScriptFUSION\Mapper\Strategy\Context;
99
use ScriptFUSION\Mapper\Strategy\Copy;
1010
use ScriptFUSION\Mapper\Strategy\CopyContext;
11+
use ScriptFUSION\Mapper\Strategy\CopyKey;
1112
use ScriptFUSION\Mapper\Strategy\Either;
1213
use ScriptFUSION\Mapper\Strategy\Filter;
1314
use ScriptFUSION\Mapper\Strategy\Flatten;
@@ -104,6 +105,24 @@ public function testCopyContext()
104105
self::assertSame(456, (new Mapper)->map($data, new CopyContext('foo'), $context));
105106
}
106107

108+
public function testCopyKey()
109+
{
110+
self::assertSame(
111+
['bar' => 'bar'],
112+
(new Mapper)->map(
113+
[
114+
'foo' => [
115+
'bar' => 'baz',
116+
],
117+
],
118+
new Collection(
119+
new Copy('foo'),
120+
new CopyKey
121+
)
122+
)
123+
);
124+
}
125+
107126
public function testCallback()
108127
{
109128
self::assertSame(

0 commit comments

Comments
 (0)