Doctrine\ODM\PHPCR\Mapping\ClassMetadata::assertValidChildClass PHP Method

assertValidChildClass() public method

Assert that the given class FQN can be a child of the document this metadata represents.
public assertValidChildClass ( ClassMetadata $class )
$class ClassMetadata
    public function assertValidChildClass(ClassMetadata $class)
    {
        if ($this->isLeaf()) {
            throw new OutOfBoundsException(sprintf('Document "%s" has been mapped as a leaf. It cannot have children', $this->name));
        }
        $childClasses = $this->getChildClasses();
        if (0 === count($childClasses)) {
            return;
        }
        foreach ($childClasses as $childClass) {
            if ($class->name === $childClass || $class->reflClass->isSubclassOf($childClass)) {
                return;
            }
        }
        throw new OutOfBoundsException(sprintf('Document "%s" does not allow children of type "%s". Allowed child classes "%s"', $this->name, $class->name, implode('", "', $childClasses)));
    }

Usage Example

Example #1
0
 /**
  * It should throw an exception if the given class is not allowed.
  *
  * @expectedException \Doctrine\ODM\PHPCR\Exception\OutOfBoundsException
  * @expectedExceptionMessage does not allow children of type "stdClass"
  */
 public function testAssertValidChildClassesNotAllowed()
 {
     $cm = new ClassMetadata('Doctrine\\Tests\\ODM\\PHPCR\\Mapping\\Person');
     $cm->initializeReflection(new RuntimeReflectionService());
     $childCm = new ClassMetadata('stdClass');
     $childCm->initializeReflection(new RuntimeReflectionService());
     $cm->setChildClasses(array('Doctrine\\Tests\\ODM\\PHPCR\\Mapping\\Person'));
     $cm->assertValidChildClass($childCm);
 }
ClassMetadata