/**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadataInterface $classMetadata, array $normalizationGroups = null, array $denormalizationGroups = null, array $validationGroups = null)
 {
     $validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($classMetadata->getName());
     foreach ($classMetadata->getAttributesMetadata() as $attributeName => $attributeMetadata) {
         foreach ($validatorClassMetadata->getPropertyMetadata($attributeName) as $propertyMetadata) {
             if (null === $validationGroups) {
                 foreach ($propertyMetadata->findConstraints($validatorClassMetadata->getDefaultGroup()) as $constraint) {
                     if ($this->isRequired($constraint)) {
                         $attributeMetadata = $attributeMetadata->withRequired(true);
                         break 2;
                     }
                 }
             } else {
                 foreach ($validationGroups as $validationGroup) {
                     if (!is_string($validationGroup)) {
                         continue;
                     }
                     foreach ($propertyMetadata->findConstraints($validationGroup) as $constraint) {
                         if ($this->isRequired($constraint)) {
                             $attributeMetadata = $attributeMetadata->withRequired(true);
                             break 3;
                         }
                     }
                 }
             }
         }
         $classMetadata = $classMetadata->withAttributeMetadata($attributeName, $attributeMetadata);
     }
     return $classMetadata;
 }
 /**
  * Gets ClassMetadata of desired class with annotations and others (xml, yml, php) using metadata factory
  *
  * @param string $className
  *
  * @return ClassMetadata Returns ClassMetadata object of desired entity with annotations info
  */
 public function getClassMetadata($className)
 {
     if (!isset($this->classesMetadata[$className])) {
         $this->classesMetadata[$className] = $this->metadataFactory->getMetadataFor($className);
     }
     return $this->classesMetadata[$className];
 }
 /**
  * {@inheritdoc}
  */
 public function loadClassMetadata(ClassMetadata $classMetadata, array $normalizationGroups = null, array $denormalizationGroups = null, array $validationGroups = null)
 {
     $validatorClassMetadata = $this->validatorMetadataFactory->getMetadataFor($classMetadata->getName());
     foreach ($classMetadata->getAttributes() as $attributeMetadata) {
         $attributeName = $attributeMetadata->getName();
         foreach ($validatorClassMetadata->getPropertyMetadata($attributeName) as $propertyMetadata) {
             if (null === $validationGroups) {
                 foreach ($propertyMetadata->findConstraints($validatorClassMetadata->getDefaultGroup()) as $constraint) {
                     if ($this->isRequired($constraint)) {
                         $attributeMetadata->setRequired(true);
                         break 2;
                     }
                 }
             } else {
                 foreach ($validationGroups as $validationGroup) {
                     foreach ($propertyMetadata->findConstraints($validationGroup) as $constraint) {
                         if ($this->isRequired($constraint)) {
                             $attributeMetadata->setRequired(true);
                             break 3;
                         }
                     }
                 }
             }
         }
     }
     return true;
 }
 public function testGenerateClass()
 {
     $constraint = new Range(array('min' => 10));
     $cm = new ClassMetadata(__NAMESPACE__ . '\\TestEntity');
     $cm->addPropertyConstraint('field1', $constraint);
     $this->metadataFactory->expects($this->once())->method('getMetadataFor')->will($this->returnValue($cm));
     $data = $this->generator->generateClass(__NAMESPACE__ . '\\TestEntity');
     $this->assertArrayHasKey('field1', $data);
     $this->assertArrayHasKey('range', $data['field1']);
     $this->assertEquals($constraint, $data['field1']['range']);
 }
 /**
  * @param Node $node
  * @return void
  */
 public function enterNode(Node $node)
 {
     if ($node instanceof Node\Stmt\Namespace_) {
         if (isset($node->name)) {
             $this->namespace = implode('\\', $node->name->parts);
         }
         return;
     }
     if (!$node instanceof Node\Stmt\Class_) {
         return;
     }
     $name = '' === $this->namespace ? $node->name : $this->namespace . '\\' . $node->name;
     if (!class_exists($name)) {
         return;
     }
     $metadata = $this->metadataFactory instanceof ClassMetadataFactoryInterface ? $this->metadataFactory->getClassMetadata($name) : $this->metadataFactory->getMetadataFor($name);
     if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) {
         return;
     }
     $this->extractFromConstraints($metadata->constraints);
     foreach ($metadata->members as $members) {
         foreach ($members as $member) {
             $this->extractFromConstraints($member->constraints);
         }
     }
 }
 /**
  * Generate array representation of constraints that is exported to JSON.
  *
  * @param string $className
  * @return array
  */
 public function generateClass($className)
 {
     $data = array();
     $metadata = $this->metadataFactory->getMetadataFor($className);
     $properties = $metadata->getConstrainedProperties();
     foreach ($properties as $property) {
         $data[$property] = array();
         $constraintsList = $metadata->getPropertyMetadata($property);
         foreach ($constraintsList as $constraints) {
             foreach ($constraints->constraints as $constraint) {
                 $const = $this->translateConstraint($constraint);
                 $data[$property][$this->getConstraintName($const)] = $const;
             }
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  */
 public function validatePropertyValue($object, $property_name, $value, $groups = NULL)
 {
     if (!is_object($object)) {
         throw new \InvalidArgumentException('Passing class name is not supported.');
     } elseif (!$object instanceof TypedDataInterface) {
         throw new \InvalidArgumentException('The passed in object has to be typed data.');
     } elseif (!$object instanceof ListInterface && !$object instanceof ComplexDataInterface) {
         throw new \InvalidArgumentException('Passed data does not contain properties.');
     }
     $data = $object->get($property_name);
     $metadata = $this->metadataFactory->getMetadataFor($data);
     $constraints = $metadata->findConstraints(Constraint::DEFAULT_GROUP);
     return $this->validate($value, $constraints, $groups, TRUE);
 }
 /**
  * Validates an object against the constraints defined for its class.
  *
  * If no metadata is available for the class, but the class is an instance
  * of {@link \Traversable} and the selected traversal strategy allows
  * traversal, the object will be iterated and each nested object will be
  * validated instead.
  *
  * @param object                    $object            The object to cascade
  * @param string                    $propertyPath      The current property path
  * @param string[]                  $groups            The validated groups
  * @param int                       $traversalStrategy The strategy for traversing the
  *                                                     cascaded object
  * @param ExecutionContextInterface $context           The current execution context
  *
  * @throws NoSuchMetadataException      If the object has no associated metadata
  *                                      and does not implement {@link \Traversable}
  *                                      or if traversal is disabled via the
  *                                      $traversalStrategy argument
  * @throws UnsupportedMetadataException If the metadata returned by the
  *                                      metadata factory does not implement
  *                                      {@link ClassMetadataInterface}
  */
 private function validateObject($object, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
 {
     try {
         $classMetadata = $this->metadataFactory->getMetadataFor($object);
         if (!$classMetadata instanceof ClassMetadataInterface) {
             throw new UnsupportedMetadataException(sprintf('The metadata factory should return instances of ' . '"Symfony\\Component\\Validator\\Mapping\\ClassMetadataInterface", ' . 'got: "%s".', is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)));
         }
         $this->validateClassNode($object, spl_object_hash($object), $classMetadata, $propertyPath, $groups, null, $traversalStrategy, $context);
     } catch (NoSuchMetadataException $e) {
         // Rethrow if not Traversable
         if (!$object instanceof \Traversable) {
             throw $e;
         }
         // Rethrow unless IMPLICIT or TRAVERSE
         if (!($traversalStrategy & (TraversalStrategy::IMPLICIT | TraversalStrategy::TRAVERSE))) {
             throw $e;
         }
         $this->validateEachObjectIn($object, $propertyPath, $groups, $context);
     }
 }
Exemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function hasMetadataFor($object)
 {
     return $this->metadataFactory->hasMetadataFor($object);
 }