Skip to content

Commit

Permalink
Improve the quality of some other methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Sep 8, 2016
1 parent 399d4b3 commit fb71f9e
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|null
*/
private function isRequiredByGroups(ValidatorPropertyMetadataInterface $validatorPropertyMetadata, array $options)
{
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 fb71f9e

Please sign in to comment.