public function copyAlterableProperties($object, AbstractEntity $entity)
{
$rules = $this->getRules();
if (!isset($rules[static::RULE_TYPE_ALTERABLE])) {
//Nothing to copy
throw new \Exception(sprintf("ApiEntityAdapter::RULE_TYPE_ALTERABLE offset of rules has not been defined for the %s class.", get_class($this)));
}
$it = $entity->getIterator();
$notAlterable = array_diff(array_keys(get_object_vars($object)), $rules[static::RULE_TYPE_ALTERABLE]);
if (!empty($notAlterable)) {
if (count($notAlterable) > 1) {
$message = "You are trying to set properties %s that either are not alterable or do not exist";
} else {
$message = "You are trying to set the property %s which either is not alterable or does not exist";
}
throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_STRUCTURE, sprintf($message, implode(', ', $notAlterable)));
}
$settingsRules = null;
$collection = null;
foreach ($rules[static::RULE_TYPE_ALTERABLE] as $key) {
if (!property_exists($object, $key)) {
continue;
}
//As the name of the property that goes into response may be different from the
//real property name in the Entity object it should be mapped at first
if (!empty($rules[static::RULE_TYPE_TO_DATA])) {
//if toData rule is null it means all properties are allowed
if (($property = array_search($key, $rules[static::RULE_TYPE_TO_DATA])) !== false) {
if (is_string($property)) {
//In this case the real name of the property is the key of the array
if ($property[0] === '_' && method_exists($this, $property)) {
//It is callable
$this->{$property}($object, $entity, self::ACT_CONVERT_TO_ENTITY);
continue;
}
} else {
$property = $key;
}
}
}
//As the name of the property that goes into response may be different from the
//real setting name in the Entity object it should be mapped at first
if (empty($property) && !empty($rules[static::RULE_TYPE_SETTINGS])) {
if (!isset($settingsRules)) {
$settingsRules = $this->getSettingsRules();
}
if (($property = array_search($key, $settingsRules)) !== false) {
if (!isset($collection)) {
$collection = $this->getSettingsCollection($entity);
}
if (!is_string($property)) {
$property = $key;
}
$collection[$property] = $object->{$key};
continue;
}
}
$property = isset($property) ? $property : $key;
$entity->{$property} = $object->{$key} === null ? null : static::convertInputValue($it->getField($property)->column->type, $object->{$key}, $key);
}
}