Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

added getTranslationsArray() method #347

Merged
merged 5 commits into from
Jun 6, 2017
Merged
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,22 @@ public function toArray()
return $attributes;
}

/**
* @return array
*/
public function getTranslationsArray()
{
$translations = [];

foreach ($this->translations as $translation) {
foreach ($this->translatedAttributes as $attr) {
$translations[$translation->{$this->getLocaleKey()}][$attr] = $translation->{$attr};
}
}

return $translations;
}

/**
* @return string
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,26 @@ public function test_it_uses_the_default_locale_from_the_model()
$this->assertEquals($country->name, 'Tunisie');
}

public function test_retriving_translatable_array()
{
$country = new Country();
$country->fill([
'code' => 'tn',
'name:en' => 'Tunisia',
'name:fr' => 'Tunisie',
]);

$testArr = [];

foreach ($country->translations as $translation) {
foreach ($country->translatedAttributes as $attr) {
$testArr[$translation->locale][$attr] = $translation->{$attr};
}
}

$this->assertEquals($testArr, $country->getTranslationsArray());
}

public function test_fill_when_locale_key_unknown()
{
config(['translatable.locales' => ['en']]);
Expand Down