Download the PHP package 2rius/dart-transformer without Composer
On this page you can find all versions of the php package 2rius/dart-transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download 2rius/dart-transformer
More information about 2rius/dart-transformer
Files in 2rius/dart-transformer
Package dart-transformer
Short Description Laravel package for converting PHP classes and types to Dart equivalents
License MIT
Homepage https://github.com/2rius/dart-transformer
Informations about the package dart-transformer
Dart Transformer
Laravel package for converting PHP classes and types to Dart equivalents, with special support for Spatie Laravel Data classes and PHP enums.
This package is heavily inspired by Spatie's TypeScript Transformer, but targets Dart instead of TypeScript.
Features
- 🚀 Transform Spatie Laravel Data classes to Dart classes
- 🎯 Convert PHP enums to Dart enums
- 📦 Generate JSON serialization code with
json_annotation - 🔧 Configurable output paths and transformers
- 🎨 Smart type mapping from PHP to Dart
- ⚡ Artisan command for easy transformation
Installation
You can install the package via composer:
You can publish the config file with:
Usage
Generate aggregated Dart definitions
Run the command to generate a single aggregated file with all transformable classes and enums:
By default this writes to resources/dart/generated.dart.
Example Transformations
Laravel Data Class
PHP:
Generated Dart:
PHP Enum
PHP:
Generated Dart:
Configuration
The package provides several configuration options:
- auto_discover_types: Configure which directories to scan for classes
- collectors: Customize which collectors to use
- transformers: Customize which transformers to use
- default_type_replacements: Configure default type replacements when mapping from PHP to Dart types.
- output_file: Set the output file
- formatter: Format the generated Dart file.
- dart: Dart-specific options like nullable types and JSON annotations
Class naming and collisions
Dart does not have PHP-style namespaces inside a single file. When generating a single aggregated Dart file, two PHP classes that share the same short class name (e.g. App\Data\UserData and App\Admin\UserData) will collide.
- By default we use the short class name via
ShortClassNamingStrategy. - If you run into collisions, either:
- switch to
FqcnUnderscoredNamingStrategyin your config to produce names likeApp_Admin_UserData, or - implement your own strategy by implementing
M2rius\\DartTransformer\\Naming\\NamingStrategyand set it indart.naming_strategy.
- switch to
Example config:
Type Mapping
| PHP Type | Dart Type |
|---|---|
int |
int |
float, double |
double |
string |
String |
bool |
bool |
array |
List<dynamic> |
object |
Map<String, dynamic> |
mixed |
dynamic |
| Custom classes | Preserved as-is |
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- Max-Emil Thorius
- All Contributors
- Inspired by Spatie's TypeScript Transformer
License
The MIT License (MIT). Please see License File for more information.
All versions of dart-transformer with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^10.0||^11.0||^12.0
spatie/laravel-data Version ^4.0
roave/better-reflection Version ^6.0