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]; }
/** * {@inheritdoc} */ public function getMetadataFor($value) { if (!is_object($value) && !is_string($value)) { throw new NoSuchMetadataException(sprintf('Cannot create metadata for non-objects. Got: %s', gettype($value))); } $class = ltrim(is_object($value) ? get_class($value) : $value, '\\'); if (isset($this->loadedClasses[$class])) { return $this->loadedClasses[$class]; } if (null !== $this->cache && false !== ($this->loadedClasses[$class] = $this->cache->read($class))) { return $this->loadedClasses[$class]; } if (!class_exists($class) && !interface_exists($class)) { throw new NoSuchMetadataException(sprintf('The class or interface "%s" does not exist.', $class)); } $metadata = new ClassMetadata($class); // Include constraints from the parent class if ($parent = $metadata->getReflectionClass()->getParentClass()) { $metadata->mergeConstraints($this->getMetadataFor($parent->name)); } // Include constraints from all implemented interfaces foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) { if ('Symfony\\Component\\Validator\\GroupSequenceProviderInterface' === $interface->name) { continue; } $metadata->mergeConstraints($this->getMetadataFor($interface->name)); } if (null !== $this->loader) { $this->loader->loadClassMetadata($metadata); } if (null !== $this->cache) { $this->cache->write($metadata); } return $this->loadedClasses[$class] = $metadata; }
public function getClassMetadata($class) { $class = ltrim($class, '\\'); if (isset($this->loadedClasses[$class])) { return $this->loadedClasses[$class]; } if (null !== $this->cache && false !== ($this->loadedClasses[$class] = $this->cache->read($class))) { return $this->loadedClasses[$class]; } $metadata = new ClassMetadata($class); // Include constraints from the parent class if ($parent = $metadata->getReflectionClass()->getParentClass()) { $metadata->mergeConstraints($this->getClassMetadata($parent->name)); } // Include constraints from all implemented interfaces foreach ($metadata->getReflectionClass()->getInterfaces() as $interface) { if ('Symfony\\Component\\Validator\\GroupSequenceProviderInterface' === $interface->name) { continue; } $metadata->mergeConstraints($this->getClassMetadata($interface->name)); } if (null !== $this->loader) { $this->loader->loadClassMetadata($metadata); } if (null !== $this->cache) { $this->cache->write($metadata); } return $this->loadedClasses[$class] = $metadata; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $annotClass = 'Symfony\\Component\\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; }
public function loadClassMetadata(ClassMetadata $metadata) { /** @var \ReflectionClass $refClass */ $refClass = $metadata->getReflectionClass(); $baseClass = $this->classNameProvider->getBaseClass($refClass->name); if (!$baseClass) { return false; } if (!array_key_exists($baseClass, $this->config->models)) { return false; } foreach ($this->config->models[$baseClass]->properties as $propName => $property) { if ($property->validation) { if (!is_array($property->validation)) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation must be array"); } foreach ($property->validation as $index => $rule) { if (!is_array($rule) or count($rule) != 1) { throw new \LogicException("Configuration error: {$baseClass}:{$propName}:validation: rule #{$index} must be array of 1 element which contains array"); } $constraintClass = array_keys($rule)[0]; $constraintOptions = $rule[$constraintClass]; $fullClass = $this->getFullConstraintClass($constraintClass); $metadata->addGetterConstraint($propName, new $fullClass($constraintOptions)); } } } return true; }
public function loadClassMetadata(ClassMetadata $metadata) { $class_name = $metadata->getReflectionClass()->getName(); if ($this->loader->hasAdminClass($class_name)) { $admin = $this->loader->getAdminByClass($class_name); $admin->loadValidatorMetadata($metadata); return true; } }
/** * {@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; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->getName(); $loaded = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addConstraint($constraint); } } elseif ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $loaded = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->getName(), $constraint); } $loaded = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->getName() == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { // TODO: clean this up $name = lcfirst(substr($method->getName(), 0, 3) == 'get' ? substr($method->getName(), 3) : substr($method->getName(), 2)); if ($constraint instanceof Set) { foreach ($constraint->constraints as $constraint) { $metadata->addGetterConstraint($name, $constraint); } } elseif ($constraint instanceof Constraint) { $metadata->addGetterConstraint($name, $constraint); } $loaded = true; } } } return $loaded; }
/** * {@inheritDoc} */ public function loadClassMetadata(ClassMetadata $metadata) { /** @var \ReflectionClass $reflClass */ $reflClass = $metadata->getReflectionClass(); if (!$reflClass->isInterface() && $reflClass->hasMethod($this->methodName)) { $reflMethod = $reflClass->getMethod($this->methodName); if (!$reflMethod->isStatic()) { throw new MappingException(sprintf('The method %s::%s should be static', $reflClass->name, $this->methodName)); } if ($reflMethod->getDeclaringClass()->name != $reflClass->name) { return false; } $reflMethod->invoke(null, $metadata); return true; } return false; }
/** * {@inheritdoc} */ public function loadClassMetadata(ClassMetadata $metadata) { $reflClass = $metadata->getReflectionClass(); $className = $reflClass->name; $success = false; foreach ($this->reader->getClassAnnotations($reflClass) as $constraint) { if ($constraint instanceof GroupSequence) { $metadata->setGroupSequence($constraint->groups); } elseif ($constraint instanceof GroupSequenceProvider) { $metadata->setGroupSequenceProvider(true); } elseif ($constraint instanceof Constraint) { $metadata->addConstraint($constraint); } $success = true; } foreach ($reflClass->getProperties() as $property) { if ($property->getDeclaringClass()->name == $className) { foreach ($this->reader->getPropertyAnnotations($property) as $constraint) { if ($constraint instanceof Constraint) { $metadata->addPropertyConstraint($property->name, $constraint); } $success = true; } } } foreach ($reflClass->getMethods() as $method) { if ($method->getDeclaringClass()->name == $className) { foreach ($this->reader->getMethodAnnotations($method) as $constraint) { if ($constraint instanceof Callback) { $constraint->callback = $method->getName(); $constraint->methods = null; $metadata->addConstraint($constraint); } elseif ($constraint instanceof Constraint) { if (preg_match('/^(get|is|has)(.+)$/i', $method->name, $matches)) { $metadata->addGetterConstraint(lcfirst($matches[2]), $constraint); } else { throw new MappingException(sprintf('The constraint on "%s::%s" cannot be added. Constraints can only be added on methods beginning with "get", "is" or "has".', $className, $method->name)); } } $success = true; } } } return $success; }
public function testLoadClassMetadata() { $loader = new AnnotationLoader(); $metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity'); $expected->addConstraint(new NotNull()); $expected->addConstraint(new ConstraintA()); $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); }
public function testLoadGroupSequenceProviderAnnotation() { $loader = new AnnotationLoader(new AnnotationReader()); $metadata = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $loader->loadClassMetadata($metadata); $expected = new ClassMetadata('Symfony\\Component\\Validator\\Tests\\Fixtures\\GroupSequenceProviderEntity'); $expected->setGroupSequenceProvider(true); $expected->getReflectionClass(); $this->assertEquals($expected, $metadata); }