Skip to content

Commit

Permalink
Change property accessor's setPrivate behaviour, fixes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed Jan 27, 2019
1 parent fa998f3 commit 3beb622
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/PropertyAccessor/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,26 @@ protected function getPrivate($object, string $propertyName)
}

/**
* Adapted from https://gist.github.com/githubjeka/153e5a0f6d15cf20512e.
*
* @param $object
* @param string $propertyName
* @param $value
*/
protected function setPrivate($object, string $propertyName, $value): void
{
$propertyNameLength = \strlen($propertyName);

array_walk(
$object,
function (&$objectValue, $objectPropertyName) use ($value, $propertyName, $propertyNameLength): void {
// Since breaking out of `array_walk` isn't possible, we'll
// keep track of the fact whether or not we have successfully
// set the property using a static variable. This to prevent
// doing lots of `substr` calls
static $setComplete = false;
if ($setComplete) {
return;
}
if (substr($objectPropertyName, - $propertyNameLength) === $propertyName) {
$objectValue = $value;
$setComplete = true;
}
});
$reflectionClass = new \ReflectionClass($object);

// Parent properties are not included in the reflection class, so we'll
// go up the inheritance chain and check if the property exists in one
// of the parents.
while (
!$reflectionClass->hasProperty($propertyName)
&& $parent = $reflectionClass->getParentClass()
) {
$reflectionClass = $parent;
}
$reflectionProperty = $reflectionClass->getProperty($propertyName);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use AutoMapperPlus\Test\Models\Inheritance\DestinationParent;
use AutoMapperPlus\Test\Models\Inheritance\SourceChild;
use AutoMapperPlus\Test\Models\Inheritance\SourceParent;
use AutoMapperPlus\Test\Models\Issues\Issue33\User;
use AutoMapperPlus\Test\Models\Issues\Issue33\UserDto;
use AutoMapperPlus\Test\Models\Nested\Address;
use AutoMapperPlus\Test\Models\Nested\AddressDto;
use AutoMapperPlus\Test\Models\Nested\Person;
Expand Down Expand Up @@ -660,6 +662,27 @@ public function testItMapsPrivatePropertiesWithTheSameSuffix()
$this->assertEquals('id_2', $result->second_id);
}

/**
* @see https://github.com/mark-gerarts/automapper-plus/issues/33
*/
public function testItMapsPrivatePropertiesWithTheSameSuffixOnTheTarget()
{
$config = new AutoMapperConfig();
$config->registerMapping(UserDto::class, User::class);
$mapper = new AutoMapper($config);

$source = new UserDto();
$source->id = 'id-value';
$source->cellphone = 'cellphone-value';
$source->phone = 'phone-value';
/** @var User $result */
$result = $mapper->map($source, User::class);

$this->assertEquals('id-value', $result->getId());
$this->assertEquals('cellphone-value', $result->getCellphone());
$this->assertEquals('phone-value', $result->getPhone());
}

/**
* https://github.com/mark-gerarts/automapper-plus/issues/25
*/
Expand Down
25 changes: 25 additions & 0 deletions test/Models/Issues/Issue33/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace AutoMapperPlus\Test\Models\Issues\Issue33;

class User
{
private $id;
private $cellphone;
private $phone;

public function getId()
{
return $this->id;
}

public function getCellphone()
{
return $this->cellphone;
}

public function getPhone()
{
return $this->phone;
}
}
10 changes: 10 additions & 0 deletions test/Models/Issues/Issue33/UserDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace AutoMapperPlus\Test\Models\Issues\Issue33;

class UserDto
{
public $id;
public $cellphone;
public $phone;
}
14 changes: 14 additions & 0 deletions test/PropertyAccessor/PropertyAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AutoMapperPlus\PropertyAccessor;

use AutoMapperPlus\Test\Models\Inheritance\SourceChild;
use AutoMapperPlus\Test\Models\Issues\Issue33\User;
use AutoMapperPlus\Test\Models\Visibility\InheritedVisibility;
use PHPUnit\Framework\TestCase;
use AutoMapperPlus\Test\Models\Visibility\Visibility;
Expand Down Expand Up @@ -150,4 +151,17 @@ public function testItWritesToAParentsPublicProperty()

$this->assertEquals('new value', $source->getPublicProperty());
}

/**
* @see https://github.com/mark-gerarts/automapper-plus/issues/33
*/
public function testItWritesCorrectlyWhenPropertiesShareASuffix()
{
$accessor = new PropertyAccessor();
$source = new User();

$accessor->setProperty($source, 'phone', 'phone-value');

$this->assertEquals('phone-value', $source->getPhone());
}
}

0 comments on commit 3beb622

Please sign in to comment.