Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CollectionTransformer performance #7

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions src/Translator/Transformers/CollectionTransformer.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php
namespace Tectonic\LaravelLocalisation\Translator\Transformers;

use Tectonic\LaravelLocalisation\Translator\Translated\Collection as TranslatedCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Tectonic\LaravelLocalisation\Database\Translation;
use Tectonic\Localisation\Contracts\Transformer;
use Tectonic\Localisation\Translator\Transformers\Transformer as BaseTransformer;

class CollectionTransformer extends BaseTransformer implements Transformer
{
/**
* This transformer is only appropriate for eloquent collection objects.
* This transformer is only appropriate for collection objects.
*
* @param $object
* @return mixed
Expand Down Expand Up @@ -50,7 +51,7 @@ public function shallow($collection, $language = null, $fields = null)
*
* @param $collection
* @param boolean $shallow
* @return TranslatedCollection
* @return Collection
*/
private function translate($collection, $shallow)
{
Expand Down Expand Up @@ -85,14 +86,64 @@ public function getTranslationResources(Collection $collection, $shallow)
* @param Collection $collection
* @param Collection $translations
* @param bool $shallow
* @return TranslatedCollection
*
* @return Collection
*/
public function applyTranslations(Collection $collection, Collection $translations, $shallow)
public function applyTranslations(Collection $collection, Collection $translations, bool $shallow)
{
$translations = $translations->groupBy(fn ($translation) => $this->groupByKey($translation));

foreach ($collection as $model) {
app(ModelTransformer::class)->applyTranslations($model, $translations, $shallow);
if (($modelTranslations = $this->modelTranslations($translations, $model, $shallow))->isNotEmpty()) {
app(ModelTransformer::class)->applyTranslations($model, $modelTranslations, $shallow);
}
}

return $collection;
}

protected function modelTranslations(Collection $translations, Model $model, bool $shallow = false)
{
$relationTranslations = ($translations->get($this->groupByKey($model), collect()))->toBase();

if (!$shallow && $relations = array_filter($model->getRelations())) {
$relationTranslations = $relationTranslations->merge(
$this->parseRelationsTranslations($relations, $translations)
);
}

return new \Illuminate\Database\Eloquent\Collection(
$relationTranslations
->filter()
->flatten()
);
}

protected function parseRelationsTranslations(array $relations, Collection $translations): Collection
{
$relationTranslations = collect();
foreach ($relations as $relation) {
if ($relation instanceof Collection) {
foreach ($relation as $item) {
$relationTranslations = $relationTranslations->merge(
$this->modelTranslations($translations, $item)
->groupBy(fn ($item) => $this->groupByKey($item))
);
}
} else {
$relationTranslations->push($translations->get($this->groupByKey($relation)));
}
}

return $relationTranslations;
}

protected function groupByKey($item)
{
if ($item instanceof Translation || $item instanceof \stdClass) {
return $item->foreign_id.$item->resource;
}

return $item->id.class_basename($item);
}
}