-
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.
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
# SimplexMapper | ||
|
||
A simple PHP library to map data from one object to another. | ||
A simple PHP library to map data from a source (object or array) to an object. | ||
|
||
```php | ||
$dbData = [ | ||
'username' => 'pfazzi', | ||
'email_address' => '[email protected]', | ||
'is_enabled' => '1', | ||
]; | ||
|
||
$mapper = new \Pfazzi\SimplexMapper\Mapper(); | ||
|
||
$user = $mapper->map( | ||
source: $dbData, | ||
target: User::class, | ||
nameConverter: new SnakeToCamel() | ||
); | ||
``` | ||
|
||
## Installation | ||
|
||
|
@@ -21,28 +37,28 @@ $mapper = new \Pfazzi\SimplexMapper\Mapper(); | |
|
||
// From Array | ||
|
||
$someClassInstance = $mapper->map(source: ['name' => 'patrick'], target: UserEntity::class); | ||
$userEntity = $mapper->map(source: ['name' => 'patrick'], target: UserEntity::class); | ||
|
||
// From stdClass | ||
|
||
$rawData = new \stdClass(); | ||
$rawData->name = 'Patrick'; | ||
|
||
$someClassInstance = $mapper->map(source: $rawData, target: UserEntity::class); | ||
$userEntity = $mapper->map(source: $rawData, target: UserEntity::class); | ||
|
||
// From anonymous class | ||
|
||
$rawData = new class { | ||
public function __construct(private string $name = 'Patrick') {} | ||
}; | ||
|
||
$someClassInstance = $mapper->map(source: $rawData, target: UserEntity::class); | ||
$userEntity = $mapper->map(source: $rawData, target: UserEntity::class); | ||
|
||
// From object | ||
|
||
$rawData = new UserDto('Patrick'); | ||
|
||
$someClassInstance = $mapper->map(source: $rawData, target: UserEntity::class); | ||
$userEntity = $mapper->map(source: $rawData, target: UserEntity::class); | ||
``` | ||
|
||
### Hydrate | ||
|
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 |
---|---|---|
|
@@ -17,6 +17,23 @@ protected function setUp(): void | |
$this->mapper = new Mapper(); | ||
} | ||
|
||
public function test_demo(): void | ||
{ | ||
$dbData = [ | ||
'username' => 'pfazzi', | ||
'email_address' => '[email protected]', | ||
'is_enabled' => '1', | ||
]; | ||
|
||
$user = $this->mapper->map( | ||
source: $dbData, | ||
target: User::class, | ||
nameConverter: new SnakeToCamel() | ||
); | ||
|
||
self::assertEquals(new User('pfazzi', '[email protected]', true), $user); | ||
} | ||
|
||
public function test_it_maps_an_array_to_an_object(): void | ||
{ | ||
$source = [ | ||
|
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pfazzi\SimplexMapper\Test; | ||
|
||
class User | ||
{ | ||
public function __construct( | ||
private string $username, | ||
private string $emailAddress, | ||
private bool $isEnabled, | ||
) { | ||
} | ||
} |