Skip to content

Commit

Permalink
Merge pull request #38 from gabbanaesteban/add-restriction-to-map-mul…
Browse files Browse the repository at this point in the history
…tiple

Add validation to mapMultiple operation
  • Loading branch information
mark-gerarts authored May 20, 2019
2 parents fd62a84 + 28e4163 commit 40a69b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;
use AutoMapperPlus\Configuration\MappingInterface;
use AutoMapperPlus\Exception\AutoMapperPlusException;
use AutoMapperPlus\Exception\InvalidArgumentException;
use AutoMapperPlus\Exception\UnregisteredMappingException;
use AutoMapperPlus\MappingOperation\ContextAwareOperation;
use AutoMapperPlus\MappingOperation\MapperAwareOperation;
Expand Down Expand Up @@ -96,6 +97,13 @@ public function mapMultiple(
string $destinationClass,
array $context = []
): array {

if(!is_iterable($sourceCollection)){
throw new InvalidArgumentException(
'The collection provided should be iterable.'
);
}

$mappedResults = [];
foreach ($sourceCollection as $source) {
$mappedResults[] = $this->map($source, $destinationClass, $context);
Expand Down
12 changes: 12 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace AutoMapperPlus\Exception;

/**
* Class InvalidArgumentException
*
* @package AutoMapperPlus\Exception
*/
class InvalidArgumentException extends AutoMapperPlusException
{
}
13 changes: 13 additions & 0 deletions test/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use AutoMapperPlus\Test\Models\Visibility\Visibility;
use AutoMapperPlus\Test\Models\SimilarPropertyNames\Source as SimilarSource;
use AutoMapperPlus\Test\Models\SimilarPropertyNames\Destination as SimilarDestination;
use AutoMapperPlus\Exception\InvalidArgumentException;

/**
* Class AutoMapperTest
Expand Down Expand Up @@ -721,4 +722,16 @@ public function testMappingsCanBeGeneratedOnTheFlyIfOptionIsSet()

$this->assertEquals('a name', $result->name);
}

public function testAnExceptionIsThrownForNoIterableSourceInMultpleMappings()
{
$this->expectException(InvalidArgumentException::class);

$this->config->registerMapping(Source::class, Destination::class);
$mapper = new AutoMapper($this->config);

$sourceCollection = new \stdClass();

$mapper->mapMultiple($sourceCollection, Destination::class);
}
}

0 comments on commit 40a69b3

Please sign in to comment.