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

Commit

Permalink
Merge pull request #257 from easteregg/master
Browse files Browse the repository at this point in the history
Add support for Model accessors to be called
  • Loading branch information
dimsav authored Aug 17, 2016
2 parents cffbd7c + 73e40f3 commit 3e73600
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ public function getAttribute($key)
return null;
}

// If the given $key has a mutator, we push it to $attributes and then call getAttributeValue
// on it. This way, we can use Eloquent's checking for Mutation, type casting, and
// Date fields.
if ($this->hasGetMutator($key)) {
$this->attributes[$key] = $this->getTranslation($locale)->$key;

return $this->getAttributeValue($key);
}

return $this->getTranslation($locale)->$key;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Dimsav\Translatable\Test\Model\CountryStrict;
use Dimsav\Translatable\Test\Model\CountryWithCustomLocaleKey;
use Dimsav\Translatable\Test\Model\Food;
use Dimsav\Translatable\Test\Model\Person;

class TranslatableTest extends TestsBase
{
Expand Down Expand Up @@ -426,4 +427,12 @@ public function test_to_array_and_fallback_with_country_based_locales_enabled()
$fritesArray = Food::find(1)->toArray();
$this->assertSame('frites', $fritesArray['name']);
}

public function test_it_should_mutate_the_translated_attribute_if_a_mutator_is_set_on_model()
{
$person = new Person(['name' => 'john doe']);
$person->save();
$person = Person::find(1);
$this->assertEquals('John doe', $person->name);
}
}
17 changes: 17 additions & 0 deletions tests/migrations/2013_11_28_152610_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public function up()
$table->unique(['vegetable_identity', 'locale']);
$table->foreign('vegetable_identity')->references('identity')->on('vegetables')->onDelete('cascade');
});

Schema::create('people', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});

Schema::create('person_translations', function (Blueprint $table) {
$table->increments('id');
$table->integer('person_id')->unsigned();
$table->string('name');
$table->string('locale')->index();

$table->unique(['person_id', 'locale']);
$table->foreign('person_id')->references('id')->on('people')->onDelete('cascade');
});
}

/**
Expand All @@ -114,5 +129,7 @@ public function down()
Schema::dropIfExists('foods');
Schema::dropIfExists('vegetable_translations');
Schema::dropIfExists('vegetables');
Schema::dropIfExists('person_translations');
Schema::dropIfExists('people');
}
}
65 changes: 65 additions & 0 deletions tests/models/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Dimsav\Translatable\Test\Model;

use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Person extends Eloquent
{
protected $table = 'people';

use Translatable;

/**
* Array with the fields translated in the Translation table.
*
* @var array
*/
public $translatedAttributes = ['name'];

/**
* Set $translationModel if you want to overwrite the convention
* for the name of the translation Model. Use full namespace if applied.
*
* The convention is to add "Translation" to the name of the class extending Translatable.
* Example: Country => CountryTranslation
*/
public $translationModel;

/**
* This is the foreign key used to define the translation relationship.
* Set this if you want to overwrite the laravel default for foreign keys.
*
* @var
*/
public $translationForeignKey;

/**
* Add your translated attributes here if you want
* fill them with mass assignment.
*
* @var array
*/
public $fillable = ['name'];

/**
* The database field being used to define the locale parameter in the translation model.
* Defaults to 'locale'.
*
* @var string
*/
public $localeKey;

/**
* Mutate name attribute into upper-case.
*
* @param $value
*
* @return string
*/
public function getNameAttribute($value)
{
return ucfirst($value);
}
}
10 changes: 10 additions & 0 deletions tests/models/PersonTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Dimsav\Translatable\Test\Model;

use Illuminate\Database\Eloquent\Model as Eloquent;

class PersonTranslation extends Eloquent
{
public $timestamps = false;
}

0 comments on commit 3e73600

Please sign in to comment.