Skip to content

Commit c073e9c

Browse files
author
Paul
committed
Added key propagation to CollectionMapper.
Changed CollectionMapper to return collection keys instead of renumbering *potential BC break*. Added InvalidRecordException thrown by CollectionMapper.
1 parent a008076 commit c073e9c

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

src/CollectionMapper.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ class CollectionMapper extends Mapper
1313
*
1414
* @return \Generator
1515
*
16-
* @throws \Exception
16+
* @throws InvalidRecordException A record in the specified collection was not an array type.
1717
*/
1818
public function mapCollection(\Iterator $collection, Mapping $mapping = null, $context = null)
1919
{
20-
foreach ($collection as $record) {
20+
foreach ($collection as $key => $record) {
2121
if (!is_array($record)) {
22-
throw new \Exception('Record must be an array.'); // TODO: Specific exception type.
22+
throw new InvalidRecordException('Record must be an array.');
2323
}
2424

25-
yield $mapping
26-
? $this->mapMapping($record, $mapping, $context)
25+
yield $key => $mapping
26+
? $this->mapMapping($record, $mapping, $context, $key)
2727
: $record;
2828
}
2929
}

src/InvalidRecordException.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace ScriptFUSION\Mapper;
3+
4+
/**
5+
* The exception that is thrown when an invalid record type is specified.
6+
*/
7+
class InvalidRecordException extends \RuntimeException
8+
{
9+
// Intentionally empty.
10+
}

test/Functional/KeyPropagationTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace ScriptFUSIONTest\Functional;
33

4+
use ScriptFUSION\Mapper\AnonymousMapping;
5+
use ScriptFUSION\Mapper\CollectionMapper;
46
use ScriptFUSION\Mapper\Mapper;
57
use ScriptFUSION\Mapper\Strategy\Collection;
68
use ScriptFUSION\Mapper\Strategy\Context;
@@ -49,4 +51,17 @@ public function testStrategyPropagation()
4951

5052
self::assertSame(['bar' => 'bar'], $mapped);
5153
}
54+
55+
/**
56+
* Tests that keys are forwarded by CollectionMapper.
57+
*/
58+
public function testCollectionMapperPropagation()
59+
{
60+
$mapped = (new CollectionMapper)->mapCollection(
61+
new \ArrayIterator(['foo' => ['bar']]),
62+
new AnonymousMapping([new CopyKey])
63+
);
64+
65+
self::assertSame(['foo' => ['foo']], iterator_to_array($mapped));
66+
}
5267
}

test/Integration/Mapper/CollectionMapperTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use ScriptFUSION\Mapper\AnonymousMapping;
55
use ScriptFUSION\Mapper\CollectionMapper;
6+
use ScriptFUSION\Mapper\InvalidRecordException;
67

78
final class CollectionMapperTest extends \PHPUnit_Framework_TestCase
89
{
@@ -14,6 +15,16 @@ protected function setUp()
1415
$this->mapper = new CollectionMapper;
1516
}
1617

18+
/**
19+
* Tests that when an invalid collection is specified an exception is thrown.
20+
*/
21+
public function testMapInvalidCollection()
22+
{
23+
$this->setExpectedException(InvalidRecordException::class);
24+
25+
$this->mapper->mapCollection(new \ArrayIterator(['foo']))->valid();
26+
}
27+
1728
/**
1829
* Tests that when there is nothing to map an invalid Generator is returned.
1930
*/
@@ -53,4 +64,17 @@ public function testMapCollection()
5364

5465
self::assertSame([$record, $record], iterator_to_array($mapped));
5566
}
67+
68+
/**
69+
* Tests that the keys of a mapped collection are preserved.
70+
*/
71+
public function testCollectionKeysPreserved()
72+
{
73+
$mapped = $this->mapper->mapCollection(
74+
new \ArrayIterator($input = ['foo' => ['bar']]),
75+
new AnonymousMapping(['baz'])
76+
);
77+
78+
self::assertSame(['foo' => ['baz']], iterator_to_array($mapped));
79+
}
5680
}

0 commit comments

Comments
 (0)