1 | <?php |
||
9 | class Localization |
||
10 | { |
||
11 | /** @var \Illuminate\Contracts\Container\Container */ |
||
12 | protected $app; |
||
13 | |||
14 | 27 | public function __construct(Container $app) |
|
18 | |||
19 | 23 | protected function config(string $key) |
|
23 | |||
24 | 21 | public function getLocale(): string |
|
28 | |||
29 | 22 | public function setLocale(string $locale = '') |
|
33 | |||
34 | 3 | public function isCurrentLocale(string $locale = ''): bool |
|
38 | |||
39 | 22 | public function getSupportedLocale(string $locale = ''): Collection |
|
40 | { |
||
41 | 22 | $locales = $this->getSupportedLocales(); |
|
42 | |||
43 | 22 | if ($locales->has($locale)) { |
|
44 | 22 | return collect($locales->get($locale)); |
|
45 | } |
||
46 | |||
47 | 1 | return collect([]); |
|
48 | } |
||
49 | |||
50 | 23 | public function getSupportedLocales(): Collection |
|
54 | |||
55 | 19 | public function getSupportedLocalesKeys(): Collection |
|
59 | |||
60 | 9 | public function isSupportedLocale(string $locale = ''): bool |
|
64 | |||
65 | 4 | public function getDefaultLocale(): string |
|
69 | |||
70 | 2 | public function isDefaultLocale(string $locale = ''): bool |
|
74 | |||
75 | 19 | public function shouldHideLocaleInUrl($locale) |
|
80 | |||
81 | 6 | public function route(string $name, array $parameters = [], bool $absolute = true, string $locale = ''): string |
|
89 | |||
90 | 2 | public function currentRoute(string $locale, bool $absolute = true): string |
|
103 | |||
104 | 2 | public function renderSwitch(string $view = 'localization::switch', array $data = []): HtmlString |
|
110 | } |
||
111 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: