riverwaysoft / php-converter
PHP converter to TypeScript / Dart
Installs: 52 353
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 1
Forks: 2
Open Issues: 29
Requires
- php: >=8.0
- composer/xdebug-handler: ^3.0
- doctrine/inflector: ^2.0
- jawira/case-converter: ^3.4
- jfcherng/php-diff: ^6.10
- nikic/php-parser: ^4.16
- phpstan/phpdoc-parser: ^1.6
- symfony/console: ^5.3|^6.3|^6.2|^6.1|^7.0
- symfony/filesystem: ^5.3|^6.3|^6.2|^6.1|^7.0
- symfony/process: ^5.3|^6.3|^6.2|^6.1|^7.0
- webmozart/assert: ^1.10
Requires (Dev)
- myclabs/php-enum: ^1.8
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.7
- phpstan/phpstan-deprecation-rules: ^1.1
- phpunit/phpunit: ^10
- rregeer/phpunit-coverage-check: ^0.3.1
- slevomat/coding-standard: ^8.13
- spatie/phpunit-snapshot-assertions: ^5
- symfony/var-dumper: ^5.3|^6.0|^7.0
- symplify/easy-coding-standard: ^11.5
- dev-main
- 0.8.2.40
- 0.8.2.39
- 0.8.2.38
- 0.8.2.37
- 0.8.2.36
- 0.8.2.35
- 0.8.2.34
- 0.8.2.33
- 0.8.2.32
- 0.8.2.31
- 0.8.2.30
- 0.8.2.29
- 0.8.2.28
- 0.8.2.27
- 0.8.2.26
- 0.8.2.25
- 0.8.2.24
- 0.8.2.23
- 0.8.2.22
- 0.8.2.21
- 0.8.2.20
- 0.8.2.19
- 0.8.2.18
- 0.8.2.17
- 0.8.2.16
- 0.8.2.15
- 0.8.2.14
- 0.8.2.13
- 0.8.2.12
- 0.8.2.11
- 0.8.2.10
- 0.8.2.9
- 0.8.2.8
- 0.8.2.7
- 0.8.2.6
- 0.8.2.5
- 0.8.2.4
- 0.8.2.3
- 0.8.2.2
- 0.8.2.1
- 0.8.2
- 0.8.1
- 0.7.9.24
- 0.7.9.23
- 0.7.9.22
- 0.7.9.21
- 0.7.9.20
- 0.7.9.19
- 0.7.9.18
- 0.7.9.17
- 0.7.9.16
- 0.7.9.15
- 0.7.9.14
- 0.7.9.13
- 0.7.9.12
- 0.7.9.11
- 0.7.9.10
- 0.7.9.9
- 0.7.9.8
- 0.7.9.7
- 0.7.9.6
- 0.7.9.5
- 0.7.9.4
- 0.7.9.3
- 0.7.9.2
- 0.7.9.1
- 0.7.9
- 0.7.8
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.0
- dev-support-nullable-array-with-question-sign
- dev-release-from-pr
- dev-automatic-release-start
- dev-php8.2
- dev-mitalcoi-patch-2
- dev-mitalcoi-patch-1
- dev-options-from-to-can-be-set-from-config
- dev-support-fqcn-in-php-attribute
- dev-code-reference
- dev-extract-type-resolver
- dev-infection
- dev-revamp-symfony-generator
- dev-support-generics
- dev-ecs
- dev-box-issue
This package is auto-updated.
Last update: 2024-10-29 03:33:04 UTC
README
Generates TypeScript & Dart out of your PHP DTO classes.
Why?
Statically typed languages like TypeScript or Dart are great because they allow catching bugs without even running your code. But unless there are well-defined contracts between the API and consumer apps, you may find yourself frequently adjusting outdated typings whenever the API changes. This library generates types for you, enabling you to move faster and encounter fewer bugs.
Requirements
PHP 8.0 or above
Quick start
- Installation
composer require riverwaysoft/php-converter --dev
If the installation leads to dependency conflicts, consider using the standalone Phar version of the package.
- Mark a few classes with the #[Dto] annotation to convert them into TypeScript or Dart.
use Riverwaysoft\PhpConverter\Filter\Attributes\Dto; #[Dto] class UserOutput { public string $id; public int $age; public ?UserOutput $bestFriend; /** @var UserOutput[] */ public array $friends; }
- Run the CLI command to generate TypeScript
vendor/bin/php-converter --from=/path/to/project/src --to=.
This will generate a file generated.ts
with the following content:
type UserOutput = { id: string; age: number; bestFriend: UserOutput | null; friends: UserOutput[]; }
Features
- Supports all PHP data types including union types, nullable types, and enums.
- Supports PHP DocBlock types, e.g.,
User[]
,int[][]|null
, and generics thanks to phpstan/phpdoc-parser - Custom type resolvers (for instance, for
DateTimeImmutable
). - Generate a single output file or multiple files (1 type per file).
- Option to override the generation logic.
- Flexible class filters with the option to use your own filters.
- Generate API client from Symfony or API Platform code.
Customization
If you'd like to customize the conversion process, you need to copy the config script to your project folder:
cp vendor/riverwaysoft/php-converter/bin/default-config.php config/ts-config.php
Now you can customize this config and run the php-converter using the following script:
vendor/bin/php-converter --from=/path/to/project/src --to=. --config=config/ts-config.php