Entity, allow to duplicate objects

This commit is contained in:
fbsanchez 2021-11-15 19:29:37 +01:00
parent 9ff3c8eee1
commit cc859f3978
1 changed files with 26 additions and 0 deletions

View File

@ -100,6 +100,32 @@ abstract class Entity
}
/**
* Duplicates an object.
*
* @param array $field_exceptions Fields to skip.
* @param string $class_str Class name.
*
* @return object
*/
public function duplicate(
array $field_exceptions=[],
string $class_str=__CLASS__
) {
$keys = array_keys($this->toArray());
$new = new $class_str();
foreach ($keys as $k) {
if (in_array($k, $field_exceptions) === false) {
$new->$k($this->$k());
}
}
return $new;
}
/**
* Defines a generic constructor to extract information of the object.
*