本文整理汇总了PHP中Symfony\Component\Validator\Mapping\ClassMetadata::getReflectionClass方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::getReflectionClass方法的具体用法?PHP ClassMetadata::getReflectionClass怎么用?PHP ClassMetadata::getReflectionClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::getReflectionClass方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getClassMetadata
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];
}
示例2: getMetadataFor
/**
* {@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;
}
示例3: getClassMetadata
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;
}
示例4: loadClassMetadata
/**
* {@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;
}
示例5: loadClassMetadata
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;
}
示例6: loadClassMetadata
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;
}
}
示例7: loadClassMetadata
/**
* {@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;
}
示例8: loadClassMetadata
/**
* {@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;
}
示例9: loadClassMetadata
/**
* {@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;
}
示例10: loadClassMetadata
/**
* {@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;
}
示例11: testLoadClassMetadata
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);
}
示例12: testLoadGroupSequenceProviderAnnotation
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);
}