Skip to content

Commit 58bdf4a

Browse files
authored
Document how to write type resolvers (#21)
1 parent 2d43456 commit 58bdf4a

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

README.md

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ type User = {
5555
- Generate a single output file or multiple files (entity per class)
5656
- Custom class filters
5757
58+
## Error list
59+
Here is a list of errors `dto-converter` can throw and description what to do if you encounter these errors:
60+
61+
### 1. Property z of class X has no type. Please add PHP type
62+
It means that you've forgotten to add type for property `a` of class Y. Example:
63+
64+
```php
65+
#[Dto]
66+
class X {
67+
public $z;
68+
}
69+
```
70+
71+
At the moment there is no strict / loose mode in `dto-converter`. It is always strict. If you don't know the PHP type just use [mixed](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.mixed) type to explicitly convert it to `any`/`Object`. It could silently convert such types to TypeScript `any` or Dart `Object` if we needed it. But we prefer an explicit approach. Feel free to raise an issue if having loose mode makes sense for you.
72+
73+
74+
### 2. PHP Type X is not supported
75+
It means `dto-converter` doesn't know how to convert the type X into TypeScript or Dart. If you are using `#[Dto]` attribute you probably forgot to add it to class `X`. Example:
76+
77+
```php
78+
#[Dto]
79+
class A {
80+
public X $x;
81+
}
82+
83+
class X {
84+
public int $foo;
85+
}
86+
```
87+
5888
## Customize
5989
If you'd like to customize dto-convert you need to copy the generator script to your project folder:
6090

@@ -125,35 +155,35 @@ $application->add(
125155

126156
You can even go further and use `NegationFilter` to exclude specific files as shown in [unit tests](https://github.com/riverwaysoft/dto-converter/blob/a8d5df2c03303c02bc9148bd1d7822d7fe48c5d8/tests/EndToEndTest.php#L297).
127157

158+
### How to write custom type resolvers?
159+
`dto-converter` takes care of converting basic PHP types like number, string and so on. But what to do if you have to convert a type that isn't a DTO? For example `\DateTimeImmutable`. You can write a class that implements [UnknownTypeResolverInterface](https://github.com/riverwaysoft/dto-converter/blob/2d434562c1bc73bcb6819257b31dd75c818f4ab1/src/Language/UnknownTypeResolverInterface.php). `dto-convert` already includes such class in core functionality. There is also a shortcut to achieve it - use [InlineTypeResolver](https://github.com/riverwaysoft/dto-converter/blob/2d434562c1bc73bcb6819257b31dd75c818f4ab1/src/Language/TypeScript/InlineTypeResolver.php):
128160

129-
## Error list
130-
Here is a list of errors `dto-converter` can throw and description what to do if you encounter these errors:
131-
132-
### 1. Property z of class X has no type. Please add PHP type
133-
It means that you've forgotten to add type for property `a` of class Y. Example:
134-
135-
```php
136-
#[Dto]
137-
class X {
138-
public $z;
139-
}
140-
```
141-
142-
At the moment there is no strict / loose mode in `dto-converter`. It is always strict. If you don't know the PHP type just use [mixed](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.mixed) type to explicitly convert it to `any`/`Object`. It could silently convert such types to TypeScript `any` or Dart `Object` if we needed it. But we prefer an explicit approach. Feel free to raise an issue if having loose mode makes sense for you.
143-
144-
145-
### 2. PHP Type X is not supported
146-
It means `dto-converter` doesn't know how to convert the type X into TypeScript or Dart. If you are using `#[Dto]` attribute you probably forgot to add it to class `X`. Example:
147-
148-
```php
149-
#[Dto]
150-
class A {
151-
public X $x;
152-
}
153-
154-
class X {
155-
public int $foo;
156-
}
161+
```diff
162+
$application->add(
163+
new ConvertCommand(
164+
new Converter(Normalizer::factory(
165+
new PhpAttributeFilter('Dto'),
166+
)),
167+
new TypeScriptGenerator(
168+
new SingleFileOutputWriter('generated.ts'),
169+
[
170+
new DateTimeTypeResolver(),
171+
new ClassNameTypeResolver(),
172+
+ new InlineTypeResolver([
173+
+ // Convert libphonnumber object to string
174+
+ 'PhoneNumber' => 'string',
175+
+ // Convert PHP Money object to a custom TS type
176+
+ 'Money' => '{ amount: number; currency: string }',
177+
+ // Convert Doctrine Embeddable to an existing Dto marked as #[Dto]
178+
+ 'SomeDoctrineEmbeddable' => 'SomeDoctrineEmbeddableDto',
179+
+ ])
180+
],
181+
),
182+
new Filesystem(),
183+
new FileSystemCodeProvider('/\.php$/'),
184+
new OutputDiffCalculator(),
185+
)
186+
);
157187
```
158188

159189
## Testing

0 commit comments

Comments
 (0)