riverwaysoft / php-converter Goto Github PK
View Code? Open in Web Editor NEWGenerates TypeScript & Dart out of your PHP DTO classes.
License: MIT License
Generates TypeScript & Dart out of your PHP DTO classes.
License: MIT License
class User
{
public int $id;
public string $name;
public ?string $address;
}
Should be transformed to this
export type User = {
int: number;
name: string;
address: string | null;
}
Example:
/** @see FoodHierarchyController */
export const apiFoodHierarchyGet = (): Promise<FoodHierarchyNodeOutput[]> => {
return axios
.get<FoodHierarchyNodeOutput[]>(`/api/food_hierarchy`)
.then((response) => response.data);
}
Such links only clickable in PHP files so they won't work in JS. But, we can still generate class and method so it's possible to go to the method by copying & globally searching the copied string:
/**
* @see JobSheetMessagesApiController::createMessage
*/
Examples:
Consider using this package: https://github.com/phpstan/phpdoc-parser
class UserOutput {
final String id;
final Profile? profile;
final FullName name
UserOutput({
required this.id,
required this.name,
this.profile,
})
factory UserOutput.fromJson(Map<String, dynamic> json) {
return UserOutput(
id: json['id'],
name: FullName.fromJson(json['name']),
profile: json['profile'] != null ? Profile.fromJson(json['profile']) : null,
}
}
It should be possible to split the generated output (TypeScript or Dart) into different files. For example one class / enum per file with correct imports. To handle imports topological sort should be used.
filters
query parameter that is going to work in GET requestsFor example having this code it'd nice to generate types based on PHPStan's type output:
#[Route('/items', name: 'items_index', methods:['get'] )]
public function index(ManagerRegistry $doctrine): JsonResponse
{
$products = $doctrine
->getRepository(Proassist::class)
->findAll();
$data = [];
foreach ($products as $product) {
$data[] = [
'id' => $product->getId(),
'name' => $product->getName(),
'description' => $product->getDescription(),
];
}
return $this->json($data);
}
PHPStan should known that the type of $data is array<{id: ..., 'name': ..., 'description': ...}>
PHPStan under the hood talk: https://www.youtube.com/watch?v=85Aq2rWpGQE
Example projects (appeared randomly in GitHub search):
At the moment only myclabs/enum is supported
Use AST prettifier
Examples:
We need end-to-end test that gets files from a directory, converts classes to TS/Dart then it checks that the file is created successfully + checks the generated diff.
For inspiration: http://www.inanzzz.com/index.php/post/c7jb/testing-symfony-console-command-with-phpunit
Cover the following topics:
FAQ:
Example: https://github.com/ApiGen/ApiGen
Consider using this library: https://github.com/jfcherng/php-diff/blob/v6/example/demo_cli.php
user: `/api/${string}`
to
user: `/api/users/${string}`
Similar to prisma: https://github.com/prisma/prisma-examples
{ key: { value: Enum } }
There is no need to use vendor package for simple things like kebab case / snake case conversion. The task is to remove jawira/case-converter
from project and rewrite KebabCaseFileNameGenerator and SnakeCaseFileNameGenerator to use custom preg_replace_callback
. Unit tests are required.
At the moment dto-converter
converts PHP enums to TS enums:
<?php
use MyCLabs\Enum\Enum;
class ColorEnum extends Enum
{
private const RED = 0;
private const GREEN = 1;
private const BLUE = 2;
}
This will be converted to:
enum ColorEnum {
RED = 0;
GREEN = 1;
BLUE = 2;
}
But it also makes sense to provide an option to convert PHP Enum to TypeScript union type if someone prefers union types over enums:
type ColorEnum = 0 | 1 | 2;
// or
type Gender = 'male' | 'female' | 'unkown';
This library already can generate union types but only when a specific condition is met: https://github.com/riverwaysoft/dto-converter/blob/8c35d6cafde77fac392ef702ee8dcf97c3d46ce1/src/Language/TypeScript/TypeScriptGenerator.php#L48
The task for PR:
TypeScriptGenerator
's constructor. Something like preferUnionTypeOverEnum
: https://github.com/riverwaysoft/dto-converter/blob/master/src/Language/TypeScript/TypeScriptGenerator.php#L21It should be false by default.
If this flag is set to true - make an early return true
from this function: https://github.com/riverwaysoft/dto-converter/blob/master/src/Language/TypeScript/TypeScriptGenerator.php#L61
Write a test for this feature. Please add it to EndToEndTest.php
: https://github.com/riverwaysoft/dto-converter/blob/master/tests/EndToEndTest.php
Make a fallback to doc block for array types
Currently the following bash can be used:
#!/bin/bash
set -e
GENERATED_FILE=generated.dart
PREV_GENERATED_FILE=current-generated.dart
cp $GENERATED_FILE $PREV_GENERATED_FILE
bin/php-converter.phar --from=./src --to=./ --config=./config/php-converter/dart-config.php
if diff -q $PREV_GENERATED_FILE $GENERATED_FILE; then
echo "DTO ok"
rm $PREV_GENERATED_FILE
else
echo "Dto mismatch"
rm $PREV_GENERATED_FILE
exit 1
fi
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.