Skip to content

Commit 1db592c

Browse files
committed
Changed Mapper::map to only pass scalars through; foreign objects are now rejected.
1 parent 9a26e60 commit 1db592c

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scriptfusion/mapper",
3-
"description": "Maps data from one format to another using a DSL.",
3+
"description": "Maps data from one format to another using an object composition DSL.",
44
"authors": [
55
{
66
"name": "Bilge",
Lines changed: 10 additions & 0 deletions
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 mapper type is specified.
6+
*/
7+
class InvalidMapperTypeException extends \RuntimeException
8+
{
9+
// Intentionally empty.
10+
}

src/Mapper/Mapper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Mapper
1515
*
1616
* @return mixed
1717
*
18-
* @throws \Exception
18+
* @throws InvalidMapperTypeException An invalid strategy or mapping object was specified.
1919
*/
2020
public function map(array $record, $strategyOrMapping, $context = null)
2121
{
@@ -28,10 +28,12 @@ public function map(array $record, $strategyOrMapping, $context = null)
2828
} /* Mapping fragment. */
2929
elseif (is_array($strategyOrMapping)) {
3030
return $this->mapFragment($record, $strategyOrMapping, $context);
31+
} /* Scalar values. */
32+
elseif (is_scalar($strategyOrMapping)) {
33+
return $strategyOrMapping;
3134
}
3235

33-
// Pass unidentified object through.
34-
return $strategyOrMapping;
36+
throw new InvalidMapperTypeException('Invalid strategy or mapping: "' . get_class($strategyOrMapping) . '".');
3537
}
3638

3739
/**

test/Integration/Mapper/MapperTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
55
use ScriptFUSION\Mapper\AnonymousMapping;
6+
use ScriptFUSION\Mapper\InvalidMapperTypeException;
67
use ScriptFUSION\Mapper\Mapper;
78
use ScriptFUSION\Mapper\MapperAware;
89
use ScriptFUSION\Mapper\Strategy\Copy;
@@ -41,6 +42,37 @@ public function testMapFragment()
4142
self::assertSame(['bar' => 'foo'], $mapped);
4243
}
4344

45+
/**
46+
* @dataProvider provideScalars
47+
*/
48+
public function testMapScalars($scalar)
49+
{
50+
$mapped = $this->mapper->map([], $scalar);
51+
52+
self::assertSame($scalar, $mapped);
53+
}
54+
55+
public function provideScalars()
56+
{
57+
return [
58+
'string empty' => [''],
59+
'string non-empty' => ['foo'],
60+
'integer zero' => [0],
61+
'integer non-zero' => [-1],
62+
'float zero' => [0.],
63+
'float non-zero' => [-1.],
64+
'bool true' => [true],
65+
'bool false' => [false],
66+
];
67+
}
68+
69+
public function testMapInvalidObject()
70+
{
71+
$this->setExpectedException(InvalidMapperTypeException::class);
72+
73+
$this->mapper->map([], (object)[]);
74+
}
75+
4476
public function testInjectDependencies()
4577
{
4678
$this->mapper->map(

0 commit comments

Comments
 (0)