Luminova Framework

PHP Luminova: Encode and Compress Data

Last updated: 2026-11-28 20:57:12

Encode and decode Base32 or URL-safe values, and compress data or HTTP responses.

Use the same alphabet for both halves of a Base32 round trip. Luminova’s URL-safe value encoding wraps the input in JSON and uses its own Base64 character substitutions. For HTTP response compression, the framework can choose an available encoding from the request headers.


Usage

use Luminova\Utility\Encoder;

$encoded = Encoder::base32Encode('hello');
echo $encoded; // NBSWY3DP
echo Encoder::base32Decode($encoded); // hello

$payload = Encoder::base64UrlEncode(['page' => 2]);
if ($payload !== null) {
    print_r(Encoder::base64UrlDecode($payload)); // ['page' => 2]
}

Class Definition

  • Full namespace: Luminova\Utility\Encoder
  • This class is final.

Base32 Alphabets

NameConstant
rfc4648BASE32_RFC4648
crockfordBASE32_CROCKFORD
sid5BASE32_SID_5BITS

Methods

compress

Compress content and keep the compressed form only when it is smaller than the original. Unsupported encodings and skipped or unsuccessful compression return the original bytes.

public static function compress(
    string $content,
    ?string $encoding = null,
    ?int $minLength = 1024
): array

Parameters:

ParameterTypeDescription
$contentstringContent to compress.
$encodingstring\|nullExplicit encoding or null to resolve one from the request (default: null).
$minLengthint\|nullMinimum input size. Use 0 or null to skip the threshold (default: 1024).

Return:

array{0: string|null, 1: string, 2: int} - Encoding or null, output bytes, and output length in bytes.

Examples:

$content = str_repeat('Hello ', 512);
[$encoding, $body] = Encoder::compress($content, 'gzip');

if ($encoding !== null) {
    [, $restored] = Encoder::decompress($body, encoding: $encoding);
    var_dump($restored === $content); // bool(true)
}

compressResponse

Apply response compression when enabled and use the threshold from output.compression.min.length. Successful compression can populate the last-operation metadata.

public static function compressResponse(string $content): array

Parameters:

ParameterTypeDescription
$contentstringResponse body.

Return:

array{0: string|null, 1: string, 2: int} - Encoding or null, output bytes, and output length in bytes.


getLastResponseCompression

Read the most recently recorded successful compression. Calls that skip compression can leave earlier metadata in place.

public static function getLastResponseCompression(): ?array

Return:

array{encoding: string, handler: string, contentLength: int, compressedLength: int}|null - Encoding, PHP handler, and the original and compressed byte counts, or null if none were recorded.


decompress

Invoke the resolved decoder. If there is no handler or it returns false, return the original input. The current deflate decoder maps to gzdecode(), so use gzip for a round trip with this class.

public static function decompress(string $content, int $maxLength = 0, ?string $encoding = null): array

Parameters:

ParameterTypeDescription
$contentstringCompressed input.
$maxLengthintMaximum output size passed to the decoder. Use 0 for the decoder default (default: 0).
$encodingstring\|nullExplicit encoding or null to read the request content encoding (default: null).

Return:

array{0: string|null, 1: string} - Encoding and decoded bytes, or null and the original input if decoding was skipped or failed.


isBase64Encoded

Check by decoding and comparing a canonical re-encoding. The check requires a nonempty value whose trimmed length is divisible by four and accepts standard or normalized URL-safe Base64 characters.

public static function isBase64Encoded(string $data, bool $strict = true): bool

Parameters:

ParameterTypeDescription
$datastringCandidate Base64 text.
$strictboolWhether to request strict decoding from PHP (default: true).

Return:

bool - Whether the canonical check passes.


base32Encoder

Encode raw bytes using a custom alphabet. The output is unpadded. If input is omitted, the method generates monotonic random bytes.

public static function base32Encoder(?string $input = null, ?string $alphabet = null): string

Parameters:

ParameterTypeDescription
$inputstring\|nullRaw input bytes or null to generate bytes (default: null).
$alphabetstring\|nullCustom alphabet of exactly 32 bytes. Omit it for the RFC 4648 alphabet (default: null).

Return:

string - Unpadded Base32 string.

Throws:


base32Encode

Encode raw bytes with a named alphabet.

public static function base32Encode(string $input, ?string $type = null): string

Parameters:

ParameterTypeDescription
$inputstringRaw input bytes.
$typestring\|nullAlphabet name. Omit it for rfc4648 (default: null).

Return:

string - Unpadded Base32 string.

Throws:


base64UrlEncode

Wrap the value under a JSON data key, Base64-encode it, and substitute +, /, and = with ., _, and - respectively.

public static function base64UrlEncode(mixed $value): ?string

Parameters:

ParameterTypeDescription
$valuemixedJSON-encodable value.

Return:

string|null - Encoded string, or null on JSON encoding failure.


base64UrlDecode

Reverse the utility’s character substitutions and decode the JSON data value. JSON objects are returned as associative arrays.

public static function base64UrlDecode(string $value): mixed

Parameters:

ParameterTypeDescription
$valuestringValue produced by base64UrlEncode().

Return:

mixed - Decoded value, or null for invalid input, missing data, or encoded null.


base32Decode

Decode bytes produced with a named alphabet. Use the same alphabet name as the encoder.

public static function base32Decode(string $input, ?string $type = null): string

Parameters:

ParameterTypeDescription
$inputstringUnpadded Base32 string.
$typestring\|nullAlphabet name. Omit it for rfc4648 (default: null).

Return:

string - Decoded bytes.

Throws:


base32Decoder

Decode a Base32 string with a custom or default alphabet. Input characters are case-sensitive.

public static function base32Decoder(string $input, ?string $alphabet = null): string

Parameters:

ParameterTypeDescription
$inputstringUnpadded Base32 string.
$alphabetstring\|nullCustom alphabet of exactly 32 bytes. Omit it for the RFC 4648 alphabet (default: null).

Return:

string - Decoded bytes.

Throws:


resolveEncoding

Resolve an outgoing encoding from the accepted encodings or read the incoming content encoding. Resolved values can be cached independently for the two directions.

public static function resolveEncoding(bool $forCompression): ?string

Parameters:

ParameterTypeDescription
$forCompressionboolWhether to resolve the outgoing encoding rather than the incoming one.

Return:

string|null - Resolved encoding, or null when unavailable.


resolveCompressionHandler

Map an encoding to an available PHP function. The recognized names include gzip, x-gzip, deflate, compress, zstd, and br.

public static function resolveCompressionHandler(string $encoding, bool $encoder = true): ?string

Parameters:

ParameterTypeDescription
$encodingstringContent encoding.
$encoderboolWhether to return the encoder instead of the decoder (default: true).

Return:

string|null - Function name, or null when unavailable.