Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit bda9771

Browse files
committed
Clean up helpers
- allow non-unique abbreviation - refactor str_slug_fqcn - rename userlog to audit, and accept 3rd argument optionally
1 parent d183fe0 commit bda9771

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

app/Support/helpers.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,27 @@ function generate_sequence($input = 1)
1515
* Get Abbreviation fo the given text
1616
*/
1717
if (!function_exists('abbrv')) {
18-
function abbrv($value)
18+
function abbrv($value, $unique_characters = true)
1919
{
2020
$removeNonAlphanumeric = preg_replace("/[^A-Za-z0-9 ]/", '', $value);
2121
$removeVowels = str_replace(
2222
['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U', ' '],
2323
'',
2424
$removeNonAlphanumeric);
25-
$uppercase = strtoupper($removeVowels);
26-
$split = str_split($uppercase);
27-
$unique_characters = [];
28-
foreach ($split as $character) {
29-
if (!in_array($character, $unique_characters)) {
30-
$unique_characters[] = $character;
25+
26+
$uppercase = strtoupper($removeVowels);
27+
28+
if ($unique_characters) {
29+
$split = str_split($uppercase);
30+
$unique_characters = [];
31+
foreach ($split as $character) {
32+
if (!in_array($character, $unique_characters)) {
33+
$unique_characters[] = $character;
34+
}
3135
}
36+
return implode('', $unique_characters);
3237
}
33-
return implode('', $unique_characters);
38+
return $uppercase;
3439
}
3540
}
3641

@@ -77,23 +82,27 @@ function hashids($salt = null, $length = null, $alphabet = null)
7782
* Get Slug Name for Fully Qualified Class Name (FQCN)
7883
*/
7984
if (!function_exists('str_slug_fqcn')) {
80-
function str_slug_fqcn($value)
85+
function str_slug_fqcn($object)
8186
{
82-
$fqcn = get_class($value);
83-
$fqcn = str_replace('\\', '-', $fqcn);
84-
return strtolower($fqcn);
87+
return strtolower(str_replace('\\', '-', get_class($object)));
8588
}
8689
}
8790

8891
/**
89-
* Log Current logged in user activities
92+
* Audit Trail
9093
*/
91-
if (!function_exists('userlog')) {
92-
function userlog($model, $message)
94+
if (!function_exists('audit')) {
95+
function audit($model, $message, $causedBy = null)
9396
{
94-
activity()
95-
->performedOn($model)
96-
->causedBy(auth()->user())
97-
->log($message);
97+
if (is_null($causedBy)) {
98+
activity()
99+
->performedOn($model)
100+
->log($message);
101+
} else {
102+
activity()
103+
->performedOn($model)
104+
->causedBy(auth()->user())
105+
->log($message);
106+
}
98107
}
99108
}

0 commit comments

Comments
 (0)