Skip to content

Commit 5513735

Browse files
committed
chore(docs): restructure documentation and exclude from composer export
1 parent a6075bc commit 5513735

File tree

13 files changed

+673
-235
lines changed

13 files changed

+673
-235
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/assets export-ignore
2+
/docs export-ignore
3+
/tests export-ignore
4+
/.github export-ignore
5+
/.gitignore export-ignore
6+
/phpunit.xml export-ignore
7+
/CHANGELOG.md export-ignore

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
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
11+
12+
## [1.0.0] - 2024-12-28
13+
### Added
14+
- Initial stable release

README.md

Lines changed: 69 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,32 @@
1414
</div>
1515

1616
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.
2217

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+
2441
### Features
25-
- **Base Encoding**: Supports customizable bases, including Base62 and others.
42+
- **Base Encoding**: Supports customizable bases (Base62).
2643
- **Variable-Length Encoding**: Allows encoding with custom lengths for obfuscation.
2744
- **Mapper-Based Obfuscation**: Adds an extra layer of security by using configurable prime numbers and mappers.
2845
- **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
3350
### Requirements
3451
- PHP 8.1 or higher
3552
- 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
4056
**Step 1: Install via Composer**
4157
```bash
4258
composer require nassiry/laravel-encoder
4359
```
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.
46-
#### Using the Service Container
47-
48-
```php
49-
$encoder = app('Encoder');
50-
51-
// Encoding and Decoding IDs
52-
$encodedId = $encoder->encodeId(12345);
53-
$decodedId = $encoder->decodeId($encodedId);
5460

55-
// Encoding and Decoding Strings
56-
$encodedString = $encoder->encodeString('Hello World');
57-
$decodedString = $encoder->decodeString($encodedString);
58-
```
59-
#### Using Dependency Injection
60-
For better maintainability and testability, inject the encoder into your controllers or services:
61-
```php
62-
use Nassiry\Encoder\Encoder;
63-
64-
class MyController extends Controller
65-
{
66-
public function __construct(protected Encoder $encoder)
67-
{
68-
// Your other codes
69-
}
70-
71-
public function encodeData()
72-
{
73-
$encoded = $this->encoder->encodeString('my data');
74-
return response()->json(['encoded' => $encoded]);
75-
}
76-
}
77-
```
78-
#### Using the Facade
79-
The package provides a facade for quick access to encoder methods:
61+
### Quick Example
62+
- #### Encode & Decode an ID
8063
```php
8164
use Nassiry\Encoder\Facades\Encoder;
8265

83-
// Encoding and Decoding IDs
84-
$encodedId = Encoder::encodeId(12345);
85-
$decodedId = Encoder::decodeId($encodedId);
66+
$encoded = Encoder::encodeId(12345);
67+
$decoded = Encoder::decodeId($encoded);
8668

87-
// Encoding and Decoding Strings
88-
$encodedString = Encoder::encodeString('Hello World');
89-
$decodedString = Encoder::decodeString($encodedString);
69+
echo $encoded; // Encoded Strings
70+
echo $decoded; // 12345
9071
```
9172

92-
### Standalone Usage
93-
To use the package in a non-Laravel PHP project, follow these steps:
94-
1. #### Usage Example
73+
- #### Encode & Decode a String
9574
```php
96-
require __DIR__ . '/vendor/autoload.php';
75+
$encoded = Encoder::encodeString('Hello World');
76+
$decoded = Encoder::decodeString($encoded);
9777

98-
use Nassiry\Encoder\Encoder;
78+
echo $encoded; // Encoded Strings
79+
echo $decoded; // Hello World
80+
```
9981

100-
// Create an Encoder instance
101-
$encoder = new Encoder();
82+
### Documentation
10283

103-
// Encoding an ID
104-
$encodedId = $encoder->encodeId(12345);
105-
echo "Encoded ID: $encodedId\n"; // eqwb
84+
#### Laravel Integration
10685

107-
// Decoding an ID
108-
$decodedId = $encoder->decodeId($encodedId);
109-
echo "Decoded ID: $decodedId\n"; // 12345
86+
`Encode/decode` `strings` and `IDs` using Laravel container, facades, or dependency injection.
11087

111-
// Encoding an ID with length
112-
$encodedId = $encoder->encodeId(12345, 8);
113-
echo "Encoded ID: $encodedId\n"; // d29Buhe7
88+
- [Laravel Usage](docs/laravel.md)
11489

115-
// Decoding an ID
116-
$decodedId = $encoder->decodeId($encodedId);
117-
echo "Decoded ID: $decodedId\n"; // 12345
90+
#### Standalone PHP Usage
91+
Use in any PHP project, no Laravel required.
11892

119-
// Encoding a String
120-
$encodedString = $encoder->encodeString('Hello World');
121-
echo "Encoded String: $encodedString\n"; // 73XpUgyMwkGr29M
93+
- [Standalone Guide](docs/standalone.md)
12294

123-
// Decoding a String
124-
$decodedString = $encoder->decodeString($encodedString);
125-
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.
13797

138-
$encoder = new Encoder('base62', $config);
98+
- [Configuration Guide](docs/configuration.md)
13999

140-
// $length refers to the index (0-based), not the key
141-
$customEncodedId = $encoder->encodeId(12345, 3);
100+
#### Custom Bases
101+
Add your own Base58, Base64, or custom encoders.
102+
- [Custom Bases Guide](docs/custom-bases.md)
142103

143-
echo "Custom Encoded ID: $customEncodedId\n"; // qVX
144-
```
145-
#### Important Notes:
146-
1. The `$length` parameter represents the **index** in the configuration array, **not the key value**.
147-
2. It must always be smaller than the highest index of the configuration array.
148-
3. For example, in the above `$config`, the valid values for `$length` are `0`, `1`, `2`, `3`, or `4` (total of 5 elements, indices `0–4`).
149-
### Encoder Configuration File
150-
**Note:** This configuration file is applicable only when using the package with a **Laravel application**.
151-
For standalone usage, you need to provide configuration directly while initializing the `Encoder` class.
152-
This file allows you to set up the default base encoder and customize its behavior within Laravel.
153-
By default, the package uses the `base62` encoder, but you can define additional configurations
154-
for Base62 or other custom Base classes that you may add via the Factory.
155-
### Default Base62 Configuration
156-
If you are using the default Base62 implementation, you can override its default mappers
157-
by specifying custom mappings in the `config` array below. These mappers are used to obfuscate
158-
the encoding process by applying prime numbers for variable-length encoding.
159-
Example for overriding Base62 mappers:
160-
```php
161-
'config' => [
162-
1 => '1',
163-
5 => '41',
164-
6 => '2377',
165-
7 => '147299',
166-
8 => '9132313',
167-
],
168-
```
169-
### Usage in Laravel
170-
To publish this configuration file, run the following command:
171-
```bash
172-
php artisan vendor:publish --provider="Nassiry\Encoder\EncoderServiceProvider"
173-
```
174-
After publishing, you can modify the configuration to suit your application's needs.
175-
### Custom Bases: Add Your Own Encoding Scheme
176-
- If you are implementing your own Base class (Base58 or Base64):
177-
- Implement the `BaseEncoderInterface` in your class.
178-
- Add your custom base to the `BaseFactory`.
179-
- Use this `config` array to define any specific configuration required for your custom base.
180-
```php
181-
use Nassiry\Encoder\Bases\BaseEncoderInterface;
182-
183-
class Base58 implements BaseEncoderInterface
184-
{
185-
public function encodeId(int|string $id, int $length): string
186-
{
187-
// Implement encoding logic for Base58
188-
}
189-
190-
public function decodeId(string $hashed): string
191-
{
192-
// Implement decoding logic for Base58
193-
}
194-
195-
public function encodeString(string $string): string;
196-
{
197-
// Implement encoding logic for Base58
198-
}
199-
200-
public function decodeString(string $encoded): string;
201-
{
202-
// Implement decoding logic for Base58
203-
}
204-
}
205-
```
206-
### Register the new base in the `BaseFactory` - `create` method:
207-
```php
208-
return match (strtolower($base)) {
209-
'base62' => new Base62($config),
210-
'base58' => new Base58($config), // Register Base58 here
211-
default => throw EncoderException::unsupportedBase($base),
212-
};
213-
```
214-
### Use your custom base like this:
215-
```php
216-
use Nassiry\Encoder\Encoder;
217-
218-
$encoder = new Encoder('base58');
104+
#### Exception Handling
105+
Covers validation, decoding issues, and base errors.
219106

220-
$encodedId = $encoder->encodeId(12345);
221-
echo "Base58 Encoded ID: $encodedId\n";
107+
- [Exception Handling](docs/exceptions.md)
222108

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.
240111

241-
EncoderException::invalidLength($length);
242-
// Message: "The length must be greater than zero. Given: 0"
112+
- [Testing Guide](docs/testing.md)
243113

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.
116+
[Full Story & Benefits](docs/why.md)
246117

247-
EncoderException::invalidBaseString(string $char, string $base);
248-
// Message: "Invalid character '!' in 'base62' string."
118+
### Why Base62?
119+
[Read the Reasoning](docs/base62.md)
249120

250-
EncoderException::invalidBase(string $base);
251-
// Message: "Encoder base 'base64' is not supported."
121+
### Conclusion: Why Choose This Package?
122+
[ Final Thoughts](docs/conclusion.md)
252123

253-
EncoderException::invalidMethod(string $method);
254-
// Message: "Method 'fooBar' does not exist on the base encoder."
124+
### Contributing
125+
Feel free to submit issues or pull requests to improve the package. Contributions are welcome! Let’s improve it together!
255126

256-
EncoderException::invalidClass(string $className);
257-
// 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
263128

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.
296130

297131
### License
298132
This package is open-source software licensed under the [MIT license](LICENSE).

0 commit comments

Comments
 (0)