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

Skip to content

[Validator] [WIP] Composite constraints as attributes #42102

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 1 commit 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
23 changes: 23 additions & 0 deletions src/Symfony/Component/Validator/Constraints/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,39 @@

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <[email protected]>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class All extends Composite
{
public $constraints = [];

/**
* {@inheritdoc}
*
* @param mixed[]|Constraint[]|Constraint $constraints The nested constraints or an array of options
*/
public function __construct($constraints = null, array $groups = null)
{
if ($constraints instanceof Constraint) {
$options = ['constraints' => $constraints];
} elseif (!\is_array($constraints)) {
throw new \TypeError(sprintf('%s: Parameter #1 is extected to be an array or an instance of %s, %s given.', __METHOD__, Constraint::class, get_debug_type($constraints)));
} elseif (\array_key_exists('constraints', $constraints) || \array_key_exists('value', $constraints)) {
$options = $constraints;
} else {
$options = ['constraints' => $constraints];
}

parent::__construct($options, $groups);
}

public function getDefaultOption()
{
return 'constraints';
Expand Down
26 changes: 20 additions & 6 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*
* @author Bernhard Schussek <[email protected]>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Collection extends Composite
{
public const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
Expand All @@ -38,15 +39,28 @@ class Collection extends Composite
/**
* {@inheritdoc}
*/
public function __construct($options = null)
{
public function __construct(
array $fields = null,
bool $allowExtraFields = null,
bool $allowMissingFields = null,
string $extraFieldsMessage = null,
string $missingFieldsMessage = null,
array $groups = null
) {
// no known options set? $options is the fields array
if (\is_array($options)
&& !array_intersect(array_keys($options), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) {
$options = ['fields' => $options];
if (\is_array($fields)
&& !array_intersect(array_keys($fields), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) {
$options = ['fields' => $fields];
} else {
$options = $fields;
}

parent::__construct($options);
parent::__construct($options, $groups);

$this->allowExtraFields = $allowExtraFields ?? $this->allowExtraFields;
$this->allowMissingFields = $allowMissingFields ?? $this->allowMissingFields;
$this->extraFieldsMessage = $extraFieldsMessage ?? $this->extraFieldsMessage;
$this->missingFieldsMessage = $missingFieldsMessage ?? $this->missingFieldsMessage;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ abstract class Composite extends Constraint
* cached. When constraints are loaded from the cache, no more group
* checks need to be done.
*/
public function __construct($options = null)
public function __construct($options = null, array $groups = null)
{
parent::__construct($options);
parent::__construct($options, $groups);

$this->initializeNestedConstraints();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

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

namespace Symfony\Component\Validator\Tests\Fixtures\CompositeAttribute;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceB;
use Symfony\Component\Validator\Tests\Fixtures\CallbackClass;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;

#[
ConstraintA,
Assert\GroupSequence(['Foo', 'Entity']),
Assert\Callback([CallbackClass::class, 'callback']),
]
class Entity extends EntityParent implements EntityInterfaceB
{
#[
Assert\NotNull,
Assert\Range(min: 3),
Assert\All([new Assert\NotNull(), new Assert\Range(min: 3)]),
Assert\All(constraints: [new Assert\NotNull(), new Assert\Range(min: 3)]),
Assert\Collection(fields: [
'foo' => [new Assert\NotNull(), new Assert\Range(min: 3)],
'bar' => new Assert\Range(min: 5),
]),
Assert\Choice(choices: ['A', 'B'], message: 'Must be one of %choices%'),
]
public $firstName;
#[Assert\Valid]
public $childA;
#[Assert\Valid]
public $childB;
protected $lastName;
public $reference;
public $reference2;
private $internal;
public $data = 'Overridden data';
public $initialized = false;

public function __construct($internal = null)
{
$this->internal = $internal;
}

public function getFirstName()
{
return $this->firstName;
}

public function getInternal()
{
return $this->internal.' from getter';
}

public function setLastName($lastName)
{
$this->lastName = $lastName;
}

#[Assert\NotNull]
public function getLastName()
{
return $this->lastName;
}

public function getValid()
{
}

#[Assert\IsTrue]
public function isValid()
{
return 'valid';
}

#[Assert\IsTrue]
public function hasPermissions()
{
return 'permissions';
}

public function getData()
{
return 'Overridden data';
}

#[Assert\Callback(payload: 'foo')]
public function validateMe(ExecutionContextInterface $context)
{
}

#[Assert\Callback]
public static function validateMeStatic($object, ExecutionContextInterface $context)
{
}

public function getChildA(): mixed
{
return $this->childA;
}

public function setChildA(mixed $childA)
{
$this->childA = $childA;
}

public function getChildB(): mixed
{
return $this->childB;
}

public function setChildB(mixed $childB)
{
$this->childB = $childB;
}

public function getReference()
{
return $this->reference;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

namespace Symfony\Component\Validator\Tests\Fixtures\CompositeAttribute;

use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceA;

class EntityParent implements EntityInterfaceA
{
protected $firstName;
private $internal;
private $data = 'Data';
private $child;

#[NotNull]
protected $other;

public function getData()
{
return 'Data';
}

public function getChild()
{
return $this->child;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

namespace Symfony\Component\Validator\Tests\Fixtures\CompositeAttribute;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\GroupSequenceProviderInterface;

#[Assert\GroupSequenceProvider]
class GroupSequenceProviderEntity implements GroupSequenceProviderInterface
{
public $firstName;
public $lastName;

protected $sequence = [];

public function __construct($sequence)
{
$this->sequence = $sequence;
}

public function getGroupSequence()
{
return $this->sequence;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@ public function provideNamespaces(): iterable
if (\PHP_VERSION_ID >= 80000) {
yield 'attributes' => ['Symfony\Component\Validator\Tests\Fixtures\Attribute'];
}
if (\PHP_VERSION_ID >= 80100) {
yield 'composite attributes' => ['Symfony\Component\Validator\Tests\Fixtures\CompositeAttribute'];
}
}
}