Last active
March 3, 2023 18:33
-
-
Save BoShurik/009cdeef1fb7a43fc1856feaaf317062 to your computer and use it in GitHub Desktop.
Dump Symfony Constraints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Validator\Mapping\ClassMetadataInterface; | |
use Symfony\Component\Validator\Mapping\PropertyMetadata; | |
use Symfony\Component\Validator\Validator\ValidatorInterface; | |
class ConstraintDumper | |
{ | |
public function __construct(private readonly ValidatorInterface $validator) | |
{ | |
} | |
public function dump(string $class): array | |
{ | |
/** @var ClassMetadataInterface $metadata */ | |
$metadata = $this->validator->getMetadataFor($class); | |
$properties = []; | |
foreach ($metadata->getConstrainedProperties() as $property) { | |
$propertyConstraints = []; | |
/** @var PropertyMetadata $propertyMetadata */ | |
foreach ($metadata->getPropertyMetadata($property) as $propertyMetadata) { | |
$reflection = $propertyMetadata->getReflectionMember($class); | |
$type = $reflection->getType(); | |
if (!$type instanceof ReflectionNamedType || $type->isBuiltin()) { | |
$propertyConstraints = array_merge($propertyConstraints, array_map($this->dumpConstraint(...), $propertyMetadata->getConstraints())); | |
} else { | |
$propertyConstraints[] = $this->dump($type->getName()); | |
} | |
} | |
$properties[$property] = $propertyConstraints; | |
} | |
return [ | |
'class' => array_map($this->dumpConstraint(...), $metadata->getConstraints()), | |
'properties' => $properties, | |
]; | |
} | |
private function dumpConstraint(Constraint $constraint): array | |
{ | |
return array_merge([ | |
'constraint' => get_class($constraint), | |
], get_object_vars($constraint)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"class": [], | |
"properties": { | |
"username": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\NotBlank", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"CreateUserModel" | |
], | |
"message": "This value should not be blank.", | |
"allowNull": false, | |
"normalizer": null | |
} | |
], | |
"email": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\NotBlank", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"CreateUserModel" | |
], | |
"message": "This value should not be blank.", | |
"allowNull": false, | |
"normalizer": null | |
}, | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\Email", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"CreateUserModel" | |
], | |
"message": "This value is not a valid email address.", | |
"mode": null, | |
"normalizer": null | |
} | |
], | |
"password": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\NotBlank", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"CreateUserModel" | |
], | |
"message": "This value should not be blank.", | |
"allowNull": false, | |
"normalizer": null | |
} | |
], | |
"age": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\Range", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"CreateUserModel" | |
], | |
"notInRangeMessage": "This value should be between {{ min }} and {{ max }}.", | |
"minMessage": "This value should be {{ limit }} or more.", | |
"maxMessage": "This value should be {{ limit }} or less.", | |
"invalidMessage": "This value should be a valid number.", | |
"invalidDateTimeMessage": "This value should be a valid datetime.", | |
"min": 18, | |
"minPropertyPath": null, | |
"max": 150, | |
"maxPropertyPath": null | |
} | |
], | |
"requisites": [ | |
{ | |
"class": [], | |
"properties": { | |
"bik": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\NotBlank", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"Requisites" | |
], | |
"message": "This value should not be blank.", | |
"allowNull": false, | |
"normalizer": null | |
} | |
], | |
"rs": [ | |
{ | |
"constraint": "Symfony\\Component\\Validator\\Constraints\\NotBlank", | |
"payload": null, | |
"groups": [ | |
"Default", | |
"Requisites" | |
], | |
"message": "This value should not be blank.", | |
"allowNull": false, | |
"normalizer": null | |
} | |
] | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment