File manager - Edit - /var/www/html/spdc/api/app/Helpers/main.php
Back
<?php use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Http; use Intervention\Image\Facades\Image; use Illuminate\Support\Facades\Storage; if (! function_exists('authServiceClient')) { function authServiceClient(string $method, string $url, Request|array|null $request = null, array $headers = []) { $bearerToken = isset($request) && $request instanceof Request && !empty($request->bearerToken()) ? 'Bearer ' . $request->bearerToken() : null; $httpClient = Http::withHeaders([ 'Accept' => 'application/json', 'Authorization' => $bearerToken, 'App' => env('CLIENT_APP_KEY'), ...$headers ]); switch(strtoupper($method)) { case 'GET': return $httpClient->get($url); case 'POST': return $httpClient->post($url, $request); case 'PUT': return $httpClient->put($url, $request); case 'PATCH': return $httpClient->patch($url, $request); case "DELETE": return $httpClient->delete($url); } } } if (! function_exists('authLogin')) { function authLogin() { // login $url = env('AUTH_SERVICE_URL') . '/api/login'; $loginResponse = authServiceClient('post', $url, [ 'grant_type' => 'password', 'client_id' => env('PASSPORT_CLIENT_ID'), 'client_secret' => env('PASSPORT_SECRET'), 'username' => env('USERNAME'), 'password' => env('PASSWORD'), 'scope' => '', ]); if (!$loginResponse->successful()) new \Exception(json_encode($loginResponse->json())); return $loginResponse->json(); } } if (! function_exists('treatArrayInput')) { function treatArrayInput($input) { $formattedInput = $input; if (is_string($input)) { /** * cheking if is a valid json as a string */ $formattedInput = json_decode($input); /** * if not, checking if it is input separated by comma. Ex.: banana, orange, tomato */ if (is_null($formattedInput)) { $formattedInput = explode(",", $input); } } if (is_null($input) || empty($input)) $formattedInput = []; return $formattedInput; } } if (! function_exists('uploadFile')) { function uploadFile(UploadedFile|string $file, string $path = '/', string $extension = 'png'): string { if ($file instanceof UploadedFile) { $filePath = $file->store($path); // 'uploads' is a directory within the storage/app directory return $filePath; } else if (is_string($file)) { $extension = strtolower($extension); // Decode the base64 data $base64Data = $file; $fileData = base64_decode(preg_replace('#^data:\w+/\w+;base64,#i', '', $base64Data)); if (empty($fileData)) { return ''; } // Generate a unique filename $filename = uniqid() . '.' . $extension; // You can change the file extension as needed if ($path != '/') $filePath = $path . '/' . $filename; else $filePath = $filename; // Store the file in the storage/app/public directory Storage::put($filePath, $fileData); return $filePath; } else { return ''; } } } if (! function_exists('removeFile')) { function removeFile(string $filePath): bool { if (Storage::exists($filePath)) { // Delete the file Storage::delete($filePath); return true; // File was successfully deleted } return false; } } if (! function_exists('generateValidCPF')) { function generateValidCPF() { // Generate the first nine digits $digits = []; for ($i = 0; $i < 9; $i++) { $digits[] = mt_rand(0, 9); } // Calculate the first checksum digit $sum = 0; for ($i = 0; $i < 9; $i++) { $sum += $digits[$i] * (10 - $i); } $remainder = $sum % 11; $digit1 = ($remainder < 2) ? 0 : (11 - $remainder); // Add the first checksum digit $digits[] = $digit1; // Calculate the second checksum digit $sum = 0; for ($i = 0; $i < 10; $i++) { $sum += $digits[$i] * (11 - $i); } $remainder = $sum % 11; $digit2 = ($remainder < 2) ? 0 : (11 - $remainder); // Add the second checksum digit $digits[] = $digit2; // Format the CPF as a string $cpf = implode('', $digits); return $cpf; } } if (! function_exists('getEnumValuesToArray')) { function getEnumValuesToArray(array $cases): array { foreach ($cases as $case) { $values[] = $case->value; } return $values; } } if (! function_exists('uploadImage')) { function uploadImage(\Illuminate\Http\UploadedFile $file, $path = '/', int $rotateDegrees = 0): string { $fileName = str()->random(5) . time() . '.' . $file->getClientOriginalExtension(); $imagePath = $path . '/' . $fileName; $image = Image::make($file)->rotate($rotateDegrees)->encode($file->getClientOriginalExtension()); $image->save(storage_path("app/$path/") . $fileName); return $imagePath; } } if (! function_exists('getCharactersBetweenStrings')) { function getCharactersBetweenStrings($input, $start, $end) { $startPos = strpos($input, $start); if ($startPos === false) { return false; // Start string not found } $endPos = strpos($input, $end, $startPos + strlen($start)); if ($endPos === false) { return false; // End string not found } $startPos += strlen($start); return substr($input, $startPos, $endPos - $startPos); } } if (! function_exists('getCharactersAfterCharacter')) { function getCharactersAfterCharacter($input, $char) { $position = strpos($input, $char); if ($position === false) { return false; // Character not found in the string } // Use substr to get all characters after the specified character return substr($input, $position + 1); } } /** * get all items from a specific key in a multidimensional array */ if (! function_exists('getItemsByKey')) { function getItemsByKey($array, $keyToSearch): array { $collection = collect($array); // Use the pluck method to get all items with the specified key return $collection->pluck($keyToSearch)->toArray(); } } if (! function_exists('getUsersInfo')) { function getUsersInfo(array $usersIds) { $usersInfo = []; $filters = ''; // like[0]=name,meh&like[1]=username,dar for($i = 0; $i < count($usersIds); $i++) { if ($i == 0) { $filters = "?like[$i]=id," . $usersIds[$i]; } else { $filters .= "&like[$i]=id," . $usersIds[$i]; } } $url = env('AUTH_SERVICE_URL') . '/api/user' . $filters . '&without_paginate=1'; $usersResponse = authServiceClient('get', $url, null, [ 'Authorization' => 'Bearer ' . authLogin()['access_token'] ]); if ($usersResponse->successful()) { $usersInfo = $usersResponse->json()['data']; } return $usersInfo; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings