Skip to content

Commit

Permalink
Feature: use other argument as object to populate
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohan Yang committed Jan 10, 2024
1 parent 8246173 commit e44e06f
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/mango/HttpKernel/MapRequestPayloadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,36 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
{
$request = $event->getRequest();

if (! $routeParams = $request->attributes->get('_route_params', [])) {
return;
}

$arguments = $event->getArguments();

foreach ($arguments as $i => $argument) {
if (! $argument instanceof MapRequestPayload) {
continue;
}

$groups = $argument->serializationContext['groups'] ?? [];

if ($groups !== [] && ! in_array('route', $groups, true)) {
if ($argument->disabled) {
continue;
}

$object = $this->denormalizer->denormalize(
$routeParams,
$argument->metadata->getType(),
context: ['groups' => ['route']] + $argument->serializationContext,
);
if (str_ends_with($name = $argument->metadata->getName(), 'Payload')) {
$object = $event->getNamedArguments()[substr($name, 0, -7)] ?? null;
} else if ($routeParams = $request->attributes->get('_route_params', [])) {
$groups = $argument->serializationContext['groups'] ?? [];

if ($groups !== [] && ! in_array('route', $groups, true)) {
continue;
}

$object = $this->denormalizer->denormalize(
$routeParams,
$argument->metadata->getType(),
context: ['groups' => ['route']] + $argument->serializationContext,
);
}

if (!isset($object)) {
continue;
}

$attribute = new MapRequestPayload(
$argument->acceptFormat,
Expand Down

0 comments on commit e44e06f

Please sign in to comment.