From fc38b999227a595be730f02d79d2a0fa40a0fefd Mon Sep 17 00:00:00 2001 From: pfazzi Date: Thu, 6 Jan 2022 17:27:33 +0100 Subject: [PATCH] Add some docs to README.md --- README.md | 26 +++++++++++++++++++++----- test/MapperTest.php | 17 +++++++++++++++++ test/User.php | 15 +++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test/User.php diff --git a/README.md b/README.md index dc14bcf..ad81769 100644 --- a/README.md +++ b/README.md @@ -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' => 'pfazzi@test.com', + 'is_enabled' => '1', +]; + +$mapper = new \Pfazzi\SimplexMapper\Mapper(); + +$user = $mapper->map( + source: $dbData, + target: User::class, + nameConverter: new SnakeToCamel() +); +``` ## Installation @@ -21,14 +37,14 @@ $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 @@ -36,13 +52,13 @@ $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 diff --git a/test/MapperTest.php b/test/MapperTest.php index 59b4edc..875ae9e 100644 --- a/test/MapperTest.php +++ b/test/MapperTest.php @@ -17,6 +17,23 @@ protected function setUp(): void $this->mapper = new Mapper(); } + public function test_demo(): void + { + $dbData = [ + 'username' => 'pfazzi', + 'email_address' => 'pfazzi@test.com', + 'is_enabled' => '1', + ]; + + $user = $this->mapper->map( + source: $dbData, + target: User::class, + nameConverter: new SnakeToCamel() + ); + + self::assertEquals(new User('pfazzi', 'pfazzi@test.com', true), $user); + } + public function test_it_maps_an_array_to_an_object(): void { $source = [ diff --git a/test/User.php b/test/User.php new file mode 100644 index 0000000..4b3f5ec --- /dev/null +++ b/test/User.php @@ -0,0 +1,15 @@ +