-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nested mapping support via new ObjectMap
class
#97
base: master
Are you sure you want to change the base?
Changes from all commits
7ce7d3b
58d13a1
d94a0ce
2a3f057
1fd5f6e
364d2ac
20a6f22
b78b63f
589eaba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Yiisoft\Hydrator; | ||||||
|
||||||
use function array_key_exists; | ||||||
|
||||||
/** | ||||||
* Class provides mapping object property names to keys in the data array. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* @psalm-import-type MapType from ArrayData | ||||||
*/ | ||||||
final class ObjectMap | ||||||
{ | ||||||
/** | ||||||
* @param array $map Object property names mapped to keys in the data array that hydrator will use when hydrating | ||||||
* an object. | ||||||
* @psalm-param MapType $map | ||||||
*/ | ||||||
public function __construct( | ||||||
public readonly array $map | ||||||
) { | ||||||
} | ||||||
|
||||||
/** | ||||||
* @psalm-return string|list<string>|ObjectMap|null | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need description. |
||||||
*/ | ||||||
public function getPath(string $name): string|array|self|null | ||||||
{ | ||||||
return $this->map[$name] ?? null; | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return string[] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need description. |
||||||
* @psalm-return list<string> | ||||||
*/ | ||||||
public function getNames(): array | ||||||
{ | ||||||
return array_keys($this->map); | ||||||
} | ||||||
|
||||||
public function exists(string $name): bool | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need description. |
||||||
{ | ||||||
return array_key_exists($name, $this->map); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Nested | ||
{ | ||
public string $var = ''; | ||
public ?Nested2 $nested2 = null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Nested2 | ||
{ | ||
public string $var1 = ''; | ||
public string $var2 = ''; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Hydrator\ArrayData; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\ObjectMap; | ||
|
||
final class ObjectMapTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
|
||
$root = $hydrator->create( | ||
Root::class, | ||
new ArrayData( | ||
['key' => 'test'], | ||
['nested' => new ObjectMap(['var' => 'key'])], | ||
), | ||
); | ||
|
||
$this->assertSame('test', $root->nested?->var); | ||
$this->assertNull($root->nested->nested2); | ||
} | ||
|
||
public function testNested2(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
|
||
$root = $hydrator->create( | ||
Root::class, | ||
new ArrayData( | ||
['a' => 'A', 'b' => ['b1' => 'B1'], 'c' => 'C'], | ||
[ | ||
'nested' => new ObjectMap([ | ||
'var' => 'a', | ||
'nested2' => new ObjectMap([ | ||
'var1' => ['b', 'b1'], | ||
'var2' => 'c', | ||
]), | ||
]), | ||
], | ||
), | ||
); | ||
|
||
$this->assertSame('A', $root->nested?->var); | ||
$this->assertSame('B1', $root->nested?->nested2?->var1); | ||
$this->assertSame('C', $root->nested?->nested2?->var2); | ||
} | ||
|
||
public function testWithSameKeyInData(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
|
||
$root = $hydrator->create( | ||
Root::class, | ||
new ArrayData( | ||
[ | ||
'var' => 'test', | ||
'var1' => 'A', | ||
'var2' => 'B', | ||
], | ||
[ | ||
'nested' => new ObjectMap([ | ||
'nested2' => new ObjectMap([ | ||
'var1' => 'var', | ||
]), | ||
]), | ||
], | ||
), | ||
); | ||
|
||
$this->assertSame('', $root->nested?->var); | ||
$this->assertSame('test', $root->nested?->nested2?->var1); | ||
$this->assertSame('', $root->nested?->nested2?->var2); | ||
} | ||
|
||
public function testWithoutMap(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
|
||
$root = $hydrator->create( | ||
Root::class, | ||
new ArrayData( | ||
[ | ||
'nested' => [ | ||
'nested2' => [ | ||
'var1' => 'A', | ||
'var2' => 'B', | ||
], | ||
], | ||
], | ||
), | ||
); | ||
|
||
$this->assertSame('A', $root->nested?->nested2?->var1); | ||
$this->assertSame('B', $root->nested?->nested2?->var2); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap\Readme; | ||
|
||
final class Body | ||
{ | ||
public string $text = ''; | ||
public string $html = ''; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap\Readme; | ||
|
||
final class Message | ||
{ | ||
public string $subject = ''; | ||
public ?Body $body = null; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap\Readme; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Hydrator\ArrayData; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\ObjectMap; | ||
|
||
final class ObjectMapReadmeTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$hydrator = new Hydrator(); | ||
$data = [ | ||
'title' => 'Hello, World!', | ||
'textBody' => 'Nice to meet you.', | ||
'htmlBody' => '<h1>Nice to meet you.</h1>', | ||
]; | ||
$map = [ | ||
'subject' => 'title', | ||
'body' => new ObjectMap([ | ||
'text' => 'textBody', | ||
'html' => 'htmlBody', | ||
]), | ||
]; | ||
|
||
$message = $hydrator->create(Message::class, new ArrayData($data, $map)); | ||
|
||
$this->assertSame('Hello, World!', $message->subject); | ||
$this->assertSame('Nice to meet you.', $message->body?->text); | ||
$this->assertSame('<h1>Nice to meet you.</h1>', $message->body?->html); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Root | ||
{ | ||
public ?Nested $nested = null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about constructor, method calls, immutable objects etc.?