### Usage Example with Default Driver Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md Example of using the rule in a validator with the default driver. It encodes an integer and validates it. ```php use Illuminate\Support\Facades\Validator; use LaravelSqids\Facades\Sqids; use LaravelSqids\Validations\SqidsValidationRule; $encoded = Sqids::encodeInteger($id); $validator = Validator::make( ['endpoint' => $encoded], ['endpoint' => [new SqidsValidationRule]] ); if ($validator->passes()) { // Valid Sqid } ``` -------------------------------- ### unsqids usage examples Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Examples of using the unsqids helper to decode strings, including with a named driver. ```php $decoded = unsqids($encoded); // [1, 2, 3] $tracked = unsqids($tracked, 'tracking_code'); // [1234] ``` -------------------------------- ### Manual boot check with Testbench Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md PHP example showing how to explicitly provide the service provider in a Testbench test case. ```php use LaravelSqids\SqidsServiceProvider; // Normal usage: the provider is registered automatically via composer extra.laravel.providers. // In a test bench it is supplied explicitly, e.g.: class TestCase extends Orchestra\Testbench\TestCase { protected function getPackageProviders($app) { return [SqidsServiceProvider::class]; } } ``` -------------------------------- ### Constructor Usage Example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md Shows how to instantiate the rule with the default driver or a named driver. ```php use LaravelSqids\Validations\SqidsValidationRule; $ruleDefault = new SqidsValidationRule(); // default driver $ruleCustom = new SqidsValidationRule('custom_driver'); // named driver ``` -------------------------------- ### Direct construction example for SqidsAdapter Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Shows how to instantiate SqidsAdapter directly with named parameters. The example uses a custom alphabet, length 6, empty blocklist, and empty pad, then encodes [123] to produce a string like "Lqj8a0". ```php use LaravelSqids\SqidsAdapter; // Direct construction; all optional parameters are named. $adapter = new SqidsAdapter( alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', length: 6, blocklist: [], pad: '', ); echo $adapter->encode([123]); // e.g. "Lqj8a0" ``` -------------------------------- ### Usage Example with Custom Driver Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md Example of using the rule with a custom driver. Note that validation is driver-specific; a string from the default driver will fail against a different driver configuration. ```php use Illuminate\Support\Facades\Validator; use LaravelSqids\Facades\Sqids; use LaravelSqids\Validations\SqidsValidationRule; $encoded = Sqids::driver('custom_driver')->encodeInteger(123); $validator = Validator::make( ['code' => $encoded], ['code' => [new SqidsValidationRule('custom_driver')]] ); if ($validator->fails()) { // 'The code must be a valid Sqid.' } ``` -------------------------------- ### getDriverConfig() usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how to retrieve the configuration array for a specific driver. The example demonstrates that a missing driver returns null without throwing an exception. ```php $manager = $container->get(SqidsManager::class); $config = $manager->getDriverConfig('tracking_code'); // => ['pad' => '5000', 'length' => '6', 'alphabet' => '5364127908'] (from config/sqids.php) $missing = $manager->getDriverConfig('nope'); // null — no exception here ``` -------------------------------- ### Install Laravel Sqids via Composer Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Install the package using Composer. Requires bcmath or gmp extension for Sqids to work. ```bash composer require a-sabagh/laravel-sqids ``` -------------------------------- ### sqids usage examples Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Examples of using the sqids helper to encode numbers, including with a named driver. ```php $encoded = sqids([1, 2, 3]); // e.g. "Lqj8a0" $tracked = sqids([1234], 'tracking_code'); // using a named driver ``` -------------------------------- ### unsqidsInt usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Example of using the unsqidsInt helper to decode a single integer. ```php $singleDecoded = unsqidsInt($singleEncoded); // 123 ``` -------------------------------- ### driver() usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how to obtain the default driver adapter and a named driver adapter, and demonstrates that repeated calls for the same driver return the same cached instance. ```php use LaravelSqids\SqidsManager; use LaravelSqids\SqidsAdapter; $manager = $container->get(SqidsManager::class); $defaultDriver = $manager->driver(); // default driver adapter $tracking = $manager->driver('tracking_code'); // named driver adapter $cached = $manager->driver('tracking_code'); // same instance as $tracking ``` -------------------------------- ### unsqidsCollect usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Example of using the unsqidsCollect helper to decode a string into a Collection and filter it. ```php $collection = unsqidsCollect($encoded); // Collection([1, 2, 3]) $filtered = $collection->filter(fn ($n) => $n > 1); ``` -------------------------------- ### sqidsInt usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Example of using the sqidsInt helper to encode a single integer. ```php $singleEncoded = sqidsInt(123); // e.g. "M7k2b1" ``` -------------------------------- ### decodeCollect usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Example of decoding an encoded array into a Laravel Collection. The example uses the SqidsAdapter with the LaravelSqids namespace and demonstrates converting the collection to an array. ```php use LaravelSqids\SqidsAdapter; $adapter = new SqidsAdapter(); $collection = $adapter->decodeCollect($adapter->encode([1, 2, 3])); $array = $collection->toArray(); // [1, 2, 3] ``` -------------------------------- ### createDriver() usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how to create a new adapter instance for a named driver, bypassing the cache. Equivalent to driver('url_endpoint') but without caching. ```php $manager = $container->get(SqidsManager::class); // Equivalent to driver('url_endpoint') but bypasses the cache. $adapter = $manager->createDriver('url_endpoint'); ``` -------------------------------- ### encodeInteger usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Example of encoding a single integer using the SqidsAdapter. The result is a string like "M7k2b1". ```php $adapter = new SqidsAdapter(); $code = $adapter->encodeInteger(456); // e.g. "M7k2b1" ``` -------------------------------- ### __call usage examples: method and alias resolution Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how Sqids::encode([123]) resolves to the default driver's encode method, while Sqids::shortIds() and Sqids::default() return driver instances via alias resolution. ```php // Resolved as a real method of the default driver: Sqids::encode([123]); // __call('encode', [[123]]) → $driver->encode([123]) // Resolved as driver alias: $short = Sqids::shortIds()->encode([123]); // __call('shortIds', []) → driver('short_ids') $def = Sqids::default()->encode([123]); // __call('default', []) → driver('default') ``` -------------------------------- ### Default driver configuration in config/sqids.php Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Example configuration for the default driver. The 'pad', 'length', 'blocklist', and 'alphabet' values can be set via environment variables or overridden directly. ```php 'drivers' => [ 'default' => [ 'pad' => env('SQIDS_DEFAULT_PAD', ''), 'length' => env('SQIDS_DEFAULT_LENGTH', 6), 'blocklist' => env('SQIDS_DEFAULT_BLOCK_LIST', []), 'alphabet' => env('SQIDS_DEFAULT_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), ], ] ``` -------------------------------- ### driver() missing driver exception example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows the exception thrown when calling driver() with an undefined driver name. The catch block comments the exception message. ```php // Missing driver → InvalidArgumentException try { $manager->driver('pelikan-never-died'); } catch (InvalidArgumentException $e) { // "Sqids Driver [pelikan-never-died] is not defined in sqids configuration." } ``` -------------------------------- ### Error handling example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Shows how to catch an InvalidArgumentException when requesting a driver that is not defined in the sqids configuration. The exception message is 'Sqids Driver [not_configured] is not defined in sqids configuration.' ```php use LaravelSqids\Facades\Sqids; use InvalidArgumentException; try { $driver = Sqids::driver('not_configured'); } catch (InvalidArgumentException $e) { // 'Sqids Driver [not_configured] is not defined in sqids configuration.' logger('Bad driver', $e); } ``` -------------------------------- ### __call BadMethodCallException example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows the exception thrown when no method or driver alias matches. The message includes the method name, driver class, and snake_case alias. ```php // Nothing matches → BadMethodCallException try { Sqids::totallyUnknownThing(); } catch (BadMethodCallException $e) { // 'Method "totallyUnknownThing" not found on LaravelSqids.SqidsAdapter and no driver named "totally_unknown_thing" exists.' } ``` -------------------------------- ### decodeInteger usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Example of encoding an integer and then decoding it back. The decodeInteger method returns the original integer (456). ```php $adapter = new SqidsAdapter(); $id = 456; $code = $adapter->encodeInteger($id); $back = $adapter->decodeInteger($code); // 456 ``` -------------------------------- ### getDefaultDriver() usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how to retrieve the default driver name from the manager. The comment indicates the expected return value, which is 'default' when configured via sqids.default. ```php $manager = $container->get(SqidsManager::class); $default = $manager->getDefaultDriver(); // "default" (configured via sqids.default) ``` -------------------------------- ### Use custom driver via facade and camelCase alias Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Shows how to use a custom driver after adding it to the configuration. The driver can be accessed via Sqids::driver('short_ids') or via the camelCase alias Sqids::shortIds(). The example encodes and decodes an ID. ```php use LaravelSqids\Facades\Sqids; $encoded = Sqids::driver('short_ids')->encode([$id]); $decoded = Sqids::driver('short_ids')->decode($encoded); $shortId = Sqids::shortIds()->encode([$id]); // camelCase alias ``` -------------------------------- ### Sqids::getDriverConfig() - Get driver config Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Returns the config array for a driver (sqids.drivers.), or null when undefined. Backed by SqidsManager::getDriverConfig. ```php $cfg = Sqids::getDriverConfig('url_endpoint'); ``` -------------------------------- ### Access drivers via camelCase methods on the Sqids facade Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Once a driver is configured, you can call it directly using a camelCase method on the Sqids facade. For example, 'shortIds' for the 'short_ids' driver and 'default' for the default driver. ```php $shortId = Sqids::shortIds()->encode([$id]); $defaultId = Sqids::default()->encode([$id]); ``` -------------------------------- ### decode usage example Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Encodes a number with a padded adapter, then decodes it back to an integer array. The decoded value is [91234] with integer type, not string. ```php use LaravelSqids\SqidsAdapter; $adapter = new SqidsAdapter(pad: '5000', length: 6, alphabet: '5364127908'); $encoded = $adapter->encode([91234]); $decoded = $adapter->decode($encoded); // [91234] (int, not string) ``` -------------------------------- ### Usage example for encodedStringValid Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Example usage of encodedStringValid with an adapter configured with alphabet 'abcxyz', length 4, and pad 'x'. Shows true for a valid string and false for strings with invalid characters, wrong pad, or insufficient length. ```php use LaravelSqids\SqidsAdapter; $adapter = new SqidsAdapter(alphabet: 'abcxyz', length: 4, pad: 'x'); $adapter->encodedStringValid('xabcz'); // true — pad ok, length ok, chars ok $adapter->encodedStringValid('xabc1'); // false — '1' not in alphabet $adapter->encodedStringValid('yabcz'); // false — does not start with pad $adapter->encodedStringValid('xabc'); // false — below length + pad minimum (5) $adapter->encodedStringValid('x'); // false — too short and empty body ``` -------------------------------- ### Sqids::getDefaultDriver() - Get default driver name Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Returns the configured default driver name (sqids.default), fallback 'default'. Backed by SqidsManager::getDefaultDriver. ```php $name = Sqids::getDefaultDriver(); // "default" ``` -------------------------------- ### encode usage with pad and alphabet Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Creates a SqidsAdapter with a pad of '5000', length 6, and a custom alphabet, then encodes a single number. The result is a string starting with '5000' followed by the encoded body, all digits. ```php use LaravelSqids\SqidsAdapter; $adapter = new SqidsAdapter(pad: '5000', length: 6, alphabet: '5364127908'); $tracking = $adapter->encode([91234]); // "5000" + encoded body, all digits echo $tracking; ``` -------------------------------- ### Catch InvalidArgumentException for null driver Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/errors.md Example of catching the InvalidArgumentException thrown when the default driver is unresolved. The exception message is 'Unable to resolve NULL Sqids driver for [...]'. ```php use LaravelSqids\Facades\Sqids; use InvalidArgumentException; try { Sqids::driver(); // default driver unresolved → null } catch (InvalidArgumentException $e) { // 'Unable to resolve NULL Sqids driver for [...]' } ``` -------------------------------- ### Catch BadMethodCallException for unresolvable magic call Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/errors.md Example of catching the BadMethodCallException thrown when calling a non-existent method like flyToTheMoon(). The exception message is 'Method "flyToTheMoon" not found on LaravelSqids.SqidsAdapter and no driver named "fly_to_the_moon" exists.' ```php use LaravelSqids\Facades\Sqids; use BadMethodCallException; try { Sqids::flyToTheMoon(); } catch (BadMethodCallException $e) { // 'Method "flyToTheMoon" not found on LaravelSqids.SqidsAdapter and no driver named "fly_to_the_moon" exists.' } ``` -------------------------------- ### boot() method signature Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md PHP method signature for the boot() method, which publishes config when running in console. ```php public function boot(): void ``` -------------------------------- ### Direct construction of SqidsManager Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Shows how to directly construct a SqidsManager instance with a container. Normally the service provider constructs it as a singleton. ```php use Illuminate\Contracts\Container\Container; use LaravelSqids\SqidsManager; // Normally constructed by the service provider: // app->singleton(SqidsManager::class, fn (app) => new SqidsManager(app)) // Direct construction: $manager = new SqidsManager($container); ``` -------------------------------- ### Decode and Get a Laravel Collection Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Use decodeCollect to get a Laravel Collection from an encoded string, enabling easier chaining with Laravel collections. ```php $decodeCollection = Sqids::decodeCollect($encodeString); ``` -------------------------------- ### Publishing config in boot() Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md PHP code that publishes the package config to the application's config directory with the 'sqids' tag. ```php $this->publishes([ __DIR__.'/../config/sqids.php' => config_path('sqids.php'), ], 'sqids'); ``` -------------------------------- ### Constructor Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Creates a new SqidsAdapter instance. All parameters are optional and named. The pad is a static prefix prepended to every encoded string and stripped on decode. ```APIDOC ## Constructor ### Description Creates a new SqidsAdapter instance. The constructor forwards alphabet, length (as minLength), and blocklist to the parent Sqids class; the pad is handled entirely by this adapter. ### Method Constructor ### Signature ```php public function __construct( protected string $alphabet = self::DEFAULT_ALPHABET, protected int $length = self::DEFAULT_MIN_LENGTH, protected array $blocklist = self::DEFAULT_BLOCKLIST, protected string $pad = self::DEFAULT_PAD, ) ``` ### Parameters - **alphabet** (string) - Optional - Default: `self::DEFAULT_ALPHABET` (inherited constant, value defined in sqids-php) - Character set used by the encoder. Must satisfy sqids-php constraints (≥ 3 unique chars, no consecutive space chars). - **length** (int) - Optional - Default: `self::DEFAULT_MIN_LENGTH` (inherited constant, value defined in sqids-php) - Minimum output length of the encoded body (pad is additional). - **blocklist** (array) - Optional - Default: `self::DEFAULT_BLOCKLIST` (inherited constant, value defined in sqids-php) - List of words that must never appear in encoded output. - **pad** (string) - Optional - Default: `self::DEFAULT_PAD` (`''` - `final public const DEFAULT_PAD`, `src/SqidsAdapter.php:10`) - Static prefix prepended to every encoded string and stripped before decode. ### Example ```php use LaravelSqids\SqidsAdapter; // Direct construction; all optional parameters are named. $adapter = new SqidsAdapter( alphabet: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', length: 6, blocklist: [], pad: '', ); echo $adapter->encode([123]); // e.g. "Lqj8a0" ``` ``` -------------------------------- ### Using Sqids Helper Functions for Encoding and Decoding Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Demonstrates the usage of the sqids, unsqids, sqidsInt, unsqidsInt, and unsqidsCollect helper functions. Shows encoding multiple numbers, decoding back to an array, encoding a single integer, decoding it, and decoding into a Collection. ```php // Encode multiple numbers $encoded = sqids([1, 2, 3]); // e.g. "Lqj8a0" // Decode back $decoded = unsqids($encoded); // [1, 2, 3] // Encode a single integer $singleEncoded = sqidsInt(123); // e.g. "M7k2b1" // Decode single integer $singleDecoded = unsqidsInt($singleEncoded); // 123 // Decode into a Collection $collection = unsqidsCollect($encoded); // Collection([1, 2, 3]) ``` -------------------------------- ### Add a custom driver with its own configuration Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Shows how to add a 'short_ids' driver with custom settings. The blocklist excludes ambiguous characters like '0', 'O', 'I', and '1'. ```php 'drivers' => [ 'default' => [ /* default config */ ], 'short_ids' => [ 'pad' => '0', 'length' => 4, 'blocklist' => ['0', 'O', 'I', '1'], 'alphabet' => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789', ], ], ``` -------------------------------- ### register() method signature Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md PHP method signature for the register() method, which merges config and binds the manager singleton. ```php public function register(): void ``` -------------------------------- ### Constructor signature for SqidsAdapter Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md The constructor accepts alphabet, length, blocklist, and pad parameters, all optional with inherited defaults. It forwards alphabet, length (as minLength), and blocklist to the parent constructor; pad is handled by the adapter. ```php public function __construct( protected string $alphabet = self::DEFAULT_ALPHABET, protected int $length = self::DEFAULT_MIN_LENGTH, protected array $blocklist = self::DEFAULT_BLOCKLIST, protected string $pad = self::DEFAULT_PAD, ) { parent::__construct( alphabet: $this->alphabet, minLength: $this->length, blocklist: $this->blocklist ); } ``` -------------------------------- ### Create driver in SqidsManager::createDriver() Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Instantiates a new SqidsAdapter by spreading the driver configuration array as named arguments. Unknown keys are ignored by the constructor. ```php return new SqidsAdapter(...$config); ``` -------------------------------- ### Using SqidsValidationRule in Laravel Validator Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Validates that an input is a valid Sqid string using the SqidsValidationRule. The example encodes an integer to a Sqid, then validates it in a Validator instance, checking if it passes. ```php $encoded = Sqids::encodeInteger($id); $validator = Validator::make( ['endpoint' => $encoded], ['endpoint' => [new SqidsValidationRule]] ); if ($validator->passes()) { // Valid Sqid } ``` -------------------------------- ### Catch InvalidArgumentException for undefined driver Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/errors.md Example of catching the InvalidArgumentException thrown when calling driver('drivers_license') which is not defined in the configuration. The exception message is 'Sqids Driver [drivers_license] is not defined in sqids configuration.' ```php use LaravelSqids\Facades\Sqids; use InvalidArgumentException; try { Sqids::driver('drivers_license'); } catch (InvalidArgumentException $e) { // 'Sqids Driver [drivers_license] is not defined in sqids configuration.' } ``` -------------------------------- ### Constructor: new SqidsManager(Container $container) Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Creates a new SqidsManager instance. The container is used to resolve the config repository, which is stored for later use. The driver cache is initialized as an empty array. ```APIDOC ## Constructor: new SqidsManager(Container $container) ### Description Creates a new SqidsManager instance. The container is used to resolve the config repository, which is stored for later use. The driver cache is initialized as an empty array. ### Method Constructor ### Signature ```php public function __construct(Container $container) ``` ### Parameters - **container** (`Illuminate\Contracts\Container\Container`) - Required - The application container. Used to resolve the `config` repository (`$container->make('config')`), stored on `$this->container`. ### Return None (constructor). The manager also initializes a protected `$config` property (the `config` container binding) and `protected $drivers = []` used as the driver instance cache. ### Example ```php use Illuminate\Contracts\Container\Container; use LaravelSqids\SqidsManager; // Normally constructed by the service provider: // app->singleton(SqidsManager::class, fn (app) => new SqidsManager(app)) // Direct construction: $manager = new SqidsManager($container); ``` ``` -------------------------------- ### Composer autoload configuration for helpers Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/helpers.md Registers the helpers file for autoloading via composer.json's autoload-dev files array. Requires running composer dump-autoload after adding. ```json "autoload-dev": { "psr-4": { "LaravelSqids\\Tests\\": "tests/" }, "files": [ "src/helpers.php" ] } ``` -------------------------------- ### __call(string $method, array $parameters): mixed Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Magic-dispatch entry point that makes the facade and manager dynamically respond to unknown methods. Resolves method calls to the default driver, driver aliases, or throws an exception. ```APIDOC ## __call(string $method, array $parameters): mixed ### Description The magic-dispatch entry point that makes the facade and manager dynamically respond to unknown methods. It resolves method calls to the default driver, driver aliases, or throws an exception. ### Method `__call` ### Signature ```php public function __call(string $method, array $parameters): mixed ``` ### Parameters - **method** (string) - Required - The unresolved method name (e.g. `'encode'`, `'trackingCode'`). - **parameters** (array) - Required - Arguments passed to the dynamic call. ### Return `mixed` — either the adapter method result (string/array/int/Collection/bool) or a `SqidsAdapter` driver instance. ### Throws - `BadMethodCallException` — when no matching method or driver alias is found. ### Logic 1. Resolve the default driver via `$this->driver()`. 2. If `method_exists($driver, $method)` → call `$driver->$method(...$parameters)` and return the result. 3. Otherwise compute `$alias = Str::snake($method)` (camelCase → snake_case, e.g. `trackingCode` → `tracking_code`). If `driverExists($alias)` → return `$this->driver($alias)` — the returned adapter is used directly (this is how `Sqids::trackingCode()` works). 4. Otherwise throw `BadMethodCallException` with message `'Method "%s" not found on %s and no driver named "%s" exists.'` interpolating `$method`, the driver's class name (or string cast), and `$alias`. ### Usage Examples ```php // Resolved as a real method of the default driver: Sqids::encode([123]); // __call('encode', [[123]]) → $driver->encode([123]) // Resolved as driver alias: $short = Sqids::shortIds()->encode([123]); // __call('shortIds', []) → driver('short_ids') $def = Sqids::default()->encode([123]); // __call('default', []) → driver('default') ``` ```php // Nothing matches → BadMethodCallException try { Sqids::totallyUnknownThing(); } catch (BadMethodCallException $e) { // 'Method "totallyUnknownThing" not found on LaravelSqids.SqidsAdapter and no driver named "totally_unknown_thing" exists.' } ``` ``` -------------------------------- ### Shipped driver definitions in config/sqids.php Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Verbatim shipped content of config/sqids.php defining the 'default', 'tracking_code', and 'url_endpoint' drivers. Note that 'default' has commented-out blocklist and alphabet keys, and length values are integers for 'default' but strings for the others. ```php 'drivers' => [ 'default' => [ 'pad' => env('SQIDS_DEFAULT_PAD', ''), 'length' => env('SQIDS_DEFAULT_LENGTH', 6), // 'blocklist' => env('SQIDS_DEFAULT_BLOCK_LIST', []), // 'alphabet' => env('SQIDS_DEFAULT_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), ], 'tracking_code' => [ 'pad' => env('SQIDS_TRACKING_PAD', '5000'), 'length' => env('SQIDS_TRACKING_LENGTH', '6'), 'alphabet' => env('SQIDS_TRACKING_ALPHABET', '5364127908'), ], 'url_endpoint' => [ 'pad' => env('SQIDS_URL_ENDPOINT_PAD', ''), 'length' => env('SQIDS_URL_ENDPOINT_LENGTH', '8'), 'alphabet' => env('SQIDS_URL_ENDPOINT_ALPHABET', '6loKCI3XaO4dqT8sBLGPbpHVu7zgSjnrvkYEhZA2McxeDJyF5RQ9iWUfw1tN0m'), ], ], ``` -------------------------------- ### Publish Sqids config file with artisan vendor:publish Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Run this Artisan command to publish the sqids configuration file to your application so you can customize settings like alphabet, minimum length, and blocklist. ```bash php artisan vendor:publish --tag=sqids ``` -------------------------------- ### encodedStringValid(string $encodedString): bool Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Checks if the provided string is a valid encoded ID. Returns true only if the string's characters (after stripping the pad prefix) are within the configured alphabet, the total length meets the minimum (length + pad length), and the string starts with the configured pad. ```APIDOC ## encodedStringValid(string $encodedString): bool ### Description Checks if the provided string is a valid encoded ID. Returns true only if the string's characters (after stripping the pad prefix) are within the configured alphabet, the total length meets the minimum (length + pad length), and the string starts with the configured pad. ### Method encodedStringValid ### Signature public function encodedStringValid(string $encodedString): bool ### Parameters - **encodedString** (string) - Required - Arbitrary string to check. ### Return Value - **bool** - `true` only if all three private checks pass: 1. `encodedStringAlphabetFairlyValid($encodedString)` — the substring after stripping `strlen($pad)` leading characters contains only characters present in the configured `alphabet` (`array_diff` of input chars vs. alphabet chars is empty). Note this check does **not** verify that the string actually starts with the pad — it just skips the first `strlen($pad)` characters. 2. `encodedStringMinLengthDefinitiveValid($encodedString)` — `strlen($encodedString) >= ($this->length + strlen($this->pad))` (pad length counts toward the total minimum). 3. `encodedStringPadDefinitiveValid($encodedString)` — `str_starts_with($encodedString, $this->pad)` (always true when `pad` is `''`). ### Usage Example ```php use LaravelSqids\SqidsAdapter; $adapter = new SqidsAdapter(alphabet: 'abcxyz', length: 4, pad: 'x'); $adapter->encodedStringValid('xabcz'); // true — pad ok, length ok, chars ok $adapter->encodedStringValid('xabc1'); // false — '1' not in alphabet $adapter->encodedStringValid('yabcz'); // false — does not start with pad $adapter->encodedStringValid('xabc'); // false — below length + pad minimum (5) $adapter->encodedStringValid('x'); // false — too short and empty body ``` ``` -------------------------------- ### Default driver configuration key Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Sets the default driver name, read from the SQIDS_DEFAULT environment variable with fallback 'default'. Used when no explicit driver is requested. ```php 'default' => env('SQIDS_DEFAULT', 'default'), ``` -------------------------------- ### Driver alias magic calls Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Any driver name registered in the `sqids.drivers` config can be reached as a camelCase static method. The manager converts it with `Str::snake()` and returns the driver adapter, which can be used directly to chain `.encode()`, `.decode()`, etc. ```APIDOC ## Sqids::trackingCode() ### Description Access the 'tracking_code' driver via the facade. Returns the driver adapter for direct use. ### Method Static method call on the Sqids facade. ### Endpoint `Sqids::trackingCode()` ### Parameters None. ### Return Value Returns the driver adapter for the 'tracking_code' driver. ### Usage Example ```php $short = Sqids::trackingCode()->encode([$id]); // numeric-alphabet, pad '5000' $back = Sqids::trackingCode()->decode($short); ``` ## Sqids::urlEndpoint() ### Description Access the 'url_endpoint' driver via the facade. Returns the driver adapter for direct use. ### Method Static method call on the Sqids facade. ### Endpoint `Sqids::urlEndpoint()` ### Parameters None. ### Return Value Returns the driver adapter for the 'url_endpoint' driver. ### Usage Example ```php $urlFriendly = Sqids::urlEndpoint()->encode([$id]); ``` ## Sqids::customDriver() ### Description Access the 'custom_driver' driver via the facade. Returns the driver adapter for direct use. ### Method Static method call on the Sqids facade. ### Endpoint `Sqids::customDriver()` ### Parameters None. ### Return Value Returns the driver adapter for the 'custom_driver' driver. ### Usage Example ```php Sqids::customDriver()->encode([$id]); ``` ## Sqids::default() ### Description Access the 'default' driver via the facade. Returns the driver adapter for direct use. ### Method Static method call on the Sqids facade. ### Endpoint `Sqids::default()` ### Parameters None. ### Return Value Returns the driver adapter for the 'default' driver. ### Usage Example ```php Sqids::default()->encode([$id]); ``` ## Error Handling ### Description If a call matches neither a default-driver method nor a section of a configured driver, the manager throws `BadMethodCallException`. Additionally, calling `Sqids::driver('not_configured')` with an unconfigured driver name throws `InvalidArgumentException`. ### Usage Example ```php use LaravelSqids\Facades\Sqids; use InvalidArgumentException; try { $driver = Sqids::driver('not_configured'); } catch (InvalidArgumentException $e) { // 'Sqids Driver [not_configured] is not defined in sqids configuration.' logger('Bad driver', $e); } ``` ``` -------------------------------- ### Sqids::createDriver() - Create fresh driver adapter Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Builds a fresh SqidsAdapter from a driver's config, bypassing the cache. Throws InvalidArgumentException when the driver is not defined. Backed by SqidsManager::createDriver. ```php $fresh = Sqids::createDriver('url_endpoint'); ``` -------------------------------- ### driver() method signature Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Signature of the driver() method as it appears in the source file. It accepts an optional driver name and returns an object. ```php public function driver(?string $driver = null): object ``` -------------------------------- ### Constructor Signature for SqidsValidationRule Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md The constructor accepts an optional driver name. When null, the default driver is used. ```php public function __construct( public ?string $driver = null ) {} ``` -------------------------------- ### __call method signature Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Public magic method signature for dynamic dispatch. Resolves the default driver, calls real methods, or returns a driver instance for aliases. Throws BadMethodCallException when nothing matches. ```php public function __call(string $method, array $parameters): mixed ``` -------------------------------- ### driver(?string $driver = null): object Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Returns a LaravelSqids\SqidsAdapter instance for the named driver. When the driver is null, it resolves to the default driver. Instances are cached per driver name. ```APIDOC ## driver(?string $driver = null): object ### Description Returns a LaravelSqids\SqidsAdapter instance for the named driver. When the driver is null, it resolves to the default driver. Instances are cached per driver name. ### Method `driver` ### Signature ```php public function driver(?string $driver = null): object ``` ### Parameters - **driver** (?string) - Optional - Driver name. When null, resolves to `getDefaultDriver()`. ### Return - **object** - A `LaravelSqids\SqidsAdapter` instance for the named driver. Instances are cached per driver name in the protected `$drivers` array: the first call builds the adapter, subsequent calls return the cached instance. ### Throws - **InvalidArgumentException** - When the resolved driver name itself is null. Message: `'Unable to resolve NULL Sqids driver for [%s].'` with `static::class` interpolated. ### Usage Examples ```php use LaravelSqids\SqidsManager; use LaravelSqids\SqidsAdapter; $manager = $container->get(SqidsManager::class); $defaultDriver = $manager->driver(); // default driver adapter $tracking = $manager->driver('tracking_code'); // named driver adapter $cached = $manager->driver('tracking_code'); // same instance as $tracking ``` ```php // Missing driver → InvalidArgumentException try { $manager->driver('pelikan-never-died'); } catch (InvalidArgumentException $e) { // "Sqids Driver [pelikan-never-died] is not defined in sqids configuration." } ``` ``` -------------------------------- ### Runtime config mutation for custom driver Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Sets a custom driver configuration via the container config and then resolves it using the Sqids facade. The driver is cached per name after first resolution, so config changes only take effect before first use. ```php $this->app->config->set('sqids.drivers.custom_driver', [ 'alphabet' => '123456789', 'length' => 12, 'pad' => '5000', ]); Sqids::customDriver()->encode([123]); // now resolvable Sqids::driver('custom_driver')->encode([123]); ``` -------------------------------- ### Use a specific driver for encoding and decoding Source: https://github.com/a-sabagh/laravel-sqids/blob/main/README.md Use the 'driver' method on the Sqids facade to encode and decode with a named driver. The driver must be registered in config/sqids.php. ```php $encoded = Sqids::driver('short_ids')->encode([$id]); $decoded = Sqids::driver('short_ids')->decode($encoded); ``` -------------------------------- ### Import SqidsValidationRule Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md Import the SqidsValidationRule class from the LaravelSqids\Validations namespace. ```php use LaravelSqids\Validations\SqidsValidationRule; ``` -------------------------------- ### SqidsManager::__call() magic method resolution Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/errors.md Source code from src/SqidsManager.php:73-93. Throws BadMethodCallException when the invoked method is not a method of the default driver and its snake_case alias is not a configured driver. ```php public function __call(string $method, array $parameters): mixed { $driver = $this->driver(); if (method_exists($driver, $method)) { return $driver->$method(...$parameters); } $alias = Str::snake($method); if ($this->driverExists($alias)) { return $this->driver($alias); } throw new BadMethodCallException(sprintf( 'Method "%s" not found on %s and no driver named "%s" exists.', $method, is_object($driver) ? get_class($driver) : (string) $driver, $alias )); } ``` -------------------------------- ### Constructor signature for SqidsManager Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Constructor signature for SqidsManager, accepting a Container instance. The container is used to resolve the config repository. ```php public function __construct(Container $container) ``` -------------------------------- ### Sqids::driver() - Resolve driver adapter Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Resolves and caches a driver adapter. When null, uses the default driver. Throws InvalidArgumentException for undefined drivers. Backed by SqidsManager::driver. ```php $tracking = Sqids::driver('tracking_code'); $tracking->encodeInteger(12345); // "5000…" (pad-prefixed, numeric alphabet) ``` -------------------------------- ### Class declaration for SqidsAdapter Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-adapter.md Shows the class declaration extending Sqids with a final constant DEFAULT_PAD. The constants DEFAULT_ALPHABET, DEFAULT_MIN_LENGTH, and DEFAULT_BLOCKLIST are inherited from the parent class. ```php class SqidsAdapter extends Sqids { final public const DEFAULT_PAD = ''; ``` -------------------------------- ### Composer auto-discovery for SqidsServiceProvider Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md JSON configuration in composer.json that enables Laravel auto-discovery of the service provider. ```json "extra": { "laravel": { "providers": [ "LaravelSqids\\SqidsServiceProvider" ] } } ``` -------------------------------- ### Import LaravelSqids Facade Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md The import statement used to reference the Sqids facade in PHP files. ```php use LaravelSqids\Facades\Sqids; ``` -------------------------------- ### Merge config in SqidsServiceProvider::register() Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/configuration.md Merges the package config file under the 'sqids' key so consumers can override it. Called in SqidsServiceProvider::register(). ```php $this->mergeConfigFrom(__DIR__.'/../config/sqids.php', 'sqids'); ``` -------------------------------- ### Driver alias magic calls Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md Any driver registered in the sqids.drivers config can be called as a camelCase static method. The manager converts it with Str::snake() and returns the driver adapter, which can be used directly for encoding and decoding. ```php // Driver 'tracking_code' → method trackingCode() $short = Sqids::trackingCode()->encode([$id]); // numeric-alphabet, pad '5000' $back = Sqids::trackingCode()->decode($short); // Driver 'url_endpoint' → method urlEndpoint() $urlFriendly = Sqids::urlEndpoint()->encode([$id]); // Driver 'custom_driver' → method customDriver() Sqids::customDriver()->encode([$id]); // Driver 'default' → method default() Sqids::default()->encode([$id]); ``` -------------------------------- ### SqidsManager::createDriver() undefined driver check Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/errors.md Source code from src/SqidsManager.php:55-62. Throws InvalidArgumentException when the requested driver has no config entry in sqids.drivers.. ```php public function createDriver($driver): object { $config = $this->getDriverConfig($driver); if (is_null($config)) { throw new InvalidArgumentException(sprintf( 'Sqids Driver [%s] is not defined in sqids configuration.', $driver )); } return new SqidsAdapter(...$config); } ``` -------------------------------- ### SqidsValidationRule::__construct Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-validation-rule.md Creates a new SqidsValidationRule instance with an optional driver name. When null, the default driver is used. ```APIDOC ## Constructor ### Description Creates a new SqidsValidationRule instance. The driver parameter specifies which driver to use for validation; when null, the default driver is used. ### Method Constructor ### Signature ```php public function __construct( public ?string $driver = null ) {} ``` ### Parameters - **driver** (?string) - Optional - Driver name used for validation. When `null`, the default driver (`sqids.default`) is used. Stored as a public property `$this->driver`. ### Example ```php use LaravelSqids\Validations\SqidsValidationRule; $ruleDefault = new SqidsValidationRule(); // default driver $ruleCustom = new SqidsValidationRule('custom_driver'); // named driver ``` ``` -------------------------------- ### getDriverConfig(?string $driver = null): mixed Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Returns the driver configuration array for a given driver name, or null if not configured. When the driver is null, it resolves to the default driver. ```APIDOC ## getDriverConfig(?string $driver = null): mixed ### Description Returns the driver configuration array stored at `sqids.drivers.`, or `null` when the driver key is not configured. The returned array is spread into the `SqidsAdapter` constructor by `createDriver()`. ### Method `getDriverConfig` ### Signature ```php public function getDriverConfig(?string $driver = null): mixed ``` ### Parameters - **driver** (?string) - Optional - Driver name. When `null`, resolves to `getDefaultDriver()`. ### Return Value - **Type**: `mixed` - **Description**: The driver config array, or `null` if the driver key is not configured. ### Usage Example ```php $manager = $container->get(SqidsManager::class); $config = $manager->getDriverConfig('tracking_code'); // => ['pad' => '5000', 'length' => '6', 'alphabet' => '5364127908'] (from config/sqids.php) $missing = $manager->getDriverConfig('nope'); // null — no exception here ``` ``` -------------------------------- ### SqidsServiceProvider class declaration Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-service-provider.md PHP class declaration for the service provider, extending Illuminate\Support\ServiceProvider. ```php class SqidsServiceProvider extends ServiceProvider ``` -------------------------------- ### getFacadeAccessor Method Signature Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/facade-sqids.md The protected static method that returns the manager class name, binding the facade to the manager. ```php protected static function getFacadeAccessor(): string ``` -------------------------------- ### createDriver($driver): object Source: https://github.com/a-sabagh/laravel-sqids/blob/main/_autodocs/api-reference/sqids-manager.md Creates a new SqidsAdapter instance for the given driver name, bypassing the cache. The driver's configuration is spread as named constructor arguments. ```APIDOC ## createDriver($driver): object ### Description Creates a new SqidsAdapter instance for the given driver name, bypassing the cache. The driver's configuration is spread as named constructor arguments. ### Method `createDriver` ### Signature ```php public function createDriver($driver): object ``` ### Parameters - **driver** (mixed) - Required - Driver name used for config lookup. In practice, a string. ### Return - **object** - `new SqidsAdapter(...$config)` where `$config = $this->getDriverConfig($driver)`; the config array is spread as named constructor arguments (`alphabet`, `length`, `blocklist`, `pad`). ### Throws - **InvalidArgumentException** - When the driver's config is null (i.e. not defined). Message: `'Sqids Driver [%s] is not defined in sqids configuration.'` ### Usage Example ```php $manager = $container->get(SqidsManager::class); // Equivalent to driver('url_endpoint') but bypasses the cache. $adapter = $manager->createDriver('url_endpoint'); ``` ```