1 | <?php |
||
7 | class ImportSingleTranslation |
||
8 | { |
||
9 | private $overwriteProtection = true; |
||
10 | private $dry = false; |
||
11 | |||
12 | /** |
||
13 | * key: actiontype, value: item array |
||
14 | * e.g. ['inserts' => ['key' => 'foo.bar', 'new_value' => 'baz']] |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | private $stats = []; |
||
19 | |||
20 | /** |
||
21 | * @param $locale |
||
22 | * @param $key |
||
23 | * @param $value |
||
24 | * @return $this |
||
25 | */ |
||
26 | 9 | public function import($locale, $key, $value) |
|
34 | |||
35 | /** |
||
36 | * @param $locale |
||
37 | * @param $key |
||
38 | * @param $value |
||
39 | * @return $this |
||
40 | */ |
||
41 | 9 | private function insertTranslation($locale, $key, $value) |
|
52 | |||
53 | /** |
||
54 | * @param Line $line |
||
55 | * @param $locale |
||
56 | * @param $key |
||
57 | * @param $value |
||
58 | * @return $this |
||
59 | */ |
||
60 | 6 | private function updateTranslation(Line $line, $locale, $key, $value) |
|
61 | { |
||
62 | 6 | $translation = $line->getValue($locale, false); |
|
63 | |||
64 | // Ignore the translation if it has remained the same |
||
65 | 6 | if ($translation === $value) { |
|
66 | 6 | $this->setStats('remained_same', $locale, $key, $value, $translation); |
|
67 | 6 | return $this; |
|
68 | } |
||
69 | |||
70 | 6 | if (!is_null($translation) && $this->overwriteProtection) { |
|
71 | 3 | $this->setStats('updates_on_hold', $locale, $key, $value, $translation); |
|
72 | 3 | return $this; |
|
73 | } |
||
74 | |||
75 | 3 | if ($translation) { |
|
76 | 3 | if (!$this->dry) { |
|
77 | 3 | $line->saveValue($locale, $value); |
|
78 | } |
||
79 | 3 | $this->setStats('updates', $locale, $key, $value, $translation); |
|
80 | |||
81 | 3 | return $this; |
|
82 | } |
||
83 | |||
84 | return $this->insertTranslation($locale, $key, $value); |
||
85 | } |
||
86 | |||
87 | 9 | public function getStats() |
|
91 | |||
92 | /** |
||
93 | * @param $action |
||
94 | * @param $locale |
||
95 | * @param $key |
||
96 | * @param $new_value |
||
97 | * @param null $original_value |
||
98 | */ |
||
99 | 9 | private function setStats($action, $locale, $key, $new_value, $original_value = null) |
|
103 | |||
104 | /** |
||
105 | * @param bool $enable |
||
106 | * @return $this |
||
107 | */ |
||
108 | 9 | public function enableOverwriteProtection($enable = true) |
|
113 | |||
114 | /** |
||
115 | * @return $this |
||
116 | */ |
||
117 | public function disableOverwriteProtection() |
||
121 | |||
122 | /** |
||
123 | * @param bool $enable |
||
124 | * @return $this |
||
125 | */ |
||
126 | 9 | public function dry($enable = true) |
|
131 | } |
||
132 |