Skip to content

Commit

Permalink
Merge pull request #39 from kozlice/map-array-to-object
Browse files Browse the repository at this point in the history
Allow mapping of an array into an existing object
  • Loading branch information
mark-gerarts committed Jun 15, 2019
2 parents 40a69b3 + 7bed9b2 commit e0a411b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/AutoMapper.php
Expand Up @@ -8,6 +8,7 @@
use AutoMapperPlus\Exception\AutoMapperPlusException;
use AutoMapperPlus\Exception\InvalidArgumentException;
use AutoMapperPlus\Exception\UnregisteredMappingException;
use AutoMapperPlus\Exception\UnsupportedSourceTypeException;
use AutoMapperPlus\MappingOperation\ContextAwareOperation;
use AutoMapperPlus\MappingOperation\MapperAwareOperation;

Expand Down Expand Up @@ -59,11 +60,9 @@ public function map($source, string $destinationClass, array $context = [])
$sourceClass = \get_class($source);
}
else {
$sourceClass= \gettype($source);
$sourceClass = \gettype($source);
if ($sourceClass !== DataType::ARRAY) {
throw new AutoMapperPlusException(
'Mapping from something else than an object or array is not supported yet.'
);
throw UnsupportedSourceTypeException::fromType($sourceClass);
}
}

Expand Down Expand Up @@ -117,10 +116,19 @@ public function mapMultiple(
*/
public function mapToObject($source, $destination, array $context = [])
{
$sourceClassName = \get_class($source);
$destinationClassName = \get_class($destination);
if (\is_object($source)) {
$sourceClass = \get_class($source);
}
else {
$sourceClass = \gettype($source);
if ($sourceClass !== DataType::ARRAY) {
throw UnsupportedSourceTypeException::fromType($sourceClass);
}
}

$mapping = $this->getMapping($sourceClassName, $destinationClassName);
$destinationClass = \get_class($destination);

$mapping = $this->getMapping($sourceClass, $destinationClass);
if ($mapping->providesCustomMapper()) {
return $this->getCustomMapper($mapping)->mapToObject($source, $destination, [
self::DESTINATION_CONTEXT => $destination,
Expand Down
17 changes: 17 additions & 0 deletions src/Exception/UnsupportedSourceTypeException.php
@@ -0,0 +1,17 @@
<?php

namespace AutoMapperPlus\Exception;

class UnsupportedSourceTypeException extends AutoMapperPlusException
{
/**
* @param string $type
* @return UnsupportedSourceTypeException
*/
public static function fromType($type): UnsupportedSourceTypeException
{
$message = sprintf('Expected object or array as a source, got %s.', $type);

return new static($message);
}
}
6 changes: 3 additions & 3 deletions src/MapperInterface.php
Expand Up @@ -15,7 +15,7 @@ interface MapperInterface
* Maps an object to an instance of class $to, provided a mapping is
* configured.
*
* @param $source
* @param array|object $source
* The source object.
* @param string $targetClass
* The target classname.
Expand All @@ -34,9 +34,9 @@ public function map($source, string $targetClass/**, array $context = [] */);
/**
* Maps properties of object $from to an existing object $to.
*
* @param $source
* @param array|object $source
* The source object.
* @param $destination
* @param object $destination
* The target object.
* @param array $context
* See MapperInterface::map()
Expand Down

0 comments on commit e0a411b

Please sign in to comment.