-
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.
Merge pull request #19 from spotlibs/feature/exploration
new trait TraitConvertibleDtos
- Loading branch information
Showing
5 changed files
with
170 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/** | ||
* PHP version 8 | ||
* | ||
* @category Library | ||
* @package Dtos | ||
* @author Made Mas Adi Winata <m45adiwinata@gmail.com> | ||
* @license https://mit-license.org/ MIT License | ||
* @version GIT: 0.0.4 | ||
* @link https://github.com/spotlibs | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spotlibs\PhpLib\Dtos; | ||
|
||
use ReflectionClass; | ||
use Throwable; | ||
|
||
/** | ||
* TraitDtos | ||
* | ||
* @category Library | ||
* @package Dtos | ||
* @author Made Mas Adi Winata <m45adiwinata@gmail.com> | ||
* @license https://mit-license.org/ MIT License | ||
* @link https://github.com/spotlibs | ||
*/ | ||
trait TraitConvertibleDtos | ||
{ | ||
/** | ||
* Construct a DTO instance from associative array. Array key and value data type must comply DTO class property | ||
* | ||
* @param array $data associative array | ||
* | ||
* @return mixed | ||
*/ | ||
public function __construct(array $data = []) | ||
{ | ||
$reflector = new ReflectionClass(static::class); | ||
foreach ($data as $key => $value) { | ||
try { | ||
$prop = $reflector->getProperty($key); | ||
} catch (Throwable) { | ||
// array key is not one of constructed DTO's property name | ||
continue; | ||
} | ||
$value = TypeConverter::assertType($value, $reflector, $prop); | ||
$this->{$key} = $value; | ||
} | ||
} | ||
|
||
/** | ||
* Create a DTO instance from associative array. Array key and value data type must comply DTO class property | ||
* | ||
* @param array $data associative array | ||
* | ||
* @return mixed | ||
*/ | ||
public static function create(array $data): mixed | ||
{ | ||
$self = new self($data); | ||
|
||
return $self; | ||
} | ||
|
||
/** | ||
* Convert instance to associative array | ||
* | ||
* @return array | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return (array) $this; | ||
} | ||
|
||
/** | ||
* Convert instance to json | ||
* | ||
* @return bool|string | ||
*/ | ||
public function toJson() | ||
{ | ||
$data = $this->toArray(); | ||
return json_encode($data); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Dtos; | ||
use Spotlibs\PhpLib\Dtos\TraitDtos; | ||
|
||
class Dto2 | ||
{ | ||
use TraitDtos; | ||
|
||
public string $name; | ||
public int $employeeId; | ||
public bool $isActive; | ||
public array $relatives; | ||
public Vehicle $vehicle; | ||
} |
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