Completed
Pull Request — master (#34)
by Sebastian
04:04
created

helpers.php ➔ rgb_to_hex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
function locale():string
4
{
5
    return app()->getLocale();
6
}
7
8
function content_locale():string
9
{
10
    return app('currentLocale')->getContentLocale();
11
}
12
13
function fragment($name, array $replacements = []):string
14
{
15
    return trans($name, $replacements);
16
}
17
18
function fragment_slug($name, array $replacements = []):string
19
{
20
    $translation = fragment($name, $replacements);
21
22
    return str_slug($translation);
23
}
24
25
function translate_field_name(string $name, string $locale = ''):string
26
{
27
    $locale = $locale ?? content_locale();
28
29
    return "translated_{$locale}_{$name}";
30
}
31
32
function article(string $technicalName):App\Models\Article
33
{
34
    return App\Models\Article::findByTechnicalName($technicalName);
35
}
36
37
function carbon(string $date, string $format = 'Y-m-d H:i:s'):Carbon\Carbon
38
{
39
    return Carbon\Carbon::createFromFormat($format, $date);
40
}
41
42
function diff_date_for_humans(Carbon\Carbon $date):string
43
{
44
    return (new Jenssegers\Date\Date($date->timestamp))->ago();
45
}
46
47
function roman_year(int $year = null):string
48
{
49
    if (!is_numeric($year)) {
50
        $year = date('Y');
51
    }
52
53
    $result = '';
54
55
    $romanNumerals = [
56
        'M' => 1000,
57
        'CM' => 900,
58
        'D' => 500,
59
        'CD' => 400,
60
        'C' => 100,
61
        'XC' => 90,
62
        'L' => 50,
63
        'XL' => 40,
64
        'X' => 10,
65
        'IX' => 9,
66
        'V' => 5,
67
        'IV' => 4,
68
        'I' => 1,
69
    ];
70
71
    foreach ($romanNumerals as $roman => $yearNumber) {
72
        // Divide to get  matches
73
        $matches = intval($year / $yearNumber);
74
75
        // Assign the roman char * $matches
76
        $result .= str_repeat($roman, $matches);
77
78
        // Substract from the number
79
        $year = $year % $yearNumber;
80
    }
81
82
    return $result;
83
}
84
85
function short_class_name($object):string
86
{
87
    $objectProperties = new \ReflectionClass($object);
88
89
    return $objectProperties->getShortName();
90
}
91
92
function class_constants($object, string  $startsWithFilter = ''):array
93
{
94
    $objectProperties = new \ReflectionClass($object);
95
96
    $constants = $objectProperties->getConstants();
97
98
    if ($startsWithFilter == '') {
99
        return $constants;
100
    }
101
102
    return array_filter($constants, function ($key) use ($startsWithFilter) {
103
        return starts_with(strtolower($key), strtolower($startsWithFilter));
104
    }, ARRAY_FILTER_USE_KEY);
105
}
106
107
/**
108
 * @return \App\Services\Auth\Back\User|\App\Services\Auth\Front\User|null
109
 *
110
 * @throws \Exception
111
 */
112
function current_user()
113
{
114
    if (request()->isFront()) {
115
        return current_front_user();
116
    }
117
118
    if (request()->isBack()) {
119
        return current_back_user();
120
    }
121
122
    throw new \Exception('Request was neither for front or back');
123
}
124
125
/** @return \App\Services\Auth\Front\User|null */
126
function current_front_user()
127
{
128
    if (!auth()->guard('front')->check()) {
129
        return;
130
    }
131
132
    return auth()->guard('front')->user();
133
}
134
135
/** @return \App\Services\Auth\Back\User|null */
136
function current_back_user()
137
{
138
    if (!auth()->guard('back')->check()) {
139
        return;
140
    }
141
142
    return auth()->guard('back')->user();
143
}
144
145
function login_url():string
146
{
147
    return request()->isFront() ?
148
        action('Front\AuthController@getLogin') :
149
        action('Back\AuthController@getLogin');
150
}
151
152
function logout_url():string
153
{
154
    return request()->isFront() ?
155
        action('Front\AuthController@getLogout') :
156
        action('Back\AuthController@getLogout');
157
}
158
159
function register_url():string
160
{
161
    return action('Front\AuthController@getRegister');
162
}
163
164
/**
165
 * Validate some data.
166
 *
167
 * @param string|array $fields
168
 * @param string|array $rules
169
 *
170
 * @return bool
171
 */
172
function validate($fields, $rules):bool
173
{
174
    if (!is_array($fields)) {
175
        $fields = ['default' => $fields];
176
    }
177
178
    if (!is_array($rules)) {
179
        $rules = ['default' => $rules];
180
    }
181
182
    return Validator::make($fields, $rules)->passes();
183
}
184
185
function activity(string $message)
186
{
187
    Activity::log($message);
188
}
189
190
function el(string $tag, $attributes = null, $contents = null):string
191
{
192
    return \Spatie\HtmlElement\HtmlElement::render($tag, $attributes, $contents);
193
}
194
195
function rgb_to_hex(int $red, int $green, int $blue): string
196
{
197
    return '#' . collect([$red, $green, $blue])
198
        ->map(function (int $decimal): string {
199
            return str_pad(dechex($decimal), 2, STR_PAD_LEFT);
200
        })
201
        ->implode('');
202
}
203