-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from andreypostal/key-mapping-strategy
Rework trait usage and add key mapping strategy
- Loading branch information
Showing
9 changed files
with
185 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Andrey\JsonHandler; | ||
|
||
interface HydratorInterface | ||
{ | ||
public function hydrate(string|array $json, object|string $objOrClass): object; | ||
} |
This file was deleted.
Oops, something went wrong.
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,11 @@ | ||
<?php | ||
|
||
namespace Andrey\JsonHandler\KeyMapping; | ||
|
||
interface KeyMappingStrategy | ||
{ | ||
// -> Hydrate | ||
public function from(string $key): string; | ||
// <- Parse | ||
public function to(string $key): string; | ||
} |
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,60 @@ | ||
<?php /** @noinspection ForeachInvariantsInspection */ | ||
|
||
namespace Andrey\JsonHandler\KeyMapping; | ||
|
||
/** | ||
* | ||
*/ | ||
class KeyMappingUnderscore implements KeyMappingStrategy | ||
{ | ||
/** | ||
* Map keys from pascalCase to underscore_case | ||
* | ||
*/ | ||
public function from(string $key): string | ||
{ | ||
$in = str_split($key); | ||
$len = count($in); | ||
$out = ''; | ||
for ($i = 0; $i < $len; $i++) { | ||
if ($in[$i] < 'a') { | ||
$out .= '_'; | ||
// if already is an underscore, just skip case conversion | ||
// but still add another underscore before it | ||
if ($in[$i] !== '_') { | ||
$out .= chr((ord($in[$i]) - ord('A')) + ord('a')); | ||
} else { | ||
$out .= $in[$i]; | ||
} | ||
} else { | ||
$out .= $in[$i]; | ||
} | ||
} | ||
return $out; | ||
} | ||
|
||
public function to(string $key): string | ||
{ | ||
$in = str_split($key); | ||
$len = count($in); | ||
$out = ''; | ||
// my_key => myKey | ||
for ($i = 0; $i < $len; $i++) { | ||
$c = $in[$i]; | ||
if ($c === '_') { | ||
$c = $in[$i+1]; | ||
// jump to next letter (skip lowercase already dealt with) | ||
$i++; | ||
if ($c !== '_') { | ||
$out .= chr((ord($c) - ord('a')) + ord('A')); | ||
} else { | ||
$out .= $c; | ||
} | ||
} else { | ||
$out .= $c; | ||
} | ||
} | ||
|
||
return $out; | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Andrey\JsonHandler; | ||
|
||
interface SerializerInterface | ||
{ | ||
public function serialize(object $obj): array; | ||
} |
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,58 @@ | ||
<?php | ||
namespace KeyMapping; | ||
|
||
use Andrey\JsonHandler\KeyMapping\KeyMappingUnderscore; | ||
use PHPUnit\Framework\Attributes\CoversTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversTrait(KeyMappingUnderscore::class)] | ||
final class KeyMappingUnderscoreTest extends TestCase | ||
{ | ||
public function testFromKey(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->from('fromMyKey'); | ||
$this->assertEquals('from_my_key', $result); | ||
} | ||
|
||
public function testFromKeyStartingWithUnderscore(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->from('_fromMyKey'); | ||
$this->assertEquals('__from_my_key', $result); | ||
} | ||
|
||
/** | ||
* For pascal case maintain behavior otherwise we cannot keep | ||
* the equality (parser -> serializer and serializer -> parser) | ||
* | ||
* i.e. _from_my_key => FromMyKey but from_my_key => fromMyKey | ||
*/ | ||
public function testFromKeyPascalCase(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->from('FromMyKey'); | ||
$this->assertEquals('_from_my_key', $result); | ||
} | ||
|
||
public function testToKey(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->to('from_my_key'); | ||
$this->assertEquals('fromMyKey', $result); | ||
} | ||
|
||
public function testToKeyStartingWithUnderscore(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->to('__from_my_key'); | ||
$this->assertEquals('_fromMyKey', $result); | ||
} | ||
|
||
public function testToKeyPascalCase(): void | ||
{ | ||
$strategy = new KeyMappingUnderscore(); | ||
$result = $strategy->to('_from_my_key'); | ||
$this->assertEquals('FromMyKey', $result); | ||
} | ||
} |