Skip to content

Instantly share code, notes, and snippets.

@spolischook
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save spolischook/92c18fc09c25dc2630a0 to your computer and use it in GitHub Desktop.

Select an option

Save spolischook/92c18fc09c25dc2630a0 to your computer and use it in GitHub Desktop.
How to dump asserts for Entity/Document in Symfony2
<?php
$user = new User();
/** @var \Symfony\Component\Validator\Mapping\ClassMetadataFactory $metadataFactory */
$metadataFactory = $this->get('validator.mapping.class_metadata_factory');
$metadata = $metadataFactory->getMetadataFor($user);
$constraints = [];
echo "Constraints:";
foreach ($metadata->properties as $property) {
foreach ($property->constraints as $constraint) {
$class = get_class($constraint);
$arrayNamespace = explode("\\", $class);
$class = $arrayNamespace[count($arrayNamespace) -1];
if (isset($constraint->max)) {
$constraints[$property->getName()][$class]['max'] = $constraint->max;
}
if (isset($constraint->min)) {
$constraints[$property->getName()][$class]['min'] = $constraint->min;
}
if (isset($constraint->pattern)) {
$constraints[$property->getName()][$class]['pattern'] = $constraint->pattern;
}
$constraints[$property->getName()][$class]['groups'] = $constraint->groups;
}
}
echo json_encode($constraints); exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment