private function populateAttribute($attributeName, AbstractAttribute $entityAttribute, $validation, $data, $fromDb)
{
// Skip population of protected attributes if data is not coming from DB
if (!$fromDb && $entityAttribute->getSkipOnPopulate()) {
return;
}
// Dynamic attributes from database should be populated without any checks, and skipped otherwise
if ($this->isInstanceOf($entityAttribute, AttributeType::DYNAMIC) && isset($data[$attributeName])) {
$entityAttribute->setValue($data[$attributeName], $fromDb);
return;
}
/**
* Check if attribute is required and it's value is set or maybe value was already assigned
*/
if (!$fromDb && $entityAttribute->isRequired() && !isset($data[$attributeName]) && !$entityAttribute->hasValue()) {
$message = $entityAttribute->getValidationMessages('required');
if (!$message) {
$message = ValidationException::REQUIRED;
}
$ex = new ValidationException(ValidationException::VALIDATION_FAILED);
$ex->addError($attributeName, $message, []);
$validation[$attributeName] = $ex;
return;
}
/**
* In case it is an update - if the attribute is not in new $data, it's no big deal, we already have the previous value.
*/
$dataIsSet = array_key_exists($attributeName, $data);
if (!$dataIsSet && $this->exists()) {
return;
}
$canPopulate = !$this->exists() || $fromDb || !$entityAttribute->getOnce();
if ($dataIsSet && $canPopulate) {
$dataValue = $data[$attributeName];
try {
$entityAttribute->setValue($dataValue, $fromDb);
} catch (ValidationException $e) {
$validation[$attributeName] = $e;
}
}
}