Last active
July 20, 2026 13:34
-
-
Save dbalabka/651030146d59190357ab867527ed92f7 to your computer and use it in GitHub Desktop.
Fast and convinient strict typed DTO creation from REST plain array or stdClass response . Run https://3v4l.org/0uDPC#v8.1.19
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| class SomeClass | |
| { | |
| public function __construct( | |
| public readonly string $status | |
| ) { | |
| } | |
| public static function create(array|stdClass $params): self | |
| { | |
| $properties = get_class_vars(self::class); | |
| return new self(...array_intersect_key((array) $params, $properties)); | |
| } | |
| } | |
| var_dump(SomeClass::create((object) ['status' => 'pending', 'foo' => 'bar'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment