1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Utils;
|
---|
4 |
|
---|
5 | use App\Http\Controllers\Controller;
|
---|
6 | use Illuminate\Http\Request;
|
---|
7 | use Illuminate\Support\Str;
|
---|
8 | use Carbon\Carbon;
|
---|
9 | use Laravolt\Avatar\Avatar;
|
---|
10 |
|
---|
11 | class UtilController extends Controller
|
---|
12 | {
|
---|
13 | /**
|
---|
14 | * Slug function, creates a user readable slug from a provided string (title)
|
---|
15 | * @param string $string
|
---|
16 | * @return string;
|
---|
17 | */
|
---|
18 | public static function generateSlugFromString($string): string
|
---|
19 | {
|
---|
20 | return Str::slug($string);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Generate token from a provided email
|
---|
25 | * @param string $email
|
---|
26 | * @return string
|
---|
27 | */
|
---|
28 | public static function generateToken(string $email = ''): string
|
---|
29 | {
|
---|
30 | return substr(md5(rand(0, 9) . $email . time()), 0, 32);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Check if string is base64 encoded
|
---|
35 | * @param string $data
|
---|
36 | * @return boolean;
|
---|
37 | */
|
---|
38 | public static function isBase64Encoded($data): bool
|
---|
39 | {
|
---|
40 | if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $data)) {
|
---|
41 | return TRUE;
|
---|
42 | } else {
|
---|
43 | return FALSE;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Replace the X occurrences of a character/ string in a string
|
---|
49 | * @param string $from
|
---|
50 | * @param string $to
|
---|
51 | * @param string $content
|
---|
52 | * @param int $number
|
---|
53 | * @return string;
|
---|
54 | */
|
---|
55 | public static function replaceFirstString($from, $to, $content, $number): string
|
---|
56 | {
|
---|
57 | $from = '/'.preg_quote($from, '/').'/';
|
---|
58 |
|
---|
59 | return preg_replace($from, $to, $content, $number);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Count occurances of a particular character/ string in a string
|
---|
64 | * @param string $string
|
---|
65 | * @param string $substring
|
---|
66 | *
|
---|
67 | */
|
---|
68 | public static function countOccurrencesInString ($string, $substring): int
|
---|
69 | {
|
---|
70 | return substr_count($string, $substring);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Generate avatar image from name, and save it to a specific path
|
---|
75 | * @param string $image_name
|
---|
76 | * @param string $name
|
---|
77 | * @param string $path
|
---|
78 | */
|
---|
79 | public static function saveAvatarImage(string $image_name, string $name = 'user', string $path = 'app/public/users/')
|
---|
80 | {
|
---|
81 | $storage_path = storage_path($path);
|
---|
82 | if (!file_exists($path)) {
|
---|
83 | mkdir($path, 666, true);
|
---|
84 | }
|
---|
85 |
|
---|
86 | (new Avatar)->create(strtoupper($name))->save($storage_path . $image_name);
|
---|
87 | }
|
---|
88 | }
|
---|