Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added syntactic sugar for extending mappings #79

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Transfers data from one object to another, allowing custom mapping operations.
* [ReverseMap](#reversemap)
* [Copying a mapping](#copying-a-mapping)
* [Automatic creation of mappings](#automatic-creation-of-mappings)
* [Extending mappings](#extending-mappings)
* [Resolving property names](#resolving-property-names)
* [Naming conventions](#naming-conventions)
* [Explicitly state source property](#explicitly-state-source-property)
Expand Down Expand Up @@ -442,6 +443,21 @@ $config->getOptions()->createUnregisteredMappings();
With this configuration the mapper will generate a very basic mapping on the
fly instead of throwing an exception if the mapping is not configured.

### Extending mappings
There are some cases when you want to use unregistered mappings but you want to setup
mapping just for one or few properties of the mapped class without need to define mapping for
each property. In this case you can simply extend mapping and define just those properties
that you need to have mapped differently:

```php
<?php

$config->extendMapping(Employee::class, EmployeeListView::class)
->forMember('name', function (Employee $employee) {
return strtoupper($employee->name);
});
```

### Resolving property names
Unless you define a specific way to fetch a value (e.g. `mapFrom`), the mapper
has to have a way to know which source property to map from. By default, it will
Expand Down
16 changes: 16 additions & 0 deletions src/Configuration/AutoMapperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ public function registerMapping(
return $mapping;
}

/**
* @inheritdoc
*/
public function extendMapping(
string $sourceClassName,
string $destinationClassName
): MappingInterface {
if (!$this->hasMappingFor($sourceClassName, $destinationClassName)) {
throw new \RuntimeException("Undefined mapping for class $sourceClassName");
}

$mapping = $this->getMappingFor($sourceClassName, $destinationClassName);

return $this->registerMapping($sourceClassName, $destinationClassName)->copyFromMapping($mapping);
}

/**
* @inheritdoc
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Configuration/AutoMapperConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public function registerMapping(
string $destinationClassName
): MappingInterface;

/**
* Extends already registered mapping. Whether it's unregistered type
* of mapping or manually registered mapping.
*
* @param string $sourceClassName
* @param string $destinationClassName
* @return MappingInterface
*/
public function extendMapping(
string $sourceClassName,
string $destinationClassName
): MappingInterface;

/**
* @return Options
*/
Expand Down
34 changes: 34 additions & 0 deletions test/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,40 @@ public function testItCanMapWithACallback()
$this->assertEquals('NewName', $destination->name);
}

public function testItCanExtendMapping()
{
$this->config->registerMapping(Source::class, Destination::class);
$mapper = new AutoMapper($this->config);
$source = new Source();
$source->name = 'John';
$destination = $mapper->map($source, Destination::class);

$this->assertEquals('John', $destination->name);

$this->config->extendMapping(Source::class, Destination::class)
->forMember('name', function (Source $source) {
return $source->name . ' ' . 'Doe';
});
$destination = $mapper->map($source, Destination::class);

$this->assertEquals('John Doe', $destination->name);
}

public function testItThrowsRuntimeErrorOnExtendMappingWhenDoesntHaveMapping()
{
$this->expectException(\RuntimeException::class);

$this->config->getOptions()->dontCreateUnregisteredMappings();
$mapper = new AutoMapper($this->config);
$this->config->extendMapping(Source::class, Destination::class)
->forMember('name', function (Source $source) {
return $source->name . ' ' . 'Doe';
});
$source = new Source();
$source->name = 'John';
$mapper->map($source, Destination::class);
}

public function testTheConfigurationCanBeRetrieved()
{
$config = new AutoMapperConfig();
Expand Down