Closed rulatir closed 3 months ago
Probably something like this?
$extended = clone $original;
foreach ($extended as $translation) {
$extended->add(new ExtendedTranslation($translation));
}
If the old and new translations has the same id, it will be overrided, so you don't need to remove the first.
Another way is creating the ExtendedTranslations
class:
class ExtendedTranslations extends Translations {
public static function create(Translations $traslations) {
$new = new static();
$new->description = $translations->description;
$new->headers = $translations->headers;
$new->flags = $translations->flags;
$new->translations = array_map(function ($translation) {
return new ExtendedTranslation($translation))
}, $translations->getTranslations());
return $new;
}
}
$extended = ExtendedTranslations::create($original);
Use case: I receive a
Translations
object$original
containing vanillaTranslation
items, and I must return aTranslations
object$extended
with the following properties:Translation
items must be the same as in the$original
Translation
item must be replaced by an instance ofExtendedTranslation
holding the same information (as well as some extra information specific toExtendedTranslation
).This was easy while
->exchangeArray()
was available. Now that it is gone, it seems I must do this:$extended = clone $original;
getTranslations()
$extended
(O(n^2)?)Translation
items toExtendedTranslation
objects andadd()
them to$extended
Is there a simpler way with current API?
Translations
is a collection object, and I believe it should provide fully featured collection API:map()
,filter()
,removeAll()
etc.