PHP code example of nassiry / laravel-encoder

1. Go to this page and download the library: Download nassiry/laravel-encoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nassiry / laravel-encoder example snippets


$encoder = app('Encoder');

// Encoding and Decoding IDs
$encodedId = $encoder->encodeId(12345);
$decodedId = $encoder->decodeId($encodedId);

// Encoding and Decoding Strings
$encodedString = $encoder->encodeString('Hello World');
$decodedString = $encoder->decodeString($encodedString);

use Nassiry\Encoder\Encoder;

class MyController extends Controller
{
    public function __construct(protected Encoder $encoder)
    {
        // Your other codes
    }

    public function encodeData()
    {
        $encoded = $this->encoder->encodeString('my data');
        return response()->json(['encoded' => $encoded]);
    }
}

use Nassiry\Encoder\Facades\Encoder;

// Encoding and Decoding IDs
$encodedId = Encoder::encodeId(12345);
$decodedId = Encoder::decodeId($encodedId);

// Encoding and Decoding Strings
$encodedString = Encoder::encodeString('Hello World');
$decodedString = Encoder::decodeString($encodedString);



use Nassiry\Encoder\Encoder;

// Create an Encoder instance
$encoder = new Encoder();

// Encoding an ID
$encodedId = $encoder->encodeId(12345);
echo "Encoded ID: $encodedId\n"; // eqwb

// Decoding an ID
$decodedId = $encoder->decodeId($encodedId);
echo "Decoded ID: $decodedId\n"; // 12345

// Encoding an ID with length
$encodedId = $encoder->encodeId(12345, 8); 
echo "Encoded ID: $encodedId\n"; // d29Buhe7

// Decoding an ID
$decodedId = $encoder->decodeId($encodedId);
echo "Decoded ID: $decodedId\n"; // 12345

// Encoding a String
$encodedString = $encoder->encodeString('Hello World');
echo "Encoded String: $encodedString\n"; // 73XpUgyMwkGr29M

// Decoding a String
$decodedString = $encoder->decodeString($encodedString);
echo "Decoded String: $decodedString\n"; // Hello World

$config = [
     1 => '1',
    41 => '59',
    2377 => '1677',
    147299 => '187507',
    9132313 => '5952585',
];

$encoder = new Encoder('base62', $config);

// $length refers to the index (0-based), not the key
$customEncodedId = $encoder->encodeId(12345, 3);

echo "Custom Encoded ID: $customEncodedId\n"; // qVX

'config' => [
    1 => '1',
    5 => '41',
    6 => '2377',
    7 => '147299',
    8 => '9132313',
],

use Nassiry\Encoder\Bases\BaseEncoderInterface;

class Base58 implements BaseEncoderInterface
{
    public function encodeId(int|string $id, int $length): string
    {
        // Implement encoding logic for Base58
    }

    public function decodeId(string $hashed): string
    {
        // Implement decoding logic for Base58
    }
    
    public function encodeString(string $string): string;
    {
        // Implement encoding logic for Base58
    }
    
    public function decodeString(string $encoded): string;
    {
        // Implement decoding logic for Base58
    }
}

return match (strtolower($base)) {
    'base62' => new Base62($config),
    'base58' => new Base58($config),  // Register Base58 here
    default => throw EncoderException::unsupportedBase($base),
};

use Nassiry\Encoder\Encoder;

$encoder = new Encoder('base58');

$encodedId = $encoder->encodeId(12345);
echo "Base58 Encoded ID: $encodedId\n";

$decodedId = $encoder->decodeId($encodedId);
echo "Decoded ID: $decodedId\n";

EncoderException::invalidId(); 
// Message: "The ID must be a non-negative integer."

EncoderException::invalidLength($length);
// Message: "The length must be greater than zero. Given: 0"

EncoderException::emptyInput(); 
// Message: "Input string cannot be empty."

EncoderException::invalidBaseString(string $char, string $base); 
// Message: "Invalid character '!' in 'base62' string."

EncoderException::invalidBase(string $base); 
// Message: "Encoder base 'base64' is not supported."

EncoderException::invalidMethod(string $method); 
// Message: "Method 'fooBar' does not exist on the base encoder."

EncoderException::invalidClass(string $className); 
// Message: "Class 'MyBaseEncoder' must implement BaseEncoderInterface."

use Nassiry\Encoder\Exceptions\EncoderException;

try {
    $encoded = $encoder->encodeId(-1); // Invalid ID
} catch (EncoderException $e) {
    echo "Error: " . $e->getMessage();
}
bash
php artisan vendor:publish --provider="Nassiry\Encoder\EncoderServiceProvider"