generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nested mapping support via new
ObjectMap
class (#97)
Co-authored-by: Alexander Makarov <[email protected]>
- Loading branch information
Showing
14 changed files
with
397 additions
and
12 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
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator; | ||
|
||
use function array_key_exists; | ||
|
||
/** | ||
* Provides a mapping of object property names to keys in the data array. | ||
* | ||
* @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 | ||
) { | ||
} | ||
|
||
/** | ||
* Returns a path for a given property name or null if mapping dosen't exist. | ||
* | ||
* @psalm-return string|list<string>|ObjectMap|null | ||
*/ | ||
public function getPath(string $name): string|array|self|null | ||
{ | ||
return $this->map[$name] ?? null; | ||
} | ||
|
||
/** | ||
* Returns a list of property names for which mapping is set. | ||
* | ||
* @return string[] List of property names. | ||
* @psalm-return list<string> | ||
*/ | ||
public function getNames(): array | ||
{ | ||
return array_keys($this->map); | ||
} | ||
|
||
/** | ||
* Checks if a given property name exists in the mapping array. | ||
* | ||
* @param string $name The property name. | ||
* @return bool Whether the property name exists in the mapping array. | ||
*/ | ||
public function exists(string $name): bool | ||
{ | ||
return array_key_exists($name, $this->map); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Car | ||
{ | ||
public function __construct( | ||
public ?Engine $engine = null, | ||
) { | ||
} | ||
} |
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 Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Engine | ||
{ | ||
public string $version = ''; | ||
|
||
public function __construct( | ||
public string $name, | ||
) { | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Nested | ||
{ | ||
public string $var = ''; | ||
public ?Nested2 $nested2 = null; | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Hydrator\Tests\ObjectMap; | ||
|
||
final class Nested2 | ||
{ | ||
public string $var1 = ''; | ||
public string $var2 = ''; | ||
} |
Oops, something went wrong.