Example #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;
 }
Example #2
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;
 }
Example #3
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;
 }
Example #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);
 }
Example #5
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);
 }