|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use App\Models\Fragment; |
|
4
|
|
|
use Illuminate\Support\Collection; |
|
5
|
|
|
|
|
6
|
|
|
function article(string $specialArticle): App\Models\Article |
|
7
|
|
|
{ |
|
8
|
|
|
return App\Repositories\ArticleRepository::findSpecialArticle($specialArticle); |
|
9
|
|
|
} |
|
10
|
|
|
|
|
11
|
|
|
function content_locale(): string |
|
12
|
|
|
{ |
|
13
|
|
|
return \App\Services\Locale\CurrentLocale::getContentLocale(); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @return \App\Services\Auth\Back\User|\App\Services\Auth\Front\User|null |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \Exception |
|
20
|
|
|
*/ |
|
21
|
|
|
function current_user() |
|
22
|
|
|
{ |
|
23
|
|
|
if (request()->isFront()) { |
|
24
|
|
|
return current_front_user(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (request()->isBack()) { |
|
28
|
|
|
return current_back_user(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
throw new \Exception('Coud not determine current user'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return \App\Services\Auth\Front\User|null |
|
36
|
|
|
*/ |
|
37
|
|
|
function current_front_user() |
|
38
|
|
|
{ |
|
39
|
|
|
if (! auth()->guard('front')->check()) { |
|
|
|
|
|
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return auth()->guard('front')->user(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return \App\Services\Auth\Back\User|null |
|
48
|
|
|
*/ |
|
49
|
|
|
function current_back_user() |
|
50
|
|
|
{ |
|
51
|
|
|
if (! auth()->guard('back')->check()) { |
|
|
|
|
|
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return auth()->guard('back')->user(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
function diff_date_for_humans(Carbon\Carbon $date): string |
|
59
|
|
|
{ |
|
60
|
|
|
return (new Jenssegers\Date\Date($date->timestamp))->ago(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function el(string $tag, $attributes = null, $contents = null): string |
|
64
|
|
|
{ |
|
65
|
|
|
return \Spatie\HtmlElement\HtmlElement::render($tag, $attributes, $contents); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
function fragment($name, array $replacements = []): string |
|
69
|
|
|
{ |
|
70
|
|
|
return trans($name, $replacements); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
function fragment_slug($name, array $replacements = []): string |
|
74
|
|
|
{ |
|
75
|
|
|
$translation = fragment($name, $replacements); |
|
76
|
|
|
|
|
77
|
|
|
return str_slug($translation); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
function fragment_image($name, $conversion = 'thumb'): string |
|
81
|
|
|
{ |
|
82
|
|
|
if (! str_contains($name, '.')) { |
|
83
|
|
|
return $name; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
[$group, $key] = explode('.', $name, 2); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$fragment = Fragment::with('media') |
|
|
|
|
|
|
89
|
|
|
->where('group', $group) |
|
90
|
|
|
->where('key', $key) |
|
91
|
|
|
->first(); |
|
92
|
|
|
|
|
93
|
|
|
if (! $fragment) { |
|
94
|
|
|
return $name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $fragment->getFirstMediaUrl('images', $conversion); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function locale(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return app()->getLocale(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
function locales(): Collection |
|
106
|
|
|
{ |
|
107
|
|
|
return collect(config('app.locales')); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
function login_url(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return request()->isFront() ? |
|
113
|
|
|
action('Front\Auth\LoginController@showLoginForm') : |
|
114
|
|
|
action('Back\Auth\LoginController@showLoginForm'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
function logout_url(): string |
|
118
|
|
|
{ |
|
119
|
|
|
return request()->isFront() ? |
|
120
|
|
|
action('Front\Auth\LoginController@logout') : |
|
121
|
|
|
action('Back\Auth\LoginController@logout'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
function roman_year(int $year = null): string |
|
125
|
|
|
{ |
|
126
|
|
|
$year = $year ?? date('Y'); |
|
127
|
|
|
|
|
128
|
|
|
$romanNumerals = [ |
|
129
|
|
|
'M' => 1000, |
|
130
|
|
|
'CM' => 900, |
|
131
|
|
|
'D' => 500, |
|
132
|
|
|
'CD' => 400, |
|
133
|
|
|
'C' => 100, |
|
134
|
|
|
'XC' => 90, |
|
135
|
|
|
'L' => 50, |
|
136
|
|
|
'XL' => 40, |
|
137
|
|
|
'X' => 10, |
|
138
|
|
|
'IX' => 9, |
|
139
|
|
|
'V' => 5, |
|
140
|
|
|
'IV' => 4, |
|
141
|
|
|
'I' => 1, |
|
142
|
|
|
]; |
|
143
|
|
|
|
|
144
|
|
|
$result = ''; |
|
145
|
|
|
|
|
146
|
|
|
foreach ($romanNumerals as $roman => $yearNumber) { |
|
147
|
|
|
// Divide to get matches |
|
148
|
|
|
$matches = intval($year / $yearNumber); |
|
149
|
|
|
|
|
150
|
|
|
// Assign the roman char * $matches |
|
151
|
|
|
$result .= str_repeat($roman, $matches); |
|
152
|
|
|
|
|
153
|
|
|
// Substract from the number |
|
154
|
|
|
$year = $year % $yearNumber; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
function register_url(): string |
|
161
|
|
|
{ |
|
162
|
|
|
return action('Front\Auth\RegisterController@showRegistrationForm'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
function translate_field_name(string $name, string $locale = ''): string |
|
166
|
|
|
{ |
|
167
|
|
|
$locale = $locale ?? content_locale(); |
|
168
|
|
|
|
|
169
|
|
|
return "translated_{$locale}_{$name}"; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Validate some data. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string|array $fields |
|
176
|
|
|
* @param string|array $rules |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
function validate($fields, $rules): bool |
|
181
|
|
|
{ |
|
182
|
|
|
if (! is_array($fields)) { |
|
183
|
|
|
$fields = ['default' => $fields]; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (! is_array($rules)) { |
|
187
|
|
|
$rules = ['default' => $rules]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return Validator::make($fields, $rules)->passes(); |
|
191
|
|
|
} |
|
192
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: