getLabelProperty example

XML;

        $dom = new \DOMDocument();
        $dom->loadXML($xml);

        \assert($dom->documentElement instanceof \DOMElement);

        $entity = Entity::fromXml($dom->documentElement);

        static::assertSame('my_entity', $entity->getName());
        static::assertTrue($entity->isCustomFieldsAware());
        static::assertSame('name', $entity->getLabelProperty());

        $fields = $entity->getFields();
        static::assertCount(2, $fields);

        static::assertInstanceOf(StringField::class$fields[0]);
        static::assertEquals('id', $fields[0]->getName());
        static::assertInstanceOf(StringField::class$fields[1]);
        static::assertEquals('name', $fields[1]->getName());
        static::assertTrue($fields[1]->isTranslatable());
    }

    
#[Package('core')] class CustomEntityXmlSchemaValidator
{
    public function validate(CustomEntityXmlSchema $schema): void
    {
        if ($schema->getEntities() === null) {
            throw new \RuntimeException('No entities found in parsed xml file');
        }

        foreach ($schema->getEntities()->getEntities() as $entity) {
            if ($entity->isCustomFieldsAware()) {
                $label = $entity->getLabelProperty();

                if ($label === null) {
                    throw CustomEntityException::noLabelProperty();
                }

                if (!$entity->hasField($label)) {
                    throw CustomEntityException::labelPropertyNotDefined($label);
                }

                if (!$entity->getField($label) instanceof StringField) {
                    throw CustomEntityException::labelPropertyWrongType($label);
                }
static::assertEquals($fields$entity->getFields());

        $flags = [];

        $entity->setFlags($flags);
        static::assertEquals($flags$entity->getFlags());

        $entity->setCustomFieldsAware(true);
        static::assertTrue($entity->getCustomFieldsAware());

        $entity->setLabelProperty('name');
        static::assertEquals('name', $entity->getLabelProperty());
    }
}
Home | Imprint | This part of the site doesn't use cookies.