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

Add fromMethod operation #69

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class Employee
{
return $this->id;
}

public function getFullName()
{
return $this->firstName .' '. $this->lastName;
}

// And so on...
}
Expand All @@ -92,6 +97,7 @@ class EmployeeDto
public $firstName;
public $lastName;
public $age;
public $fullName;
}
```

Expand Down Expand Up @@ -219,6 +225,7 @@ The following operations are provided:
| Ignore | Ignores the property. |
| MapTo | Maps the property to another class. Allows for [nested mappings](#dealing-with-nested-mappings). Supports both single values and collections. |
| FromProperty | Use this to explicitly state the source property name. |
| FromMethod | Use this to map your value from a **public** method. This Operation does not support reverse mapping |
| DefaultMappingOperation | Simply transfers the property, taking into account the provided naming conventions (if there are any). |
| SetTo | Always sets the property to the given value |

Expand All @@ -243,7 +250,9 @@ $mapping->forMember('id', Operation::ignore());
$mapping->forMember('employee', Operation::mapTo(EmployeeDto::class));
// Explicitly state what the property name is of the source object.
$mapping->forMember('name', Operation::fromProperty('unconventially_named_property'));
// The `FromProperty` operation can be chained with `MapTo`, allowing a
// Map property from given method name of the source object.
$mapping->forMember('fullName', Operation::fromMethod('getFullName'));
// The `FromProperty` and `FromMethod` operations can be chained with `MapTo`, allowing a
// differently named property to be mapped to a class.
$mapping->forMember(
'address',
Expand Down
23 changes: 23 additions & 0 deletions src/Configuration/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AutoMapperPlus\NameConverter\NamingConvention\NamingConventionInterface;
use AutoMapperPlus\NameResolver\NameResolver;
use AutoMapperPlus\NameResolver\NameResolverInterface;
use AutoMapperPlus\PropertyAccessor\MethodReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyAccessor;
use AutoMapperPlus\PropertyAccessor\PropertyAccessorInterface;
use AutoMapperPlus\PropertyAccessor\PropertyReaderInterface;
Expand Down Expand Up @@ -50,6 +51,11 @@ class Options
*/
private $propertyReader;

/**
* @var MethodReaderInterface
*/
private $methodReader;

/**
* @var NameResolverInterface
*/
Expand Down Expand Up @@ -177,6 +183,7 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
$this->propertyReader = $propertyAccessor;
$this->propertyWriter = $propertyAccessor;
$this->propertyAccessor = $propertyAccessor;
$this->methodReader = $propertyAccessor;
}

/**
Expand Down Expand Up @@ -211,6 +218,22 @@ public function setPropertyReader(PropertyReaderInterface $propertyReader): void
$this->propertyReader = $propertyReader;
}

/**
* @return MethodReaderInterface
*/
public function getMethodReader(): MethodReaderInterface
{
return $this->methodReader ?: $this->propertyAccessor;
}

/**
* @param MethodReaderInterface $methodReader
*/
public function setMethodReader(MethodReaderInterface $methodReader): void
{
$this->methodReader = $methodReader;
}

/**
* @return MappingOperationInterface
*/
Expand Down
15 changes: 15 additions & 0 deletions src/MappingOperation/DefaultMappingOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use AutoMapperPlus\Configuration\Options;
use AutoMapperPlus\NameResolver\NameResolverInterface;
use AutoMapperPlus\PropertyAccessor\MethodReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyWriterInterface;

Expand Down Expand Up @@ -34,6 +35,11 @@ class DefaultMappingOperation implements MappingOperationInterface
*/
protected $propertyReader;

/**
* @var MethodReaderInterface
*/
protected $methodReader;

/**
* @var PropertyWriterInterface
*/
Expand All @@ -60,6 +66,7 @@ public function setOptions(Options $options): void
$this->options = $options;
$this->nameResolver = $options->getNameResolver();
$this->propertyReader = $options->getPropertyReader();
$this->methodReader = $options->getMethodReader();
$this->propertyWriter = $options->getPropertyWriter();
}

Expand Down Expand Up @@ -126,6 +133,14 @@ protected function getPropertyReader(): PropertyReaderInterface
return $this->propertyReader;
}

/**
* @return MethodReaderInterface
*/
protected function getMethodReader(): MethodReaderInterface
{
return $this->methodReader;
}

/**
* @return PropertyWriterInterface
*/
Expand Down
125 changes: 125 additions & 0 deletions src/MappingOperation/Implementations/FromMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace AutoMapperPlus\MappingOperation\Implementations;

use AutoMapperPlus\AutoMapperInterface;
use AutoMapperPlus\MappingOperation\AlternativePropertyProvider;
use AutoMapperPlus\MappingOperation\DefaultMappingOperation;
use AutoMapperPlus\MappingOperation\MapperAwareOperation;
use AutoMapperPlus\MappingOperation\MappingOperationInterface;
use AutoMapperPlus\NameResolver\CallbackNameResolver;

/**
* Class FromMethod
* @package AutoMapperPlus\MappingOperation\Implementations
*/
class FromMethod extends DefaultMappingOperation implements
AlternativePropertyProvider,
MapperAwareOperation
{
/**
* @var MappingOperationInterface|null
*/
private $nextOperation;

/**
* @var string
*/
private $methodName;

/**
* FromMethod constructor.
* @param string $methodName
*/
public function __construct(string $methodName)
{
$this->methodName = $methodName;
}

/**
* @inheritdoc
*/
public function mapProperty(string $propertyName, $source, $destination): void {
if ($this->nextOperation === null) {
parent::mapProperty($propertyName, $source, $destination);
return;
}

$this->mapPropertyWithNextOperation(
$propertyName,
$source,
$destination
);
}

/**
* @inheritdoc
*/
protected function canMapProperty(string $propertyName, $source): bool
{
$sourcePropertyName = $this->getSourcePropertyName($propertyName);

return $this->getMethodReader()->hasMethod($source, $sourcePropertyName);
}

/**
* @inheritdoc
*/
protected function getSourceValue($source, string $propertyName)
{
return $this->getMethodReader()->getMethod(
$source,
$this->getSourcePropertyName($propertyName)
);
}

/**
* @param string $propertyName
* @param $source
* @param $destination
*/
protected function mapPropertyWithNextOperation(
string $propertyName,
$source,
$destination
): void {
// We have to make the overridden property available to the next
// operation. To do this, we create a "one-time use" name resolver
// to pass to the operation.
$options = clone $this->options;
$options->setNameResolver(new CallbackNameResolver(function () {
return $this->methodName;
}));
$this->nextOperation->setOptions($options);

// The chained operation will now use the property name assigned to
// FromProperty, so we can go ahead and call it.
$this->nextOperation->mapProperty($propertyName, $source, $destination);
}

/**
* @inheritdoc
*/
public function getSourcePropertyName(string $propertyName): string
{
return $this->methodName;
}

/**
* @inheritdoc
*/
public function getAlternativePropertyName(): string
{
return $this->methodName;
}

/**
* @inheritdoc
*/
public function setMapper(AutoMapperInterface $mapper): void
{
if ($this->nextOperation instanceof MapperAwareOperation) {
$this->nextOperation->setMapper($mapper);
}
}
}
25 changes: 25 additions & 0 deletions src/PropertyAccessor/MethodReaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AutoMapperPlus\PropertyAccessor;

/**
* Interface PropertyReaderInterface
*
* @package AutoMapperPlus\PropertyAccessor
*/
interface MethodReaderInterface
{
/**
* @param $object
* @param string $methodName
* @return bool
*/
public function hasMethod($object, string $methodName): bool;

/**
* @param $object
* @param string $methodName
* @return mixed
*/
public function getMethod($object, string $methodName);
}
18 changes: 17 additions & 1 deletion src/PropertyAccessor/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace AutoMapperPlus\PropertyAccessor;

use ReflectionMethod;

/**
* Class PropertyAccessor
*
* @package AutoMapperPlus\PropertyAccessor
*/
class PropertyAccessor implements PropertyAccessorInterface
Expand Down Expand Up @@ -151,4 +152,19 @@ private function getReflectionProperty($object, string $propertyName): ?\Reflect

return null;
}

public function hasMethod($object, string $methodName): bool
{
return method_exists($object, $methodName) && $this->isMethodPublic($object, $methodName);
}

public function getMethod($object, string $methodName)
{
return $object->{$methodName}();
}

protected function isMethodPublic($object, string $methodName): bool
{
return (new ReflectionMethod($object, $methodName))->isPublic();
}
}
2 changes: 1 addition & 1 deletion src/PropertyAccessor/PropertyAccessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @package AutoMapperPlus\PropertyAccessor
*/
interface PropertyAccessorInterface extends PropertyWriterInterface, PropertyReaderInterface
interface PropertyAccessorInterface extends PropertyWriterInterface, PropertyReaderInterface, MethodReaderInterface
{
//
}
22 changes: 22 additions & 0 deletions test/Configuration/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AutoMapperPlus\Configuration;

use AutoMapperPlus\PropertyAccessor\MethodReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyAccessorInterface;
use AutoMapperPlus\PropertyAccessor\PropertyReaderInterface;
use AutoMapperPlus\PropertyAccessor\PropertyWriterInterface;
Expand Down Expand Up @@ -48,6 +49,18 @@ public function testPropertyReaderCanBeOverridden()
$this->assertEquals($reader, $options->getPropertyReader());
}

public function testMethodReaderCanBeOverridden()
{
$options = new Options();
$accessor = $this->createMock(PropertyAccessorInterface::class);
$reader = $this->createMock(MethodReaderInterface::class);
$options->setPropertyAccessor($accessor);
$options->setMethodReader($reader);

$this->assertEquals($accessor, $options->getPropertyAccessor());
$this->assertEquals($reader, $options->getMethodReader());
}

public function testPropertyWriterDefaultsToAccessor()
{
$options = new Options();
Expand All @@ -65,4 +78,13 @@ public function testPropertyReaderDefaultsToAccessor()

$this->assertEquals($accessor, $options->getPropertyReader());
}

public function testMethodReaderDefaultsToAccessor()
{
$options = new Options();
$accessor = $this->createMock(PropertyAccessorInterface::class);
$options->setPropertyAccessor($accessor);

$this->assertEquals($accessor, $options->getMethodReader());
}
}
28 changes: 28 additions & 0 deletions test/MappingOperation/Implementations/FromMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace AutoMapperPlus\MappingOperation\Implementations;

use AutoMapperPlus\Configuration\Options;
use AutoMapperPlus\Test\Models\SimpleProperties\Destination;
use AutoMapperPlus\Test\Models\Visibility\Visibility;
use PHPUnit\Framework\TestCase;

/**
* Class FromMethodTest
* @package AutoMapperPlus\MappingOperation\Implementations
*/
class FromMethodTest extends TestCase
{
public function testItMapsAMethod()
{
$operation = new FromMethod('getMethodValue');
$operation->setOptions(Options::default());

$source = new Visibility();
$destination = new Destination();

$operation->mapProperty('name', $source, $destination);

$this->assertSame('foo', $destination->name);
}
}
Loading