|
1
|
|
|
<?php namespace Modules\Core\Doctrine; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\App; |
|
4
|
|
|
use Mitch\LaravelDoctrine\EntityManagerFacade; |
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
|
|
7
|
|
|
trait Translatable |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param array $input |
|
11
|
|
|
*/ |
|
12
|
|
|
public function fillTranslations(array $input) |
|
13
|
|
|
{ |
|
14
|
|
|
foreach ($input as $locale => $attributes) { |
|
15
|
|
|
foreach ($attributes as $key => $value) { |
|
16
|
|
|
$this->createOrUpdateTranslation($key, $locale, $value); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create or update the given field name |
|
23
|
|
|
* @param string $fieldName |
|
24
|
|
|
* @param string $locale |
|
25
|
|
|
* @param string $value |
|
26
|
|
|
*/ |
|
27
|
|
|
public function createOrUpdateTranslation($fieldName, $locale, $value) |
|
28
|
|
|
{ |
|
29
|
|
|
$found = false; |
|
30
|
|
|
foreach ($this->translation as $translation) { |
|
|
|
|
|
|
31
|
|
|
if ($translation->locale == $locale) { |
|
32
|
|
|
$translation->$fieldName = $value; |
|
33
|
|
|
$found = true; |
|
34
|
|
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (! $found) { |
|
39
|
|
|
$foreignKey = $this->getForeignKey(); |
|
40
|
|
|
|
|
41
|
|
|
$translationObjectClass = $this->getTranslationClass(); |
|
42
|
|
|
$translationObject = new $translationObjectClass(); |
|
43
|
|
|
$translationObject->locale = $locale; |
|
44
|
|
|
$translationObject->$fieldName = $value; |
|
45
|
|
|
$translationObject->$foreignKey = $this; |
|
46
|
|
|
$this->translation->add($translationObject); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the translation of the given field name |
|
52
|
|
|
* @param string $fieldName |
|
53
|
|
|
* @param string|null $locale |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function translation($fieldName, $locale = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$locale = $locale ?: App::getLocale(); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->translation as $translation) { |
|
61
|
|
|
if ($translation->locale == $locale) { |
|
62
|
|
|
return $translation->$fieldName; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $fieldName |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public function translatableGetter($fieldName) |
|
72
|
|
|
{ |
|
73
|
|
|
if (in_array($fieldName, $this->getTranslatedFieldNamesForEntity())) { |
|
74
|
|
|
$result = $this->translation($fieldName); |
|
75
|
|
|
} else { |
|
76
|
|
|
$result = $this->getRawField($fieldName); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $name |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getRawField($name) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->$name; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getTranslatedFieldNamesForEntity() |
|
95
|
|
|
{ |
|
96
|
|
|
$cacheArray = []; |
|
97
|
|
|
$translatedEntityName = $this->getTranslationClass(); |
|
98
|
|
|
if (! isset($cacheArray[$translatedEntityName])) { |
|
99
|
|
|
$cacheArray[$translatedEntityName] = array_values(array_diff(EntityManagerFacade::getClassMetadata($translatedEntityName)->getColumnNames(), |
|
100
|
|
|
['id', 'locale'])); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $cacheArray[$translatedEntityName]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get the foreign key for the current class |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
private function getForeignKey() |
|
111
|
|
|
{ |
|
112
|
|
|
$reflectionClass = new ReflectionClass(get_class($this)); |
|
113
|
|
|
$foreignKey = strtolower($reflectionClass->getShortName()); |
|
114
|
|
|
|
|
115
|
|
|
return $foreignKey; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the Translations class name |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private function getTranslationClass() |
|
123
|
|
|
{ |
|
124
|
|
|
return get_class($this) . 'Translation'; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: