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

Call method after mapping #157

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class JsonMapper
*/
protected $arInspectedClasses = array();

/**
* Custom name of method fired after deserialize process
*
* @var string|null method name
*/
public $postMappingMethodName = null;

/**
* Map data all data in $json into the given $object instance.
*
Expand Down Expand Up @@ -296,6 +303,15 @@ public function map($json, $object)
$this->removeUndefinedAttributes($object, $providedProperties);
}

if ($this->postMappingMethodName !== null
&& $rc->hasMethod($this->postMappingMethodName)
) {
$refDeserializePostMethod = $rc
->getMethod($this->postMappingMethodName);
$refDeserializePostMethod->setAccessible(true);
$refDeserializePostMethod->invoke($object);
}

return $object;
}

Expand Down
47 changes: 47 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Part of JsonMapper
*
* PHP version 5
*
* @category Tools
* @package JsonMapper
* @author Christian Weiske <[email protected]>
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
*/

use PHPUnit\Framework\TestCase;

require_once 'JsonMapperTest/EventObject.php';

/**
* Unit tests for JsonMapper's object handling (events)
*
* @category Tools
* @package JsonMapper
* @author Christian Weiske <[email protected]>
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
*/
class EventTest extends TestCase
{
/**
* Test for deserialize post event
*
* @throws \JsonMapper_Exception
*/
public function testDeserializePostEvent()
{
$jm = new JsonMapper();
$jm->postMappingMethodName = '_deserializePostEvent';
/** @var JsonMapperTest_EventObject $sn */
$sn = $jm->map(
json_decode('{"pStr":"one"}', false),
new JsonMapperTest_EventObject()
);
$this->assertInternalType('string',$sn->pStr);
$this->assertEquals('two', $sn->pStr);
}
}
?>
13 changes: 13 additions & 0 deletions tests/JsonMapperTest/EventObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
class JsonMapperTest_EventObject
{
/**
* @var string
*/
public $pStr;

private function _deserializePostEvent(){
$this->pStr = 'two';
}
}
?>