Skip to content

Commit

Permalink
Update composer, style
Browse files Browse the repository at this point in the history
  • Loading branch information
mihkelallorg committed Mar 6, 2019
1 parent 22c18f6 commit ec5fdb0
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 32 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"google/cloud-translate": "^1.2",
"php": ">=7.1.3",
"illuminate/support": "5.6.*|5.7.*"
"illuminate/support": "5.6.*|5.7.*|5.8.*"
},
"require-dev": {
"matthewbdaly/artisan-standalone": "0.0.*",
Expand All @@ -25,9 +25,9 @@
}
},
"autoload-dev": {
"classmap": [
"tests"
]
"psr-4": {
"Mihkullorg\\Tests\\Translatable\\": "tests/"
}
},
"extra": {
"laravel": {
Expand Down
1 change: 1 addition & 0 deletions src/Models/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function scopeLanguage($query, $language)
public function scopeFresherThan($query, $date)
{
$date = $date instanceof Carbon ? $date->toDateTimeString() : $date;

return $query->where('updated_at', '>=', $date);
}

Expand Down
18 changes: 11 additions & 7 deletions src/Traits/Translatable.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

namespace Mihkullorg\Translatable;
namespace Mihkullorg\Translatable\Traits;

use Illuminate\Database\Eloquent\Relations\MorphMany;
use Mihkullorg\Translatable\Facades\Translator;
use Mihkullorg\Translatable\Models\Translation;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait Translatable
{
public function translate($field, $language, $translation = null)
{
if (!$translation) {
if (! $translation) {
$translation = Translator::translate($this->$field, $language);
}

Expand Down Expand Up @@ -45,7 +45,7 @@ public function translations()
/**
* @param string $field Translatable field name
* @param string $language Language code
* @return integer
* @return int
*/
public function deleteTranslation($field, $language)
{
Expand Down Expand Up @@ -79,14 +79,18 @@ public function createTranslation($field, $language, $translation)
* 'language' => 'ru',
* 'body' => 'Russian body translations',
* 'title' => '...',
* ]
* ].
*
* @return array[]
*/
public function getTranslationsAsArray()
{
return $this->translations->groupBy('language')->map(function($language) {
return $language->groupBy('field')->map(function($field) { return $field->first()->value; });
})->each(function($values, $language) { $values->put('language', $language); })->values()->toArray();
return $language->groupBy('field')->map(function($field) {
return $field->first()->value;
});
})->each(function($values, $language) {
$values->put('language', $language);
})->values()->toArray();
}
}
8 changes: 4 additions & 4 deletions src/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TranslatableServiceProvider extends BaseServiceProvider
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
$this->publishConfig();
$this->app->singleton('mt-translator', function ($app) {
return new TranslatorManager($app);
Expand All @@ -32,13 +32,13 @@ public function register()

private function publishConfig()
{
$config = __DIR__ . '/../config/config.php';
$config = __DIR__.'/../config/config.php';
$migrationFileName = '2019_01_10_000001_create_translatable_tables.php';
$migration = __DIR__ . '/database/migrations/' . $migrationFileName;
$migration = __DIR__.'/database/migrations/'.$migrationFileName;

$this->publishes([
$config => config_path('translatable.php'),
$migration => app_path('database/migrations/' . $migrationFileName),
$migration => app_path('database/migrations/'.$migrationFileName),
], 'translatable');
}
}
3 changes: 1 addition & 2 deletions src/TranslatorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Mihkullorg\Translatable;

use Illuminate\Support\Manager;
use Mihkullorg\Translatable\Contracts\TranslatorInterface;
use Mihkullorg\Translatable\Drivers\GoogleTranslator;
use Mihkullorg\Translatable\Contracts\TranslatorInterface;

class TranslatorManager extends Manager implements TranslatorInterface
{

/**
* Get the default driver name.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests;
namespace Mihkullorg\Tests\Translatable;

use Hash;

Expand All @@ -20,7 +20,7 @@ protected function getEnvironmentSetup($app)
{
}

public function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down
4 changes: 2 additions & 2 deletions tests/Models/TestModel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Mihkullorg\Translatable\Tests\Models;
namespace Mihkullorg\Tests\Translatable\Models;

use Illuminate\Database\Eloquent\Model;
use Mihkullorg\Translatable\Translatable;
use Mihkullorg\Translatable\Traits\Translatable;

class TestModel extends Model
{
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests;
namespace Mihkullorg\Tests\Translatable;

use Orchestra\Testbench\TestCase as BaseTestCase;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
Expand Down
16 changes: 7 additions & 9 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php

namespace Mihkullorg\Tests\Translatable;

use Carbon\Carbon;
use Mihkullorg\Tests\Translatable\Models\TestModel;
use Mihkullorg\Translatable\Facades\Translator;
use Mihkullorg\Translatable\Tests\Models\TestModel;
use Tests\TestCase;

class TranslatableTest extends TestCase
{
public function setUp()
{
parent::setUp();
}

public function test_translating_model_field_manually()
{
/** @var TestModel $model */
Expand Down Expand Up @@ -41,8 +37,8 @@ public function test_translating_model_field_with_translator()
}

/**
* Old translation should be deleted
* New translation should be created with Translator
* Old translation should be deleted.
* New translation should be created with Translator.
*/
public function test_translating_an_updated_model()
{
Expand Down Expand Up @@ -105,6 +101,7 @@ private function assertTranslationExists(TestModel $model, $field, $language, $v
'value' => $value,
]);
}

/**
* @param TestModel $model
* @param $field
Expand All @@ -127,6 +124,7 @@ private function createTranslationInThePast(TestModel $model, $field, $language,
$translation = $model->translations()->make(compact('field', 'language', 'value'));
$translation->updated_at = Carbon::now()->subDay();
$translation->save(['timestamps' => false]);

return $translation;
}
}
2 changes: 1 addition & 1 deletion tests/factories/TestModelFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Faker\Generator as Faker;
use Mihkullorg\Translatable\Tests\Models\TestModel;
use Mihkullorg\Tests\Translatable\Models\TestModel;

/*
|--------------------------------------------------------------------------
Expand Down

0 comments on commit ec5fdb0

Please sign in to comment.