Last active
August 29, 2015 14:05
-
-
Save spolischook/92c18fc09c25dc2630a0 to your computer and use it in GitHub Desktop.
How to dump asserts for Entity/Document in Symfony2
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 | |
| $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