Esempio n. 1
0
 /**
  * {@inheritDoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $annotClass = 'Symfony\\Components\\Validator\\Constraints\\Validation';
     $reflClass = $metadata->getReflectionClass();
     $loaded = false;
     if ($annot = $this->reader->getClassAnnotation($reflClass, $annotClass)) {
         foreach ($annot->constraints as $constraint) {
             $metadata->addConstraint($constraint);
         }
         $loaded = true;
     }
     foreach ($reflClass->getProperties() as $property) {
         if ($annot = $this->reader->getPropertyAnnotation($property, $annotClass)) {
             foreach ($annot->constraints as $constraint) {
                 $metadata->addPropertyConstraint($property->getName(), $constraint);
             }
             $loaded = true;
         }
     }
     foreach ($reflClass->getMethods() as $method) {
         if ($annot = $this->reader->getMethodAnnotation($method, $annotClass)) {
             foreach ($annot->constraints as $constraint) {
                 // TODO: clean this up
                 $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2));
                 $metadata->addGetterConstraint($name, $constraint);
             }
             $loaded = true;
         }
     }
     return $loaded;
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $reflClass = $metadata->getReflectionClass();
     if ($reflClass->hasMethod($this->methodName)) {
         $reflMethod = $reflClass->getMethod($this->methodName);
         if (!$reflMethod->isStatic()) {
             throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->getName(), $this->methodName));
         }
         $reflMethod->invoke(null, $metadata);
         return true;
     }
     return false;
 }
Esempio n. 3
0
 public function testMergeConstraintsKeepsPrivateMembersSeperate()
 {
     $parent = new ClassMetadata(self::PARENTCLASS);
     $parent->addPropertyConstraint('internal', new ConstraintA());
     $this->metadata->mergeConstraints($parent);
     $this->metadata->addPropertyConstraint('internal', new ConstraintA());
     $parentConstraints = array(new ConstraintA(array('groups' => array('Default', 'EntityParent', 'Entity'))));
     $constraints = array(new ConstraintA(array('groups' => array('Default', 'Entity'))));
     $members = $this->metadata->getMemberMetadatas('internal');
     $this->assertEquals(2, count($members));
     $this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
     $this->assertEquals($parentConstraints, $members[0]->getConstraints());
     $this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
     $this->assertEquals($constraints, $members[1]->getConstraints());
 }
Esempio n. 4
0
 public function testLoadClassMetadata()
 {
     $loader = new YamlFileLoader(__DIR__ . '/constraint-mapping.yml');
     $metadata = new ClassMetadata('Symfony\\Tests\\Components\\Validator\\Fixtures\\Entity');
     $loader->loadClassMetadata($metadata);
     $expected = new ClassMetadata('Symfony\\Tests\\Components\\Validator\\Fixtures\\Entity');
     $expected->addConstraint(new NotNull());
     $expected->addConstraint(new Min(3));
     $expected->addConstraint(new Choice(array('A', 'B')));
     $expected->addConstraint(new All(array(new NotNull(), new Min(3))));
     $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3)))));
     $expected->addConstraint(new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => array(new Min(5))))));
     $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B'))));
     $expected->addGetterConstraint('lastName', new NotNull());
     $this->assertEquals($expected, $metadata);
 }
Esempio n. 5
0
 /**
  * {@inheritDoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     if (is_null($this->classes)) {
         $this->classes = Yaml::load($this->file);
     }
     // TODO validation
     if (isset($this->classes[$metadata->getClassName()])) {
         $yaml = $this->classes[$metadata->getClassName()];
         if (isset($yaml['constraints'])) {
             foreach ($this->parseNodes($yaml['constraints']) as $constraint) {
                 $metadata->addConstraint($constraint);
             }
         }
         if (isset($yaml['properties'])) {
             foreach ($yaml['properties'] as $property => $constraints) {
                 foreach ($this->parseNodes($constraints) as $constraint) {
                     $metadata->addPropertyConstraint($property, $constraint);
                 }
             }
         }
         if (isset($yaml['getters'])) {
             foreach ($yaml['getters'] as $getter => $constraints) {
                 foreach ($this->parseNodes($constraints) as $constraint) {
                     $metadata->addGetterConstraint($getter, $constraint);
                 }
             }
         }
         return true;
     }
     return false;
 }
Esempio n. 6
0
 /**
  * {@inheritDoc}
  */
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     if (is_null($this->classes)) {
         $this->classes = array();
         $xml = $this->parseFile($this->file);
         foreach ($xml->class as $class) {
             $this->classes[(string) $class['name']] = $class;
         }
     }
     if (isset($this->classes[$metadata->getClassName()])) {
         $xml = $this->classes[$metadata->getClassName()];
         foreach ($this->parseConstraints($xml->constraint) as $constraint) {
             $metadata->addConstraint($constraint);
         }
         foreach ($xml->property as $property) {
             foreach ($this->parseConstraints($property->constraint) as $constraint) {
                 $metadata->addPropertyConstraint((string) $property['name'], $constraint);
             }
         }
         foreach ($xml->getter as $getter) {
             foreach ($this->parseConstraints($getter->constraint) as $constraint) {
                 $metadata->addGetterConstraint((string) $getter['property'], $constraint);
             }
         }
         return true;
     }
     return false;
 }
Esempio n. 7
0
 public function getClassMetadata($class)
 {
     $class = ltrim($class, '\\');
     if (!isset($this->loadedClasses[$class])) {
         if ($this->cache !== null && $this->cache->has($class)) {
             $this->loadedClasses[$class] = $this->cache->read($class);
         } else {
             $metadata = new ClassMetadata($class);
             // Include constraints from the parent class
             if ($parent = $metadata->getReflectionClass()->getParentClass()) {
                 $metadata->mergeConstraints($this->getClassMetadata($parent->getName()));
             }
             // Include constraints from all implemented interfaces
             foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) {
                 $metadata->mergeConstraints($this->getClassMetadata($interface->getName()));
             }
             $this->loader->loadClassMetadata($metadata);
             $this->loadedClasses[$class] = $metadata;
             if ($this->cache !== null) {
                 $this->cache->write($metadata);
             }
         }
     }
     return $this->loadedClasses[$class];
 }
Esempio n. 8
0
 protected function buildGroupChain(ClassMetadata $metadata, $groups)
 {
     if (is_null($groups)) {
         $groups = array(Constraint::DEFAULT_GROUP);
     } else {
         $groups = (array) $groups;
     }
     $chain = new GroupChain();
     foreach ($groups as $group) {
         if ($group == Constraint::DEFAULT_GROUP && $metadata->hasGroupSequence()) {
             $chain->addGroupSequence($metadata->getGroupSequence());
         } else {
             $chain->addGroup($group);
         }
     }
     return $chain;
 }
Esempio n. 9
0
 /**
  * Merges the constraints of the given metadata into this object
  *
  * @param ClassMetadata $source  The source metadata
  */
 public function mergeConstraints(ClassMetadata $source)
 {
     foreach ($source->getConstraints() as $constraint) {
         $this->addConstraint(clone $constraint);
     }
     foreach ($source->getConstrainedProperties() as $property) {
         foreach ($source->getMemberMetadatas($property) as $member) {
             $member = clone $member;
             foreach ($member->getConstraints() as $constraint) {
                 $constraint->addImplicitGroupName($this->getShortClassName());
             }
             $this->addMemberMetadata($member);
             if (!$member->isPrivate()) {
                 $property = $member->getPropertyName();
                 if ($member instanceof PropertyMetadata && !isset($this->properties[$property])) {
                     $this->properties[$property] = $member;
                 } else {
                     if ($member instanceof GetterMetadata && !isset($this->getters[$property])) {
                         $this->getters[$property] = $member;
                     }
                 }
             }
         }
     }
 }
Esempio n. 10
0
 public function loadClassMetadata(ClassMetadata $metadata)
 {
     $metadata->addConstraint(new ConstraintA());
 }
Esempio n. 11
0
 public function walkPropertyValue(ClassMetadata $metadata, $property, $value, $group, $propertyPath)
 {
     foreach ($metadata->getMemberMetadatas($property) as $member) {
         $this->walkMember($member, $value, $group, $propertyPath);
     }
 }
Esempio n. 12
0
 public function testLoadClassMetadata()
 {
     $loader = new AnnotationLoader();
     $metadata = new ClassMetadata('Symfony\\Tests\\Components\\Validator\\Fixtures\\Entity');
     $loader->loadClassMetadata($metadata);
     $expected = new ClassMetadata('Symfony\\Tests\\Components\\Validator\\Fixtures\\Entity');
     $expected->addConstraint(new NotNull());
     $expected->addConstraint(new Min(3));
     $expected->addConstraint(new Choice(array('A', 'B')));
     $expected->addConstraint(new All(array(new NotNull(), new Min(3))));
     $expected->addConstraint(new All(array('constraints' => array(new NotNull(), new Min(3)))));
     $expected->addConstraint(new Collection(array('fields' => array('foo' => array(new NotNull(), new Min(3)), 'bar' => new Min(5)))));
     $expected->addPropertyConstraint('firstName', new Choice(array('message' => 'Must be one of %choices%', 'choices' => array('A', 'B'))));
     $expected->addGetterConstraint('lastName', new NotNull());
     // load reflection class so that the comparison passes
     $expected->getReflectionClass();
     $this->assertEquals($expected, $metadata);
 }