Skip to content

Utilities

Viames Marino edited this page Feb 23, 2026 · 1 revision

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): void
  • jsonError(string $errorCode, string $errorMessage, int $httpCode = 400, array $extra = []): void
  • jsonSuccess(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): string
  • cleanFilename(string $string, ?string $sep = null): string
  • slugify(string $text): string
  • uniqueFilename(string $filename, string $path): string
  • randomFilename(string $extension, string $path): string

File/media checks

  • isImage(string $file): bool
  • isPdf(string $file): bool
  • isJson(string $string): bool

Date/time helpers

  • getDateTimeFromRfc(string $date): ?DateTime
  • intlFormat(?string $format = null, DateTimeInterface|null $dateTime = null): string
  • getTimeago($date): string

Misc useful helpers

  • getRandomString(int $length): string
  • arrayToEmail(array $array): array
  • arrayToInt(array $array): array
  • arrayToPositive(array $array): array
  • getExecutablePath(string $executable, ?string $envKey = null): ?string

Notes

  • This class is intentionally wide; prefer app-specific helpers for domain logic.
  • JSON helpers are commonly used by API and AJAX exception flows.

See also: ApiResponse, Request, Upload.

Clone this wiki locally