Skip to content

Commit bcfdd05

Browse files
authored
Merge pull request #12 from Astrotomic/ft-refatoring
refatoring
2 parents f143ecf + 8752e7e commit bcfdd05

19 files changed

+534
-412
lines changed

.codeclimate.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@ plugins:
4040
enabled: false
4141
CyclomaticComplexity:
4242
enabled: false
43+
CleanCode/ElseExpression:
44+
enabled: false
45+
CleanCode/BooleanArgumentFlag:
46+
enabled: false
4347
phan:
4448
enabled: false

.php_cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in('src/Translatable');
5+
6+
return PhpCsFixer\Config::create()
7+
->setRules([
8+
'ordered_class_elements' => [
9+
'order' => [
10+
'use_trait',
11+
'constant_public',
12+
'constant_protected',
13+
'constant_private',
14+
'property_public',
15+
'property_protected',
16+
'property_private',
17+
'construct',
18+
'method_public_static',
19+
'method_public',
20+
'method_protected_static',
21+
'method_protected',
22+
'method_private_static',
23+
'method_private',
24+
'destruct',
25+
'magic',
26+
],
27+
'sortAlgorithm' => 'alpha',
28+
],
29+
'yoda_style' => [
30+
'equal' => false,
31+
'identical' => false,
32+
'less_and_greater' => null,
33+
],
34+
])
35+
->setFinder($finder);

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@
4848
},
4949
"config": {
5050
"sort-packages": true
51+
},
52+
"scripts": {
53+
"csfix": "php-cs-fixer fix --using-cache=no"
5154
}
5255
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Astrotomic\Translatable\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
7+
8+
interface Translatable
9+
{
10+
public static function defaultAutoloadTranslations(): void;
11+
12+
public static function disableAutoloadTranslations(): void;
13+
14+
public static function enableAutoloadTranslations(): void;
15+
16+
public function translations(): HasMany;
17+
18+
public function deleteTranslations($locales = null): void;
19+
20+
public function getDefaultLocale(): ?string;
21+
22+
public function getNewTranslation(string $locale): Model;
23+
24+
public function getTranslation(?string $locale = null, bool $withFallback = null): ?Model;
25+
26+
public function getTranslationOrNew(?string $locale = null): Model;
27+
28+
public function getTranslationsArray(): array;
29+
30+
public function hasTranslation(?string $locale = null): bool;
31+
32+
public function isTranslationAttribute(string $key): bool;
33+
34+
public function replicateWithTranslations(array $except = null): Model;
35+
36+
public function setDefaultLocale(?string $locale);
37+
38+
public function translate(?string $locale = null, bool $withFallback = false): ?Model;
39+
40+
public function translateOrDefault(?string $locale = null): ?Model;
41+
42+
public function translateOrNew(?string $locale = null): Model;
43+
}

src/Translatable/Locales.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Locales implements Arrayable, ArrayAccess
1616
protected $config;
1717

1818
/**
19-
* @var TranslatorContract
19+
* @var array
2020
*/
21-
protected $translator;
21+
protected $locales = [];
2222

2323
/**
24-
* @var array
24+
* @var TranslatorContract
2525
*/
26-
protected $locales = [];
26+
protected $translator;
2727

2828
public function __construct(ConfigContract $config, TranslatorContract $translator)
2929
{
@@ -33,26 +33,9 @@ public function __construct(ConfigContract $config, TranslatorContract $translat
3333
$this->load();
3434
}
3535

36-
public function load(): void
36+
public function add(string $locale): void
3737
{
38-
$localesConfig = (array) $this->config->get('translatable.locales', []);
39-
40-
if (empty($localesConfig)) {
41-
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish astrotomic/laravel-translatable" and that the locales configuration is defined.');
42-
}
43-
44-
$this->locales = [];
45-
foreach ($localesConfig as $key => $locale) {
46-
if (is_string($key) && is_array($locale)) {
47-
$this->locales[$key] = $key;
48-
foreach ($locale as $country) {
49-
$countryLocale = $this->getCountryLocale($key, $country);
50-
$this->locales[$countryLocale] = $countryLocale;
51-
}
52-
} elseif (is_string($locale)) {
53-
$this->locales[$locale] = $locale;
54-
}
55-
}
38+
$this->locales[$locale] = $locale;
5639
}
5740

5841
public function all(): array
@@ -65,49 +48,61 @@ public function current()
6548
return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
6649
}
6750

68-
public function has(string $locale): bool
51+
public function forget(string $locale): void
6952
{
70-
return isset($this->locales[$locale]);
53+
unset($this->locales[$locale]);
7154
}
7255

7356
public function get(string $locale): ?string
7457
{
7558
return $this->locales[$locale] ?? null;
7659
}
7760

78-
public function add(string $locale): void
61+
public function getCountryLocale(string $locale, string $country): string
7962
{
80-
$this->locales[$locale] = $locale;
63+
return $locale.$this->getLocaleSeparator().$country;
8164
}
8265

83-
public function forget(string $locale): void
66+
public function getLanguageFromCountryBasedLocale(string $locale): string
8467
{
85-
unset($this->locales[$locale]);
68+
return explode($this->getLocaleSeparator(), $locale)[0];
8669
}
8770

8871
public function getLocaleSeparator(): string
8972
{
9073
return $this->config->get('translatable.locale_separator') ?: '-';
9174
}
9275

93-
public function getCountryLocale(string $locale, string $country): string
76+
public function has(string $locale): bool
9477
{
95-
return $locale.$this->getLocaleSeparator().$country;
78+
return isset($this->locales[$locale]);
9679
}
9780

9881
public function isLocaleCountryBased(string $locale): bool
9982
{
10083
return strpos($locale, $this->getLocaleSeparator()) !== false;
10184
}
10285

103-
public function getLanguageFromCountryBasedLocale(string $locale): string
86+
public function load(): void
10487
{
105-
return explode($this->getLocaleSeparator(), $locale)[0];
106-
}
88+
$localesConfig = (array) $this->config->get('translatable.locales', []);
10789

108-
public function toArray(): array
109-
{
110-
return $this->all();
90+
if (empty($localesConfig)) {
91+
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish astrotomic/laravel-translatable" and that the locales configuration is defined.');
92+
}
93+
94+
$this->locales = [];
95+
foreach ($localesConfig as $key => $locale) {
96+
if (is_string($key) && is_array($locale)) {
97+
$this->locales[$key] = $key;
98+
foreach ($locale as $country) {
99+
$countryLocale = $this->getCountryLocale($key, $country);
100+
$this->locales[$countryLocale] = $countryLocale;
101+
}
102+
} elseif (is_string($locale)) {
103+
$this->locales[$locale] = $locale;
104+
}
105+
}
111106
}
112107

113108
public function offsetExists($key): bool
@@ -133,4 +128,9 @@ public function offsetUnset($key)
133128
{
134129
$this->forget($key);
135130
}
131+
132+
public function toArray(): array
133+
{
134+
return $this->all();
135+
}
136136
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Astrotomic\Translatable\Traits;
4+
5+
use Illuminate\Database\Eloquent\Relations\HasMany;
6+
7+
/**
8+
* @property-read string $translationModel
9+
* @property-read string $translationForeignKey
10+
*/
11+
trait Relationship
12+
{
13+
/**
14+
* @deprecated
15+
*/
16+
public function getRelationKey(): string
17+
{
18+
return $this->getTranslationRelationKey();
19+
}
20+
21+
/**
22+
* @internal will change to protected
23+
*/
24+
public function getTranslationModelName(): string
25+
{
26+
return $this->translationModel ?: $this->getTranslationModelNameDefault();
27+
}
28+
29+
/**
30+
* @internal will change to private
31+
*/
32+
public function getTranslationModelNameDefault(): string
33+
{
34+
$modelName = get_class($this);
35+
36+
if ($namespace = $this->getTranslationModelNamespace()) {
37+
$modelName = $namespace.'\\'.class_basename(get_class($this));
38+
}
39+
40+
return $modelName.config('translatable.translation_suffix', 'Translation');
41+
}
42+
43+
/**
44+
* @internal will change to private
45+
*/
46+
public function getTranslationModelNamespace(): ?string
47+
{
48+
return config('translatable.translation_model_namespace');
49+
}
50+
51+
/**
52+
* @internal will change to protected
53+
*/
54+
public function getTranslationRelationKey(): string
55+
{
56+
if ($this->translationForeignKey) {
57+
return $this->translationForeignKey;
58+
}
59+
60+
return $this->getForeignKey();
61+
}
62+
63+
public function translations(): HasMany
64+
{
65+
return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey());
66+
}
67+
}

0 commit comments

Comments
 (0)