本文整理汇总了PHP中Symfony\Component\Validator\Mapping\ClassMetadata::mergeConstraints方法的典型用法代码示例。如果您正苦于以下问题:PHP ClassMetadata::mergeConstraints方法的具体用法?PHP ClassMetadata::mergeConstraints怎么用?PHP ClassMetadata::mergeConstraints使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\Mapping\ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata::mergeConstraints方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: testLoadClassMetadataAndMerge
/**
* Test MetaData merge with parent annotation.
*/
public function testLoadClassMetadataAndMerge()
{
$loader = new AnnotationLoader();
// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent');
$loader->loadClassMetadata($parent_metadata);
$metadata = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
// Merge parent metaData.
$metadata->mergeConstraints($parent_metadata);
$loader->loadClassMetadata($metadata);
$expected_parent = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\EntityParent');
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();
$expected = new ClassMetadata('Symfony\\Tests\\Component\\Validator\\Fixtures\\Entity');
$expected->mergeConstraints($expected_parent);
$expected->setGroupSequence(array('Foo', '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);
}