PHP Luminova: Encode and Compress Data
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
| Name | Constant |
|---|---|
rfc4648 | BASE32_RFC4648 |
crockford | BASE32_CROCKFORD |
sid5 | BASE32_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
): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | Content to compress. |
$encoding | string\|null | Explicit encoding or null to resolve one from the request (default: null). |
$minLength | int\|null | Minimum 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): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | Response 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(): ?arrayReturn:
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): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | Compressed input. |
$maxLength | int | Maximum output size passed to the decoder. Use 0 for the decoder default (default: 0). |
$encoding | string\|null | Explicit 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | Candidate Base64 text. |
$strict | bool | Whether 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$input | string\|null | Raw input bytes or null to generate bytes (default: null). |
$alphabet | string\|null | Custom alphabet of exactly 32 bytes. Omit it for the RFC 4648 alphabet (default: null). |
Return:
string - Unpadded Base32 string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the alphabet does not contain exactly 32 bytes.
base32Encode
Encode raw bytes with a named alphabet.
public static function base32Encode(string $input, ?string $type = null): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$input | string | Raw input bytes. |
$type | string\|null | Alphabet name. Omit it for rfc4648 (default: null). |
Return:
string - Unpadded Base32 string.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the alphabet name is unrecognized.
base64UrlEncode
Wrap the value under a JSON data key, Base64-encode it, and substitute +, /, and = with ., _, and - respectively.
public static function base64UrlEncode(mixed $value): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | JSON-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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$value | string | Value 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$input | string | Unpadded Base32 string. |
$type | string\|null | Alphabet name. Omit it for rfc4648 (default: null). |
Return:
string - Decoded bytes.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the alphabet name is unrecognized or the string contains characters outside it.
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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$input | string | Unpadded Base32 string. |
$alphabet | string\|null | Custom alphabet of exactly 32 bytes. Omit it for the RFC 4648 alphabet (default: null). |
Return:
string - Decoded bytes.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the alphabet length is invalid or the input contains an unsupported character.
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): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$forCompression | bool | Whether 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): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$encoding | string | Content encoding. |
$encoder | bool | Whether to return the encoder instead of the decoder (default: true). |
Return:
string|null - Function name, or null when unavailable.