Skip to content

Commit

Permalink
Add some docs to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
pfazzi committed Jan 6, 2022
1 parent de57735 commit fc38b99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
26 changes: 21 additions & 5 deletions README.md
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

Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
15 changes: 15 additions & 0 deletions test/User.php
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,
) {
}
}

0 comments on commit fc38b99

Please sign in to comment.