Skip to content

[Validator] [WIP] Validate controller arguments using constraints #47425

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 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added ControllerArgument constraint
  • Loading branch information
artyuum committed Aug 31, 2022
commit 225b58edc3dacbf0e6b9926bcdbc798e82295ee1
44 changes: 44 additions & 0 deletions src/Symfony/Component/Validator/Constraints/ControllerArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

/**
* @author Dynèsh Hassanaly <artyum@protonmail.com>
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class ControllerArgument extends Composite
{
public $constraints = [];

public function __construct(mixed $constraints = null, array $groups = null, mixed $payload = null)
{
parent::__construct($constraints ?? [], $groups, $payload);
}

public function getDefaultOption(): ?string
{
return 'constraints';
}

public function getRequiredOptions(): array
{
return ['constraints'];
}

/**
* @inheritDoc
*/
protected function getCompositeOption(): string
{
return 'constraints';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Dynèsh Hassanaly <artyum@protonmail.com>
*/
class ControllerArgumentValidator extends ConstraintValidator
{
/**
* @inheritDoc
*/
public function validate(mixed $value, Constraint $constraint)
{
if (!$constraint instanceof ControllerArgument) {
throw new UnexpectedTypeException($constraint, ControllerArgument::class);
}

if (null === $value) {
return;
}

$context = $this->context;
$validator = $context->getValidator()->inContext($context);

foreach ($constraint->constraints as $c) {
$validator->validate($value, $c);
}
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy