/** * Create a xml element with the definition of a model field * @param zibo\library\xml\dom\Document $dom * @param zibo\library\orm\definition\field\ModelField $field * @return DOMElement an xml element which defines the model field */ protected function getElementFromField(Document $dom, ModelField $field) { $element = $dom->createElement(self::TAG_FIELD); $element->setAttribute(self::ATTRIBUTE_NAME, $field->getName()); if ($field instanceof RelationField) { $element->setAttribute(self::ATTRIBUTE_MODEL, $field->getRelationModelName()); if ($field instanceof BelongsToField) { $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_BELONGS_TO); } elseif ($field instanceof HasOneField) { $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_HAS_ONE); } elseif ($field instanceof HasManyField) { $element->setAttribute(self::ATTRIBUTE_RELATION, self::RELATION_HAS_MANY); $linkModel = $field->getLinkModelName(); if ($linkModel != null) { $element->setAttribute(self::ATTRIBUTE_LINK_MODEL, $linkModel); } $indexOn = $field->getIndexOn(); if ($indexOn) { $element->setAttribute(self::ATTRIBUTE_INDEX_ON, $indexOn); } } if ($field->isDependant()) { $element->setAttribute(self::ATTRIBUTE_DEPENDANT, 'true'); } $foreignKey = $field->getForeignKeyName(); if ($foreignKey) { $element->setAttribute(self::ATTRIBUTE_FOREIGN_KEY, $foreignKey); } } else { $element->setAttribute(self::ATTRIBUTE_TYPE, $field->getType()); $default = $field->getDefaultValue(); if ($default != null) { $element->setAttribute(self::ATTRIBUTE_DEFAULT, $default); } if ($field->IsUnique()) { $element->setAttribute(self::ATTRIBUTE_UNIQUE, 'true'); } } if ($field->isLocalized()) { $element->setAttribute(self::ATTRIBUTE_LOCALIZED, 'true'); } $label = $field->getLabel(); if ($label) { $element->setAttribute(self::ATTRIBUTE_LABEL, $label); } $validators = $field->getValidators(); foreach ($validators as $validator) { $validatorElement = $dom->createElement(self::TAG_VALIDATION); $validatorElement->setAttribute(self::ATTRIBUTE_NAME, $validator->getName()); $options = $validator->getOptions(); foreach ($options as $key => $value) { $parameterElement = $dom->createElement(self::TAG_PARAMETER); $parameterElement->setAttribute(self::ATTRIBUTE_NAME, $key); $parameterElement->setAttribute(self::ATTRIBUTE_VALUE, $value); $validatorElement->appendChild($parameterElement); } $element->appendChild($validatorElement); } return $element; }
/** * Creates a form field for the provided model property field * @param zibo\library\html\form\field\FieldFactory $fieldFactory Instance of the field factory * @param zibo\library\orm\definition\field\ModelField $field Definition of the model field * @param mixed $value Value for the form field * @return zibo\library\html\form\field\Field */ protected function createFormFieldFromModelField(FieldFactory $fieldFactory, ModelField $field, $value) { $fieldName = $field->getName(); try { return $fieldFactory->createField($field->getType(), $fieldName, $value); } catch (ZiboException $e) { return $fieldFactory->createField(FieldFactory::TYPE_STRING, $fieldName, $value); } }
/** * Checks if all the necessairy information from the module field is in the builder field * @param string $name Name of the field * @param zibo\library\orm\model\field\ModelField $moduleField * @param zibo\library\orm\model\field\ModelField $builderField * @return null * @throws zibo\library\orm\exception\ModelIncompleteException when the model misses information defined in the module model */ private function checkModuleField($fieldName, ModelField $moduleField, ModelField $builderField) { if ($moduleField->isLocalized() != $builderField->isLocalized()) { throw new ModelIncompleteException('Field ' . $fieldName . ' has not the same localized flag as defined in the module model'); } $moduleValidators = $moduleField->getValidators(); $builderValidators = $moduleField->getValidators(); foreach ($moduleValidators as $moduleValidator) { $validatorFound = false; foreach ($builderValidators as $builderValidator) { if ($moduleValidator->equals($builderValidator)) { $validatorFound = true; break; } } if (!$validatorFound) { throw new ModelIncompleteException('Field ' . $fieldName . ' does not contain the validator ' . $moduleValidator->getName() . ' as defined in the module model'); } } if ($moduleField instanceof PropertyField) { if (!$builderField instanceof PropertyField) { throw new ModelIncompleteException('Field ' . $fieldName . ' should be a property as defined in the module model'); } if ($moduleField->getType() != $builderField->getType()) { throw new ModelIncompleteException('Property ' . $fieldName . ' should be of the type ' . $moduleField->getType() . ' as defined in the module model'); } return; } $relationModelName = $moduleField->getRelationModelName(); if ($relationModelName != $builderField->getRelationModelName()) { throw new ModelIncompleteException('Field ' . $fieldName . ' should have model ' . $relationModelName . ' as relation model as defined in the module model'); } $moduleLinkModelName = $moduleField->getLinkModelName(); $builderLinkModelName = $builderField->getLinkModelName(); if ($moduleLinkModelName && (!$builderLinkModelName || $moduleLinkModelName != $builderLinkModelName)) { throw new ModelIncompleteException('Field ' . $fieldName . ' should have model ' . $moduleLinkModelName . ' as link model as defined in the module model'); } if ($moduleField instanceof BelongsToField) { if (!$builderField instanceof BelongsToField) { throw new ModelIncompleteException('Field ' . $fieldName . ' should be a belongsTo relation as defined in the module model'); } } if ($moduleField instanceof HasOneField) { if (!$builderField instanceof HasOneField) { throw new ModelIncompleteException('Field ' . $fieldName . ' should be a hasOne relation as defined in the module model'); } } if ($moduleField instanceof HasManyField) { if (!$builderField instanceof HasManyField) { throw new ModelIncompleteException('Field ' . $fieldName . ' should be a hasMany relation as defined in the module model'); } } }