Skip to content

Commit d4b5312

Browse files
authored
Merge pull request #741 from dunglas/validator
Improve the quality of some other methods
2 parents 7ac1866 + 1b06aea commit d4b5312

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

src/Bridge/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\Constraints\NotBlank;
1818
use Symfony\Component\Validator\Constraints\NotNull;
1919
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
20+
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;
2021

2122
/**
2223
* Decorates a metadata loader using the validator.
@@ -39,24 +40,6 @@ public function __construct(ValidatorMetadataFactoryInterface $validatorMetadata
3940
$this->decorated = $decorated;
4041
}
4142

42-
/**
43-
* Is this constraint making the related property required?
44-
*
45-
* @param Constraint $constraint
46-
*
47-
* @return bool
48-
*/
49-
private function isRequired(Constraint $constraint) : bool
50-
{
51-
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
52-
if ($constraint instanceof $requiredConstraint) {
53-
return true;
54-
}
55-
}
56-
57-
return false;
58-
}
59-
6043
/**
6144
* {@inheritdoc}
6245
*/
@@ -68,22 +51,9 @@ public function create(string $resourceClass, string $name, array $options = [])
6851
}
6952

7053
$validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass);
71-
7254
foreach ($validatorClassMetadata->getPropertyMetadata($name) as $validatorPropertyMetadata) {
7355
if (isset($options['validation_groups'])) {
74-
foreach ($options['validation_groups'] as $validationGroup) {
75-
if (!is_string($validationGroup)) {
76-
continue;
77-
}
78-
79-
foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) {
80-
if ($this->isRequired($constraint)) {
81-
return $propertyMetadata->withRequired(true);
82-
}
83-
}
84-
}
85-
86-
return $propertyMetadata->withRequired(false);
56+
return $propertyMetadata->withRequired($this->isRequiredByGroups($validatorPropertyMetadata, $options));
8757
}
8858

8959
foreach ($validatorPropertyMetadata->findConstraints($validatorClassMetadata->getDefaultGroup()) as $constraint) {
@@ -97,4 +67,47 @@ public function create(string $resourceClass, string $name, array $options = [])
9767

9868
return $propertyMetadata->withRequired(false);
9969
}
70+
71+
/**
72+
* Tests if the property is required because of its validation groups.
73+
*
74+
* @param ValidatorPropertyMetadataInterface $validatorPropertyMetadata
75+
* @param array $options
76+
*
77+
* @return bool
78+
*/
79+
private function isRequiredByGroups(ValidatorPropertyMetadataInterface $validatorPropertyMetadata, array $options) : bool
80+
{
81+
foreach ($options['validation_groups'] as $validationGroup) {
82+
if (!is_string($validationGroup)) {
83+
continue;
84+
}
85+
86+
foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) {
87+
if ($this->isRequired($constraint)) {
88+
return true;
89+
}
90+
}
91+
}
92+
93+
return false;
94+
}
95+
96+
/**
97+
* Is this constraint making the related property required?
98+
*
99+
* @param Constraint $constraint
100+
*
101+
* @return bool
102+
*/
103+
private function isRequired(Constraint $constraint) : bool
104+
{
105+
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
106+
if ($constraint instanceof $requiredConstraint) {
107+
return true;
108+
}
109+
}
110+
111+
return false;
112+
}
100113
}

0 commit comments

Comments
 (0)