Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 7.0:
  fix compatibility with Twig 3.10
  [Strings][EnglishInflector] Fix incorrect pluralisation of 'Album'
  handle union and intersection types for cascaded validations
  move wiring of the property info extractor to the ObjectNormalizer
  restore deprecated properties
  move Process component dep to require-dev
  Remove calls to `onConsecutiveCalls()`
  fix: remove unwanted type cast
  accept AbstractAsset instances when filtering schemas
  better distinguish URL schemes and windows drive letters
  handle edge cases when constructing constraints with named arguments
  convert empty CSV header names into numeric keys
  • Loading branch information
derrabus committed May 2, 2024
2 parents 8f89f35 + 0edd0a5 commit b8ab23c
Show file tree
Hide file tree
Showing 14 changed files with 223 additions and 4 deletions.
31 changes: 30 additions & 1 deletion Mapping/ClassMetadata.php
Expand Up @@ -198,7 +198,7 @@ public function addConstraint(Constraint $constraint): static
continue;
}

if ($property->hasType() && (('array' === $type = $property->getType()->getName()) || class_exists($type))) {
if ($this->canCascade($property->getType())) {
$this->addPropertyConstraint($property->getName(), new Valid());
}
}
Expand Down Expand Up @@ -483,4 +483,33 @@ private function checkConstraint(Constraint $constraint): void
}
}
}

private function canCascade(?\ReflectionType $type = null): bool
{
if (null === $type) {
return false;
}

if ($type instanceof \ReflectionIntersectionType) {
foreach ($type->getTypes() as $nestedType) {
if ($this->canCascade($nestedType)) {
return true;
}
}

return false;
}

if ($type instanceof \ReflectionUnionType) {
foreach ($type->getTypes() as $nestedType) {
if (!$this->canCascade($nestedType)) {
return false;
}
}

return true;
}

return $type instanceof \ReflectionNamedType && (\in_array($type->getName(), ['array', 'null'], true) || class_exists($type->getName()));
}
}
12 changes: 12 additions & 0 deletions Mapping/Loader/AbstractLoader.php
Expand Up @@ -85,6 +85,18 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
}

if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
if (null === $options) {
return new $className();
}

if (!\is_array($options)) {
return new $className($options);
}

if (1 === \count($options) && isset($options['value'])) {
return new $className($options['value']);
}

return new $className(...$options);
}

Expand Down
8 changes: 7 additions & 1 deletion Mapping/Loader/XmlFileLoader.php
Expand Up @@ -80,7 +80,9 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
foreach ($nodes as $node) {
if (\count($node) > 0) {
if (\count($node->value) > 0) {
$options = $this->parseValues($node->value);
$options = [
'value' => $this->parseValues($node->value),
];
} elseif (\count($node->constraint) > 0) {
$options = $this->parseConstraints($node->constraint);
} elseif (\count($node->option) > 0) {
Expand All @@ -94,6 +96,10 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
$options = null;
}

if (isset($options['groups']) && !\is_array($options['groups'])) {
$options['groups'] = (array) $options['groups'];
}

$constraints[] = $this->newConstraint((string) $node['name'], $options);
}

Expand Down
6 changes: 6 additions & 0 deletions Mapping/Loader/YamlFileLoader.php
Expand Up @@ -86,6 +86,12 @@ protected function parseNodes(array $nodes): array
$options = $this->parseNodes($options);
}

if (null !== $options && (!\is_array($options) || array_is_list($options))) {
$options = [
'value' => $options,
];
}

$values[] = $this->newConstraint(key($childNodes), $options);
} else {
if (\is_array($childNodes)) {
Expand Down
17 changes: 17 additions & 0 deletions Tests/Fixtures/CascadingEntityIntersection.php
@@ -0,0 +1,17 @@
<?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\Tests\Fixtures;

class CascadingEntityIntersection
{
public CascadedChild&\stdClass $classes;
}
25 changes: 25 additions & 0 deletions Tests/Fixtures/CascadingEntityUnion.php
@@ -0,0 +1,25 @@
<?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\Tests\Fixtures;

class CascadingEntityUnion
{
public CascadedChild|\stdClass $classes;
public CascadedChild|array $classAndArray;
public CascadedChild|null $classAndNull;
public array|null $arrayAndNull;
public CascadedChild|array|null $classAndArrayAndNull;
public int|string $scalars;
public int|null $scalarAndNull;
public CascadedChild|int $classAndScalar;
public array|int $arrayAndScalar;
}
30 changes: 30 additions & 0 deletions Tests/Mapping/ClassMetadataTest.php
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\Validator\Mapping\CascadingStrategy;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntity;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntityIntersection;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntityUnion;
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
Expand Down Expand Up @@ -346,6 +348,34 @@ public function testCascadeConstraint()
], $metadata->getConstrainedProperties());
}

public function testCascadeConstraintWithUnionTypeProperties()
{
$metadata = new ClassMetadata(CascadingEntityUnion::class);
$metadata->addConstraint(new Cascade());

$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
$this->assertCount(5, $metadata->properties);
$this->assertSame([
'classes',
'classAndArray',
'classAndNull',
'arrayAndNull',
'classAndArrayAndNull',
], $metadata->getConstrainedProperties());
}

public function testCascadeConstraintWithIntersectionTypeProperties()
{
$metadata = new ClassMetadata(CascadingEntityIntersection::class);
$metadata->addConstraint(new Cascade());

$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
$this->assertCount(1, $metadata->properties);
$this->assertSame([
'classes',
], $metadata->getConstrainedProperties());
}

public function testCascadeConstraintWithExcludedProperties()
{
$metadata = new ClassMetadata(CascadingEntity::class);
Expand Down
33 changes: 33 additions & 0 deletions Tests/Mapping/Loader/Fixtures/ConstraintWithNamedArguments.php
@@ -0,0 +1,33 @@
<?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\Tests\Mapping\Loader\Fixtures;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

class ConstraintWithNamedArguments extends Constraint
{
public $choices;

#[HasNamedArguments]
public function __construct(array|string|null $choices = [], ?array $groups = null)
{
parent::__construct([], $groups);

$this->choices = $choices;
}

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
@@ -0,0 +1,29 @@
<?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\Tests\Mapping\Loader\Fixtures;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

class ConstraintWithoutValueWithNamedArguments extends Constraint
{
#[HasNamedArguments]
public function __construct(?array $groups = null)
{
parent::__construct([], $groups);
}

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
4 changes: 2 additions & 2 deletions Tests/Mapping/Loader/PropertyInfoLoaderTest.php
Expand Up @@ -114,7 +114,7 @@ public function getTypes(string $class, string $property, array $context = []):
$propertyAccessExtractor = $this->createMock(PropertyAccessExtractorInterface::class);
$propertyAccessExtractor
->method('isWritable')
->will($this->onConsecutiveCalls(
->willReturn(
true,
true,
true,
Expand All @@ -127,7 +127,7 @@ public function getTypes(string $class, string $property, array $context = []):
true,
false,
true
))
)
;

$propertyInfoLoader = new PropertyInfoLoader($propertyListExtractor, $propertyTypeExtractor, $propertyAccessExtractor, '{.*}');
Expand Down
5 changes: 5 additions & 0 deletions Tests/Mapping/Loader/XmlFileLoaderTest.php
Expand Up @@ -32,6 +32,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments;

class XmlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -66,6 +68,9 @@ public function testLoadClassMetadata()
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback']));
$expected->addConstraint(new Traverse(false));
$expected->addConstraint(new ConstraintWithNamedArguments('foo'));
$expected->addConstraint(new ConstraintWithNamedArguments(['foo', 'bar']));
$expected->addConstraint(new ConstraintWithoutValueWithNamedArguments(['foo']));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(['min' => 3]));
$expected->addPropertyConstraint('firstName', new Choice(['A', 'B']));
Expand Down
5 changes: 5 additions & 0 deletions Tests/Mapping/Loader/YamlFileLoaderTest.php
Expand Up @@ -29,6 +29,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments;

class YamlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -110,6 +112,9 @@ public function testLoadClassMetadata()
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback']));
$expected->addConstraint(new ConstraintWithoutValueWithNamedArguments());
$expected->addConstraint(new ConstraintWithNamedArguments('foo'));
$expected->addConstraint(new ConstraintWithNamedArguments(['foo', 'bar']));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(['min' => 3]));
$expected->addPropertyConstraint('firstName', new Choice(['A', 'B']));
Expand Down
16 changes: 16 additions & 0 deletions Tests/Mapping/Loader/constraint-mapping.xml
Expand Up @@ -36,6 +36,22 @@
false
</constraint>

<!-- Constraint with named arguments support with scalar value -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
foo
</constraint>

<!-- Constraint with named arguments support with array value -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
<value>foo</value>
<value>bar</value>
</constraint>

<!-- Constraint with named arguments support with exactly one group -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments">
<option name="groups">foo</option>
</constraint>

<!-- PROPERTY CONSTRAINTS -->

<property name="firstName">
Expand Down
6 changes: 6 additions & 0 deletions Tests/Mapping/Loader/constraint-mapping.yml
Expand Up @@ -15,6 +15,12 @@ Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
- Callback: validateMe
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
# Constraint with named arguments support without value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments: ~
# Constraint with named arguments support with scalar value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: foo
# Constraint with named arguments support with array value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: [foo, bar]

properties:
firstName:
Expand Down

0 comments on commit b8ab23c

Please sign in to comment.