Skip to content

Instantly share code, notes, and snippets.

@BoShurik
Last active March 3, 2023 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BoShurik/009cdeef1fb7a43fc1856feaaf317062 to your computer and use it in GitHub Desktop.
Save BoShurik/009cdeef1fb7a43fc1856feaaf317062 to your computer and use it in GitHub Desktop.
Dump Symfony Constraints
<?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));
}
}
{
"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