Exemplo n.º 1
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];
 }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
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);
 }