Utilities - viames/pair GitHub Wiki
Pair framework: Utilities
Pair\Helpers\Utilities provides a broad set of static helpers for strings, files, JSON responses, dates, and formatting.
Most important methods
JSON/API output
jsonResponse(object|array|null $data, int $httpCode = 200): voidjsonError(string $errorCode, string $errorMessage, int $httpCode = 400, array $extra = []): voidjsonSuccess(string $message): void
Implementation example:
use Pair\Helpers\Utilities;
if (!$valid) {
Utilities::jsonError('BAD_REQUEST', 'Invalid payload', 400);
}
Utilities::jsonResponse(['saved' => true], 201);
Text and file naming
cleanUp(string $string, ?string $sep = null): stringcleanFilename(string $string, ?string $sep = null): stringslugify(string $text): stringuniqueFilename(string $filename, string $path): stringrandomFilename(string $extension, string $path): string
File/media checks
isImage(string $file): boolisPdf(string $file): boolisJson(string $string): bool
Date/time helpers
getDateTimeFromRfc(string $date): ?DateTimeintlFormat(?string $format = null, DateTimeInterface|null $dateTime = null): stringgetTimeago($date): string
Misc useful helpers
getRandomString(int $length): stringarrayToEmail(array $array): arrayarrayToInt(array $array): arrayarrayToPositive(array $array): arraygetExecutablePath(string $executable, ?string $envKey = null): ?stringshowNoDataAlert(?string $customMessage = null): void
Notes
- This class is intentionally wide; prefer app-specific helpers for domain logic.
- JSON helpers are commonly used by API and AJAX exception flows.
showNoDataAlert()follows the UI framework selected throughApplication::uiFramework(): native<div role="alert">by default, Bootstrap alerts whenbootstrapis selected, and Bulma notifications whenbulmais selected.
See also: ApiResponse, Request, UploadedFile.