-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow undefined property handler to return a string to override varia…
…ble name This lets us add e.g. camel case mapping handlers. Resolves: #198 Resolves: #183 Resolves: #150 Resolves: #149 Resolves: #29
- Loading branch information
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
require_once 'JsonMapperTest/Simple.php'; | ||
|
||
class NameMappingTest extends TestCase | ||
{ | ||
public function testItSetKeysIfReturnedByUndefinedPropertyHandler(): void | ||
{ | ||
$jm = new JsonMapper(); | ||
$jm->undefinedPropertyHandler = function ( | ||
JsonMapperTest_Simple $object, | ||
string $key, | ||
$value | ||
): string { | ||
return lcfirst( | ||
str_replace( | ||
' ', '', ucwords(str_replace(array('_', '-'), ' ', $key)) | ||
) | ||
); | ||
}; | ||
|
||
/** @var JsonMapperTest_Simple $sn */ | ||
$sn = $jm->map( | ||
json_decode('{"hyphen_value": "abc"}'), | ||
new JsonMapperTest_Simple() | ||
); | ||
|
||
self::assertSame('abc', $sn->hyphenValue); | ||
} | ||
|
||
public function testItDoesNotMapKeyIfUndefinedPropertyHandlerDoesNotReturnValue(): void | ||
{ | ||
$jm = new JsonMapper(); | ||
$jm->undefinedPropertyHandler = function ( | ||
JsonMapperTest_Simple $object, | ||
string $key, | ||
$value | ||
): void {}; | ||
|
||
/** @var JsonMapperTest_Simple $sn */ | ||
$sn = $jm->map( | ||
json_decode('{"hyphen_value": "abc"}'), | ||
new JsonMapperTest_Simple() | ||
); | ||
|
||
self::assertNull($sn->hyphenValue); | ||
} | ||
} |