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

Commit

Permalink
added getTranslationsArray() method (#347)
Browse files Browse the repository at this point in the history
* added getTranslationsArray() method

* fixed codestyle

* Revert "fixed codestyle"

This reverts commit 23ef04d.

* fixed codestyle
  • Loading branch information
askello authored and dimsav committed Jun 6, 2017
1 parent c3e12fb commit ff2a07c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit ff2a07c

Please sign in to comment.