File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ protected function getCasters(): array
139139 'Hyperf\ViewEngine\HtmlString ' => 'FriendsOfHyperf\Tinker\TinkerCaster::castHtmlString ' ,
140140 'Stringable ' => 'FriendsOfHyperf\Tinker\TinkerCaster::castStringable ' ,
141141 'Symfony\Component\Console\Application ' => 'FriendsOfHyperf\Tinker\TinkerCaster::castApplication ' ,
142+ 'FriendsOfHyperf\ValidatedDTO\SimpleDTO ' => 'FriendsOfHyperf\Tinker\TinkerCaster::castSimpleDTO ' ,
142143 ];
143144
144145 return array_merge ($ casters , (array ) $ this ->config ->get ('tinker.casters ' , []));
Original file line number Diff line number Diff line change 1111
1212namespace FriendsOfHyperf \Tinker ;
1313
14+ use ReflectionClass ;
15+ use ReflectionProperty ;
1416use Stringable ;
1517use Symfony \Component \VarDumper \Caster \Caster ;
1618use Throwable ;
@@ -185,4 +187,23 @@ public static function castMessageBag($messageBag): array
185187 Caster::PREFIX_PROTECTED . 'format ' => $ messageBag ->getFormat (),
186188 ];
187189 }
190+
191+ /**
192+ * @param \FriendsOfHyperf\ValidatedDTO\SimpleDTO $dto
193+ */
194+ public static function castSimpleDTO ($ dto ): array
195+ {
196+ $ reflectionClass = new ReflectionClass ($ dto );
197+ $ results = [];
198+
199+ foreach ($ reflectionClass ->getProperties (ReflectionProperty::IS_PUBLIC ) as $ property ) {
200+ $ property ->setAccessible (true );
201+ $ results [Caster::PREFIX_VIRTUAL . $ property ->getName ()] = $ property ->getValue ($ dto );
202+ }
203+
204+ $ results [Caster::PREFIX_PROTECTED . 'validatedData ' ] = (fn () => $ this ->validatedData ?? [])->call ($ dto );
205+ $ results [Caster::PREFIX_VIRTUAL . $ dto ::class . '::toArray() ' ] = $ dto ->toArray ();
206+
207+ return $ results ;
208+ }
188209}
You can’t perform that action at this time.
0 commit comments