|
1
|
|
|
<?php namespace Modules\Core\Internationalisation; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
|
4
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
5
|
|
|
|
|
6
|
|
|
abstract class BaseFormRequest extends FormRequest |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Set the translation key prefix for attributes. |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $translationsAttributesKey = 'validation.attributes.'; |
|
13
|
|
|
/** |
|
14
|
|
|
* Current processed locale |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $localeKey; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Return an array of rules for translatable fields |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public function translationRules() |
|
24
|
|
|
{ |
|
25
|
|
|
return []; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Return an array of messages for translatable fields |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public function translationMessages() |
|
33
|
|
|
{ |
|
34
|
|
|
return []; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get the validator instance for the request. |
|
39
|
|
|
* @return \Illuminate\Validation\Validator |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getValidatorInstance() |
|
42
|
|
|
{ |
|
43
|
|
|
$factory = $this->container->make('Illuminate\Validation\Factory'); |
|
44
|
|
|
if (method_exists($this, 'validator')) { |
|
45
|
|
|
return $this->container->call([$this, 'validator'], compact('factory')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$rules = $this->container->call([$this, 'rules']); |
|
49
|
|
|
$attributes = $this->attributes(); |
|
50
|
|
|
$messages = []; |
|
51
|
|
|
|
|
52
|
|
|
$translationsAttributesKey = $this->getTranslationsAttributesKey(); |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->requiredLocales() as $localeKey => $locale) { |
|
55
|
|
|
$this->localeKey = $localeKey; |
|
|
|
|
|
|
56
|
|
|
foreach ($this->container->call([$this, 'translationRules']) as $attribute => $rule) { |
|
57
|
|
|
$key = $localeKey . '.' . $attribute; |
|
58
|
|
|
$rules[$key] = $rule; |
|
59
|
|
|
$attributes[$key] = trans($translationsAttributesKey . $attribute); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->container->call([$this, 'translationMessages']) as $attributeAndRule => $message) { |
|
63
|
|
|
$messages[$localeKey . '.' . $attributeAndRule] = $message; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $factory->make( |
|
68
|
|
|
$this->all(), $rules, array_merge($this->messages(), $messages), $attributes |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function withTranslations() |
|
76
|
|
|
{ |
|
77
|
|
|
$results = $this->all(); |
|
78
|
|
|
$translations = []; |
|
79
|
|
|
foreach ($this->requiredLocales() as $key => $locale) { |
|
80
|
|
|
$locales[] = $key; |
|
|
|
|
|
|
81
|
|
|
$translations[$key] = $this->get($key); |
|
82
|
|
|
} |
|
83
|
|
|
$results['translations'] = $translations; |
|
84
|
|
|
array_forget($results, $locales); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return $results; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return \Illuminate\Support\Collection |
|
91
|
|
|
*/ |
|
92
|
|
|
public function requiredLocales() |
|
93
|
|
|
{ |
|
94
|
|
|
return LaravelLocalization::getSupportedLocales(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the validation for attributes key from the implementing class |
|
99
|
|
|
* or use a sensible default |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getTranslationsAttributesKey() |
|
103
|
|
|
{ |
|
104
|
|
|
return rtrim($this->translationsAttributesKey, '.') . '.'; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.