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


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\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

php artisan vendor:publish --provider="Nassiry\Encoder\EncoderServiceProvider"

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
    }
}

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";