Top Related Projects
[Deprecated] A Laravel package for multilingual models
Easy localization for Laravel
Manage Laravel translation files
List of 126 languages for Laravel Framework, Laravel Jetstream, Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova, Laravel Spark and Laravel UI.
Quick Overview
Optimus is a PHP library for transforming, filtering, and manipulating data. It provides a fluent interface for working with arrays, collections, and database results, making it easier to perform complex data operations with minimal code.
Pros
- Fluent and intuitive API for data manipulation
- Supports both arrays and database results
- Extensive set of transformation and filtering methods
- Easily extendable with custom transformers
Cons
- Limited documentation and examples
- May have performance overhead for very large datasets
- Requires PHP 7.2 or higher
- Not actively maintained (last update was in 2021)
Code Examples
Example 1: Basic array transformation
use Jenssegers\Optimus\Optimus;
$data = [
['name' => 'John Doe', 'age' => 30],
['name' => 'Jane Smith', 'age' => 25],
];
$result = Optimus::make($data)
->transform(function ($item) {
$item['name'] = strtoupper($item['name']);
return $item;
})
->get();
Example 2: Filtering and sorting
$result = Optimus::make($data)
->filter(function ($item) {
return $item['age'] > 25;
})
->sortBy('name')
->get();
Example 3: Using built-in transformers
use Jenssegers\Optimus\Transformers\DateTransformer;
$result = Optimus::make($data)
->transform('created_at', new DateTransformer('Y-m-d'))
->get();
Getting Started
To use Optimus in your PHP project, follow these steps:
- Install the library using Composer:
composer require jenssegers/optimus
- Include the Composer autoloader in your PHP file:
require 'vendor/autoload.php';
- Use the Optimus class to start manipulating your data:
use Jenssegers\Optimus\Optimus;
$data = [/* your data array */];
$result = Optimus::make($data)
->transform(/* your transformations */)
->filter(/* your filters */)
->get();
Competitor Comparisons
[Deprecated] A Laravel package for multilingual models
Pros of Laravel-Translatable
- Specifically designed for Laravel, offering seamless integration
- Supports multiple languages and fallback locales out of the box
- Provides a simple and intuitive API for managing translations
Cons of Laravel-Translatable
- Limited to translation functionality, unlike Optimus' broader feature set
- May require additional setup for complex translation scenarios
- Less flexibility in terms of database structure customization
Code Comparison
Laravel-Translatable:
use Dimsav\Translatable\Translatable;
class Product extends Model
{
use Translatable;
public $translatedAttributes = ['name', 'description'];
}
Optimus:
use Jenssegers\Optimus\Optimus;
$optimus = new Optimus(1580030173, 59260789, 1163945558);
$encoded = $optimus->encode(15);
$decoded = $optimus->decode($encoded);
While Laravel-Translatable focuses on model translation, Optimus provides ID obfuscation functionality. Laravel-Translatable integrates directly with Eloquent models, whereas Optimus is used independently for encoding and decoding IDs.
Easy localization for Laravel
Pros of Laravel-localization
- Specifically designed for Laravel, offering seamless integration
- Provides route localization, making it easier to create multilingual URLs
- Includes language negotiation features to automatically detect user's preferred language
Cons of Laravel-localization
- Limited to Laravel framework, not suitable for other PHP projects
- Focuses solely on localization, lacking additional functionality like ID obfuscation
Code Comparison
Laravel-localization:
Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
Route::get('/', function() {
return View::make('hello');
});
});
Optimus:
$optimus = new Optimus(1580030173, 59260789, 1163945558);
$encoded = $optimus->encode(15); // 1103647397
$decoded = $optimus->decode(1103647397); // 15
Summary
Laravel-localization is a specialized package for handling localization in Laravel applications, offering route localization and language negotiation. However, it's limited to Laravel and focuses solely on localization. Optimus, on the other hand, is a more general-purpose PHP library for ID obfuscation, which can be used in various PHP projects but doesn't provide localization features. The choice between these libraries depends on the specific needs of your project, whether it's multilingual support for Laravel or ID obfuscation for any PHP application.
Manage Laravel translation files
Pros of Laravel Translation Manager
- User-friendly web interface for managing translations
- Supports importing translations from files
- Allows in-place editing and searching of translations
Cons of Laravel Translation Manager
- Focused solely on translation management
- May require additional setup for complex translation scenarios
- Limited to Laravel framework
Code Comparison
Laravel Translation Manager:
php artisan translations:import
php artisan translations:find
php artisan translations:export
Optimus:
$optimus = new Optimus(1580030173, 59260789, 1163945558);
$encoded = $optimus->encode(15);
$decoded = $optimus->decode($encoded);
Summary
Laravel Translation Manager is specifically designed for managing translations in Laravel applications, offering a web interface and import/export capabilities. Optimus, on the other hand, is a PHP library for ID obfuscation and does not deal with translations at all.
While Laravel Translation Manager provides a comprehensive solution for translation management, it is limited to the Laravel ecosystem. Optimus offers a different functionality altogether, focusing on ID obfuscation and is framework-agnostic.
The code examples highlight the distinct purposes of these libraries. Laravel Translation Manager uses Artisan commands for translation-related tasks, while Optimus demonstrates its encoding and decoding functionality for ID obfuscation.
List of 126 languages for Laravel Framework, Laravel Jetstream, Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova, Laravel Spark and Laravel UI.
Pros of lang
- Extensive language support with translations for many Laravel-specific terms and phrases
- Regular updates and contributions from the community
- Easy integration with Laravel projects for quick localization
Cons of lang
- Limited to Laravel framework, not as versatile for other PHP projects
- Requires more setup and configuration compared to simple ID obfuscation
- Larger file size and more complex structure
Code Comparison
lang:
// Using lang in a Laravel blade template
{{ __('messages.welcome') }}
// Configuring supported locales in config/app.php
'locales' => ['en', 'es', 'fr', 'de'],
Optimus:
// Creating an Optimus instance
$optimus = new Optimus(1580030173, 59260789, 1163945558);
// Encoding and decoding an ID
$encoded = $optimus->encode(1); // 1103647397
$decoded = $optimus->decode(1103647397); // 1
Summary
Lang focuses on providing comprehensive language translations for Laravel applications, offering a wide range of locales and Laravel-specific terms. It's ideal for projects requiring extensive localization.
Optimus, on the other hand, is a lightweight library for ID obfuscation, useful for creating non-sequential public IDs. It's more versatile and can be used in various PHP projects, not just Laravel.
Choose Lang for full-fledged localization in Laravel applications, and Optimus for simple ID obfuscation in any PHP project.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Optimus id transformation
With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.
Installation
Install using composer:
composer require jenssegers/optimus
If you will be running your code on a 32 bit system or will be working with large prime numbers it is suggested that you install the GMP extension. For debian/ubuntu you can install the extension with one of these commands:
apt-get install php7.4-gmp
apt-get install php8.0-gmp
apt-get install php8.1-gmp
Usage
To get started you will need 3 things;
- Large prime number lower than
2147483647
- The inverse prime so that
(PRIME * INVERSE) & MAXID == 1
- A large random integer lower than
2147483647
Luckily for you, I have included a console command that can do all of this for you. To get started, just run the following command:
> php vendor/bin/optimus spark
Prime: 2123809381
Inverse: 1885413229
Random: 146808189
If you prefer to choose your own prime number (from this list for example), you can pass it to the command to calculate the remaining numbers:
> php vendor/bin/optimus spark 1580030173
Prime: 1580030173
Inverse: 59260789
Random: 1163945558
Using those numbers, you can start creating instances of Optimus($prime, $inverted, $random)
:
use Jenssegers\Optimus\Optimus;
new Optimus(1580030173, 59260789, 1163945558);
NOTE: Make sure that you are using the same constructor values throughout your entire application!
Encoding and decoding
To encode id's, use the encode
method:
$encoded = $optimus->encode(20); // 1535832388
To decode the resulting 1535832388
back to its original value, use the decode
method:
$original = $optimus->decode(1535832388); // 20
Framework Integrations
Laravel
This is an example service provider which registers a shared Optimus instance for your entire application:
<?php
namespace App\Providers;
use Jenssegers\Optimus\Optimus;
use Illuminate\Support\ServiceProvider;
class OptimusServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(Optimus::class, function ($app) {
return new Optimus(1580030173, 59260789, 1163945558);
});
}
}
Once you have created the service provider, add it to the providers array in your config/app.php
configuration file:
App\Providers\OptimusServiceProvider::class,
Laravel's automatic injection will pass this instance where needed. Example controller:
<?php
namespace App\Http\Controllers;
use Jenssegers\Optimus\Optimus;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function show($id, Optimus $optimus)
{
$id = $optimus->decode($id);
}
}
More information: https://laravel.com/docs/5.3/container#resolving
Third-party integrations
- An integration with Laravel is provided by the propaganistas/laravel-fakeid package.
- Laravel Optimus with multiple connections provided by the cybercog/laravel-optimus package.
- An integration with Silex 2 is provided by the jaam/silex-optimus-provider package.
- An integration with Laravel is provided by the elfsundae/laravel-hashid package.
- A PSR-15 middleware provided by the icanhazstring/optimus-middleware package.
Security contact information
To report a security vulnerability, follow these steps.
License
The MIT License.
Top Related Projects
[Deprecated] A Laravel package for multilingual models
Easy localization for Laravel
Manage Laravel translation files
List of 126 languages for Laravel Framework, Laravel Jetstream, Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova, Laravel Spark and Laravel UI.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot