Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[WIP] Request validator #49002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Validator\Constraints\NotCompromisedPasswordValidator;
use Symfony\Component\Validator\Constraints\WhenValidator;
use Symfony\Component\Validator\ContainerConstraintValidatorFactory;
use Symfony\Component\Validator\EventListener\RequestValidationSubscriber;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
Expand Down Expand Up @@ -109,5 +110,12 @@
service('property_info'),
])
->tag('validator.auto_mapper')

->set('validator.request_validator', RequestValidationSubscriber::class)
->args([
service('validator'),
service('serializer')->nullOnInvalid(),
])
->tag('kernel.event_subscriber')
;
};
24 changes: 24 additions & 0 deletions src/Symfony/Component/Validator/Attribute/RequestValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Symfony\Component\Validator\Attribute;

#[\Attribute(\Attribute::TARGET_METHOD)]
final class RequestValidator
{
public const ORDER_ATTRIBUTES = 'attributes';
public const ORDER_SERIALIZE = 'serialize';
public const ORDER_QUERY = 'query';
public const ORDER_REQUEST = 'request';

public function __construct(
public string $class,
public bool $override = true,
public array $order = [
self::ORDER_ATTRIBUTES,
self::ORDER_QUERY,
self::ORDER_REQUEST,
],
public string $serializedFormat = 'json'
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

namespace Symfony\Component\Validator\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Attribute\RequestValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class RequestValidationSubscriber implements EventSubscriberInterface
{
public function __construct(
readonly ValidatorInterface $validator,
readonly ?SerializerInterface $serializer = null
) {
}

public static function getSubscribedEvents()
{
return [
ControllerArgumentsEvent::class => 'validateRequest'
];
}

public function validateRequest(ControllerArgumentsEvent $event): void {
$controller = $event->getController();
$arguments = $event->getArguments();
$reflectionMethod = $this->getReflectionMethod($controller);
$request = $event->getRequest();

$attributes = $reflectionMethod->getAttributes(RequestValidator::class, \ReflectionAttribute::IS_INSTANCEOF);

if (count($attributes) === 0) {
return;
}

// only first attribute can validate
$attribute = $attributes[0];

$attributeArguments = $attribute->getArguments();
if(key_exists('class', $attributeArguments)) {
$class = $attributeArguments['class'];
$override = key_exists('override', $attributeArguments) ? $attributeArguments['override'] : true;
$order = key_exists('order', $attributeArguments) ? $attributeArguments['order'] : [
RequestValidator::ORDER_ATTRIBUTES,
RequestValidator::ORDER_QUERY,
RequestValidator::ORDER_REQUEST,
];
$serializedFormat = key_exists('serializedFormat', $attributeArguments) ? $attributeArguments['json'] : 'json';
}else {
$class = $attributeArguments[0];
$override = key_exists(1, $attributeArguments) ? $attributeArguments[1] : true;
$order = key_exists(2, $attributeArguments) ? $attributeArguments[2] : [
RequestValidator::ORDER_ATTRIBUTES,
RequestValidator::ORDER_QUERY,
RequestValidator::ORDER_REQUEST,
];
$serializedFormat = key_exists(3, $attributeArguments) ? $attributeArguments[3] : 'json';
}

$object = new $class();

foreach ($order as $type) {
switch ($type) {
case RequestValidator::ORDER_SERIALIZE:
if(empty($request->getContent())) {
continue 2;
}
$serializer = $this->getSerializer();
$serializer->deserialize($request->getContent(), $class, $serializedFormat,
[AbstractNormalizer::OBJECT_TO_POPULATE => $object]);
continue 2;
case RequestValidator::ORDER_REQUEST:
$this->setProperties($object, $request->request->all(), $override);
break;
case RequestValidator::ORDER_QUERY:
$this->setProperties($object, $request->query->all(), $override);
break;
case RequestValidator::ORDER_ATTRIBUTES:
$this->setProperties($object, $request->attributes->all(), $override);
break;
}

}

$violations = $this->validator->validate($object);

if(count($violations) > 0) {
throw new ValidationFailedException(sprintf("Validation of %s failed!", $class), $violations);
}

foreach ($arguments as $index => $argument) {
if(!$argument instanceof $class) {
continue;
}
$arguments[$index] = $object;
}

$event->setArguments($arguments);
}

private function setProperties(object $object, array $parameters, bool $override) {
foreach ($parameters as $key => $value) {
if(false === $override && property_exists($object, $key) && isset($object->{$key})) {
continue;
}
$object->{$key} = $value;
}
}

private function getReflectionMethod(callable $controller): \ReflectionMethod
{
if (is_array($controller)) {
$class = $controller[0];
$method = $controller[1];
} else {
/** @var object $controller */
$class = $controller;
$method = '__invoke';
}

return new \ReflectionMethod($class, $method);
}

private function getSerializer(): SerializerInterface
{
if (!class_exists(Serializer::class)) {
throw new LogicException(sprintf('The "symfony/serializer" component is required to use the "%s" validator. Try running "composer require symfony/serializer".',
__CLASS__));
}

return $this->serializer;
}
}