You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
+
5
+
## [1.0.1] - 2025-04-20
6
+
7
+
### Changed
8
+
- Updated README for better clarity
9
+
- Moved extended usage instructions to `/docs`
10
+
- Excluded unnecessary files from composer archive
The Laravel Encoder package provides a robust and secure way to `encode` and `decode`**IDs** & **Strings** using customizable Base encoding mechanisms (Base62). With support for variable-length encoding, mappers for added security, and seamless integration with Laravel, this package is ideal for obfuscating sensitive data or creating URL-safe identifiers.
17
-
### Why I Created This Package
18
-
In one of my Laravel projects, I needed a way to `encode` & `decode` some strings. Initially, I tried using Laravel's built-in `encrypt` and `decrypt` functions, but they returned very long strings, which were not ideal for my use case.
19
-
Next, I turned to Base64 encoding, but I still needed to make it URL-safe, which added complexity. After looking around, I realized that there wasn't a straightforward package that supported encoding both **IDs** and **strings** while providing a customizable approach and URL-safe encoding.
20
-
So, I decided to create this package. It started as a solution for my Laravel project, but I quickly realized its usefulness beyond that and made it a standalone package that can be used in any PHP project.
21
-
With this package, I aimed to provide a simple, secure, and customizable `encoding/decoding` mechanism with support for Base62 encoding (which is URL-safe), and the ability to easily add more bases like Base58, Base64, or even your own custom encoding schemes.
22
17
23
-
> **Warning**: This package is designed for encoding and obfuscation, not for encryption. It does not provide strong cryptographic security. Use it for non-sensitive data or as a lightweight obfuscation method.
18
+
> ⚠️ **Note:** This package is meant for obfuscation, not encryption. Do not use it for storing or transmitting sensitive data securely.
19
+
20
+
## Table Of Contents
21
+
1.[Features](#features)
22
+
2.[Requirements](#requirements)
23
+
3.[Installation](#installation)
24
+
4.[Quick Example](#quick-example)
25
+
-[Encode & Decode an ID](#encode--decode-an-id)
26
+
-[Encode & Decode a String](#encode--decode-a-string)
27
+
5.[Documentation](#documentation)
28
+
-[Laravel Integration](#laravel-integration)
29
+
-[Standalone PHP Usage](#standalone-php-usage)
30
+
-[Configuration](#configuration)
31
+
-[Custom Bases](#custom-bases)
32
+
-[Exception Handling](#exception-handling)
33
+
-[Testing & Contributing](#testing--contributing)
34
+
6.[Why I Built This](#why-i-built-this)
35
+
7.[Why Base62?](#why-base62)
36
+
8.[Why Choose This Package?](#conclusion-why-choose-this-package)
37
+
9.[Contributing](#contributing)
38
+
10.[Changelog](#changelog)
39
+
11.[License](#license)
40
+
24
41
### Features
25
-
-**Base Encoding**: Supports customizable bases, including Base62 and others.
-**Variable-Length Encoding**: Allows encoding with custom lengths for obfuscation.
27
44
-**Mapper-Based Obfuscation**: Adds an extra layer of security by using configurable prime numbers and mappers.
28
45
-**Customizable Configuration**: Publish the configuration file to override default mappers.
@@ -33,266 +50,83 @@ With this package, I aimed to provide a simple, secure, and customizable `encodi
33
50
### Requirements
34
51
- PHP 8.1 or higher
35
52
- Laravel 10 or higher (optional, for Laravel integration)
36
-
-`bcmath` PHP extension enabled
37
-
-`mbstring` PHP extension enabled for encoding and decoding multi-byte strings (Persian, Arabic, etc.)
38
-
39
-
### Installation
53
+
- PHP extensions: `bcmath`, `mbstring`
54
+
55
+
## Installation
40
56
**Step 1: Install via Composer**
41
57
```bash
42
58
composer require nassiry/laravel-encoder
43
59
```
44
-
### Laravel Integration
45
-
This package integrates seamlessly with Laravel, making it easy to encode or decode IDs and strings using the service container, dependency injection, or facades.
echo "Decoded String: $decodedString\n"; // Hello World
126
-
```
127
-
2.#### Custom Configuration
128
-
When using a custom configuration, ensure that the `$length` parameter in the `encodeId` function is **an index within the configuration array**. It must be smaller than the last index of the configuration array.
129
-
```php
130
-
$config = [
131
-
1 => '1',
132
-
41 => '59',
133
-
2377 => '1677',
134
-
147299 => '187507',
135
-
9132313 => '5952585',
136
-
];
95
+
#### Configuration
96
+
Customize encoding behavior, mappers, and defaults.
137
97
138
-
$encoder = new Encoder('base62', $config);
98
+
-[Configuration Guide](docs/configuration.md)
139
99
140
-
// $length refers to the index (0-based), not the key
Covers validation, decoding issues, and base errors.
219
106
220
-
$encodedId = $encoder->encodeId(12345);
221
-
echo "Base58 Encoded ID: $encodedId\n";
107
+
-[Exception Handling](docs/exceptions.md)
222
108
223
-
$decodedId = $encoder->decodeId($encodedId);
224
-
echo "Decoded ID: $decodedId\n";
225
-
```
226
-
### Handling Exceptions
227
-
The package throws meaningful exceptions for invalid input and other runtime issues, making it easier to debug and handle errors gracefully.
228
-
The `EncoderException` class extends PHP's `RuntimeException` and provides static methods for creating specific exceptions.
229
-
#### Common Exceptions
230
-
-**Invalid ID**: Thrown when an ID is not a valid non-negative integer.
231
-
-**Invalid Length**: Thrown when length is not a valid non-negative integer.
232
-
-**Empty Input**: Thrown when the input string is empty.
233
-
-**Invalid Base String**: Thrown when a string contains invalid characters for the base.
234
-
-**Invalid Base**: Thrown when a base (Base58 or Base64) is not registered in the Factory.
235
-
-**Invalid Method Call**: Thrown when an undefined method is called on a base encoder.
236
-
-**Invalid Class**: Thrown when a custom base class does not implement the required `BaseEncoderInterface`.
237
-
```php
238
-
EncoderException::invalidId();
239
-
// Message: "The ID must be a non-negative integer."
109
+
#### Testing & Contributing
110
+
Run the test suite or contribute improvements.
240
111
241
-
EncoderException::invalidLength($length);
242
-
// Message: "The length must be greater than zero. Given: 0"
112
+
-[Testing Guide](docs/testing.md)
243
113
244
-
EncoderException::emptyInput();
245
-
// Message: "Input string cannot be empty."
114
+
### Why I Built This
115
+
Laravel's `encryption` produced long `strings`, and Base64 needed extra steps to be URL-safe. I wanted a clean, simple way to encode IDs and strings short, URL-safe, and easy to configure.
// Message: "Class 'MyBaseEncoder' must implement BaseEncoderInterface."
258
-
```
259
-
#### Example: Catching Exceptions
260
-
You can wrap your encoding/decoding logic in a `try-catch` block to handle exceptions gracefully:
261
-
```php
262
-
use Nassiry\Encoder\Exceptions\EncoderException;
127
+
### Changelog
263
128
264
-
try {
265
-
$encoded = $encoder->encodeId(-1); // Invalid ID
266
-
} catch (EncoderException $e) {
267
-
echo "Error: " . $e->getMessage();
268
-
}
269
-
```
270
-
### Testing
271
-
To ensure the package functions as expected and meets all requirements, you can run the included tests. Follow the steps below to execute the test suite:
272
-
#### Prerequisites
273
-
1. Ensure you have all dependencies installed by running:
274
-
```bash
275
-
composer install
276
-
```
277
-
2. Verify that the required PHP extensions (`bcmath` and `mbstring`) are enabled.
278
-
#### Running Tests
279
-
Execute the following command to run the test suite:
280
-
```bash
281
-
composer test
282
-
```
283
-
#### Test Coverage
284
-
The package comes with extensive test coverage for encoding and decoding methods, exception handling, and configuration. This ensures robust behavior across various use cases.
285
-
#### Contributing to Tests
286
-
If you encounter a bug or add a new feature, consider writing or updating the tests. Use the following guidelines:
287
-
1. Add your test cases to the appropriate files in the `tests` directory.
288
-
2. Follow the `PSR-12` coding standards for consistency.
289
-
3. Run `composer test` again to verify that all tests pass before submitting a pull request.
290
-
### Why Base62?
291
-
[Why Base62?](base62.md)
292
-
### Conclusion: Why Choose This Package?
293
-
[Conclusion](conclusion.md)
294
-
### Contributing
295
-
Feel free to submit issues or pull requests to improve the package. Contributions are welcome!
129
+
See [CHANGELOG](CHANGELOG.md) for release details.
296
130
297
131
### License
298
132
This package is open-source software licensed under the [MIT license](LICENSE).
0 commit comments