Skip to content

Commit

Permalink
Merge pull request #741 from dunglas/validator
Browse files Browse the repository at this point in the history
Improve the quality of some other methods
  • Loading branch information
dunglas authored Sep 9, 2016
2 parents 7ac1866 + 1b06aea commit d4b5312
Showing 1 changed file with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface as ValidatorMetadataFactoryInterface;
use Symfony\Component\Validator\Mapping\PropertyMetadataInterface as ValidatorPropertyMetadataInterface;

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

/**
* Is this constraint making the related property required?
*
* @param Constraint $constraint
*
* @return bool
*/
private function isRequired(Constraint $constraint) : bool
{
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
if ($constraint instanceof $requiredConstraint) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
Expand All @@ -68,22 +51,9 @@ public function create(string $resourceClass, string $name, array $options = [])
}

$validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($resourceClass);

foreach ($validatorClassMetadata->getPropertyMetadata($name) as $validatorPropertyMetadata) {
if (isset($options['validation_groups'])) {
foreach ($options['validation_groups'] as $validationGroup) {
if (!is_string($validationGroup)) {
continue;
}

foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) {
if ($this->isRequired($constraint)) {
return $propertyMetadata->withRequired(true);
}
}
}

return $propertyMetadata->withRequired(false);
return $propertyMetadata->withRequired($this->isRequiredByGroups($validatorPropertyMetadata, $options));
}

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

return $propertyMetadata->withRequired(false);
}

/**
* Tests if the property is required because of its validation groups.
*
* @param ValidatorPropertyMetadataInterface $validatorPropertyMetadata
* @param array $options
*
* @return bool
*/
private function isRequiredByGroups(ValidatorPropertyMetadataInterface $validatorPropertyMetadata, array $options) : bool
{
foreach ($options['validation_groups'] as $validationGroup) {
if (!is_string($validationGroup)) {
continue;
}

foreach ($validatorPropertyMetadata->findConstraints($validationGroup) as $constraint) {
if ($this->isRequired($constraint)) {
return true;
}
}
}

return false;
}

/**
* Is this constraint making the related property required?
*
* @param Constraint $constraint
*
* @return bool
*/
private function isRequired(Constraint $constraint) : bool
{
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
if ($constraint instanceof $requiredConstraint) {
return true;
}
}

return false;
}
}

0 comments on commit d4b5312

Please sign in to comment.