### Install Base62 using Composer Source: https://github.com/tuupola/base62/blob/2.x/README.md Instructions for installing the Base62 library using Composer. It provides commands for the latest version and a specific older branch. ```bash $ composer require tuupola/base62 $ composer require "tuupola/base62:^1.0" ``` -------------------------------- ### Git Commit Message Example Source: https://github.com/tuupola/base62/blob/2.x/CONTRIBUTING.md This example demonstrates the recommended format for Git commit messages. It includes a concise summary line followed by a more detailed explanation, separated by a blank line. This format helps tools process commit history effectively. ```git Capitalized, short (50 chars or less) summary More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); tools like rebase can get confused if you run the two together. ``` -------------------------------- ### Set up Automatic Tests with entr Source: https://github.com/tuupola/base62/blob/2.x/CONTRIBUTING.md This sequence of commands installs the `entr` utility and then starts a watch process that automatically reruns tests whenever code changes are detected. This requires `entr` to be installed via a package manager like Homebrew. ```bash $ brew install entr $ make watch ``` -------------------------------- ### Customizing Base62 Character Sets (PHP) Source: https://github.com/tuupola/base62/blob/2.x/README.md Illustrates how to use different character sets for Base62 encoding, including the default GMP style, an inverted set, and custom character sets. It shows encoding examples for both methods. ```php use Tuupola\Base62; print Base62::GMP; /* 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz */ print Base62::INVERTED; /* 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ */ $default = new Base62(["characters" => Base62::GMP]); $inverted = new Base62(["characters" => Base62::INVERTED]); print $default->encode("Hello world!"); /* T8dgcjRGuYUueWht */ print $inverted->encode("Hello world!"); /* t8DGCJrgUyuUEwHT */ print $default->encodeInteger(27); /* R */ print $inverted->encodeInteger(27); /* r */ ``` -------------------------------- ### Base62 Encode/Decode Integer (PHP) Source: https://github.com/tuupola/base62/blob/2.x/UPGRADING.md Illustrates the recommended way to encode and decode integers using Base62 2.x with the encodeInteger() and decodeInteger() methods. This replaces the previous direct integer handling in encode() and decode(). ```php $encoded = $base62->encodeInteger(98765432); $decoded = $base62->decodeInteger($encoded); ``` -------------------------------- ### Base62 Encode/Decode String (PHP) Source: https://github.com/tuupola/base62/blob/2.x/UPGRADING.md Demonstrates the usage of encode() and decode() methods in Base62 2.x, which now expect and return string types. Previous versions allowed integer inputs directly. ```php $encoded = $base62->encode("987654321", true); $decoded = $base62->decode($encoded, true); ``` -------------------------------- ### Handle Base62 Errors and Validate Character Sets Source: https://context7.com/tuupola/base62/llms.txt Provides examples of robust error handling within the Base62 library, focusing on decoding invalid data and validating character sets during encoder instantiation. Demonstrates catching `InvalidArgumentException` for malformed data or character sets. Requires the 'tuupola/base62' package via Composer. ```php decode($invalid); } catch (\InvalidArgumentException $e) { echo "Decode error: " . $e->getMessage() . "\n"; // Output: Decode error: Data contains invalid characters "~!@#$" } // Validate character set on construction try { // Character set with duplicates (invalid) $badEncoder = new Base62([ "characters" => "0023456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ]); } catch (\InvalidArgumentException $e) { echo "Charset error: " . $e->getMessage() . "\n"; // Output: Charset error: Character set must contain 62 unique characters } // Decode with wrong character set $encoder1 = new Base62(["characters" => Base62::GMP]); $encoded = $encoder1->encode("Hello"); try { // Missing 'T' character in custom set $encoder2 = new Base62([ "characters" => "0123456789ABCDEFGHIJKLMNOPQRSabcdefghijklmnopqrstuvwxyz-UVWXYZ" ]); $decoded = $encoder2->decode($encoded); } catch (\InvalidArgumentException $e) { echo "Character mismatch: " . $e->getMessage() . "\n"; // Output example: Character mismatch: Data contains invalid characters "T" } // Safe encoding/decoding workflow $data = "Sensitive data"; $encoded = $base62->encode($data); if (strlen($encoded) > 0) { $decoded = $base62->decode($encoded); echo "Success: " . ($data === $decoded ? "verified" : "failed") . "\n"; // Output: Success: verified } ``` -------------------------------- ### Using Base62 Static Proxy (PHP) Source: https://github.com/tuupola/base62/blob/2.x/README.md Shows how to utilize the static proxy `Base62Proxy` for convenient access to Base62 encoding and decoding functionalities without instantiating the class. ```php use Tuupola\Base62Proxy as Base62; $encoded = Base62::encode(random_bytes(128)); $decoded = Base62::decode($encoded); $encoded2 = Base62::encodeInteger(987654321); $decoded2 = Base62::decodeInteger($encoded2); ``` -------------------------------- ### Run Base62 Tests Source: https://github.com/tuupola/base62/blob/2.x/README.md Instructions for running the library's test suite. It provides commands for manual testing and for setting up watch mode with `entr` for automatic testing on file changes. ```bash $ make test $ brew install entr $ make watch ``` -------------------------------- ### Direct GmpEncoder Usage (PHP) Source: https://github.com/tuupola/base62/blob/2.x/README.md Demonstrates how to directly instantiate and use the `GmpEncoder` if the GMP extension is confirmed to be available. This is useful for performance-critical applications. ```php if (function_exists("gmp_init")) { $base62 = new Tuupola\Base62\GmpEncoder(); $base62->encode("Hello world!"); /* T8dgcjRGuYUueWht */ } else { /* Handle error... */ } ``` -------------------------------- ### Configure Base62 Encoder with Custom Character Sets Source: https://context7.com/tuupola/base62/llms.txt Demonstrates how to instantiate a Base62 encoder with default, inverted, and custom character sets. It shows encoding and decoding with these sets and includes error handling for invalid character set configurations. Requires the 'tuupola/base62' package via Composer. ```php Base62::GMP]); $defaultEncoded = $defaultEncoder->encode($data); echo "GMP encoded: " . $defaultEncoded . "\n"; // Output: GMP encoded: T8dgcjRGuYUueWht // Use inverted character set $invertedEncoder = new Base62(["characters" => Base62::INVERTED]); $invertedEncoded = $invertedEncoder->encode($data); echo "Inverted encoded: " . $invertedEncoded . "\n"; // Output: Inverted encoded: t8DGCJrgUyuUEwHT // Use custom character set (must be exactly 62 unique characters) $customCharset = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $customEncoder = new Base62(["characters" => $customCharset]); $customEncoded = $customEncoder->encode($data); echo "Custom encoded: " . $customEncoded . "\n"; // Output: Custom encoded: t9DGCJrgUyuUEwHT // Decode with matching character set $decoded = $customEncoder->decode($customEncoded); echo "Decoded: " . $decoded . "\n"; // Output: Decoded: Hello world! // Error handling for invalid character sets try { $invalidEncoder = new Base62(["characters" => "0123456789ABC"]); // Too short } catch (\InvalidArgumentException $e) { echo "Error: " . $e->getMessage() . "\n"; // Output: Error: Character set must contain 62 unique characters } ``` -------------------------------- ### Encode and Decode Arbitrary Data with Base62 (PHP) Source: https://github.com/tuupola/base62/blob/2.x/README.md Demonstrates how to use the Base62 class to encode arbitrary binary data and decode it back. It utilizes either GMP or a pure PHP implementation based on availability. ```php use Tuupola\Base62; $base62 = new Base62(); $encoded = $base62->encode(random_bytes(128)); $decoded = $base62->decode($encoded); ``` -------------------------------- ### Run Tests Manually Source: https://github.com/tuupola/base62/blob/2.x/CONTRIBUTING.md This command executes the test suite for the base62 project. It's a manual way to ensure all tests pass before committing changes. ```bash $ make test ``` -------------------------------- ### Encode and Decode Integers with Base62 (PHP) Source: https://github.com/tuupola/base62/blob/2.x/README.md Shows how to encode integers into Base62 strings and decode them back using the `encodeInteger` and `decodeInteger` methods. This is distinct from encoding string representations of integers. ```php use Tuupola\Base62; $base62 = new Base62(); $integer = $base62->encodeInteger(987654321); /* 14q60P */ print $base62->decodeInteger("14q60P"); /* 987654321 */ ``` -------------------------------- ### Encode/Decode Data Using Base62 Static Proxy Interface Source: https://context7.com/tuupola/base62/llms.txt Utilizes the static Base62 proxy interface for encoding and decoding arbitrary data or integers without direct object instantiation. Allows configuration of static options like custom character sets. Requires the 'tuupola/base62' package via Composer. ```php Base62::INVERTED, ]; $invertedEncoded = Base62::encode("Test data"); echo "Inverted encoded: " . $invertedEncoded . "\n"; // Output example: Inverted encoded: rLnK3mJp8sW // Reset to defaults Base62::$options = [ "characters" => Base62::GMP, ]; ``` -------------------------------- ### Encode/Decode Integers in PHP Source: https://context7.com/tuupola/base62/llms.txt Converts integers to base62 strings and decodes them back to their original integer values using optimized integer encoding methods. This is distinct from encoding the string representation of an integer. ```php encodeInteger($number); echo "Encoded integer: " . $encodedInt . "\n"; // Output: Encoded integer: 14q60P // Decode back to integer $decodedInt = $base62->decodeInteger($encodedInt); echo "Decoded integer: " . $decodedInt . "\n"; // Output: Decoded integer: 987654321 // Encode large integers (PHP_INT_MAX) $largeNumber = PHP_INT_MAX; $encodedLarge = $base62->encodeInteger($largeNumber); echo "Encoded large: " . $encodedLarge . "\n"; // Output example (64-bit): Encoded large: AzL8n0Y58m7 // Important: String vs Integer encoding produces different results $stringEncoded = $base62->encode("987654321"); $intEncoded = $base62->encodeInteger(987654321); echo "String '987654321' encoded: " . $stringEncoded . "\n"; // Output: String '987654321' encoded: KHc6iHtXW3iD echo "Integer 987654321 encoded: " . $intEncoded . "\n"; // Output: Integer 987654321 encoded: 14q60P echo "Results are different: " . ($stringEncoded !== $intEncoded ? "true" : "false") . "\n"; // Output: Results are different: true ``` -------------------------------- ### Encode/Decode Arbitrary Binary Data in PHP Source: https://context7.com/tuupola/base62/llms.txt Encodes arbitrary binary data or strings into base62-encoded strings using the default GMP character set and decodes them back to the original data. This function is useful for creating compact, readable identifiers. It handles binary data with leading zeros correctly. ```php encode($data); echo "Encoded: " . $encoded . "\n"; // Output example: Encoded: 7Qm3zXG9dLkMnPrTvWxY // Encode string data $message = "Hello world!"; $encodedMessage = $base62->encode($message); echo "Encoded message: " . $encodedMessage . "\n"; // Output: Encoded message: T8dgcjRGuYUueWht // Decode back to original $decoded = $base62->decode($encodedMessage); echo "Decoded: " . $decoded . "\n"; // Output: Decoded: Hello world! // Handle binary data with leading zeros $binaryData = hex2bin("0000010203040506"); $encodedBinary = $base62->encode($binaryData); echo "Encoded binary: " . $encodedBinary . "\n"; // Output: Encoded binary: 00JVb3WII $decodedBinary = $base62->decode($encodedBinary); echo "Decoded matches: " . ($binaryData === $decodedBinary ? "true" : "false") . "\n"; // Output: Decoded matches: true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.